Skip to content

fix(sdk): add None input validation to heuristic metrics#6543

Open
zhoufengen wants to merge 4 commits intocomet-ml:mainfrom
zhoufengen:fix/metrics-none-input-validation
Open

fix(sdk): add None input validation to heuristic metrics#6543
zhoufengen wants to merge 4 commits intocomet-ml:mainfrom
zhoufengen:fix/metrics-none-input-validation

Conversation

@zhoufengen
Copy link
Copy Markdown

Summary

Several heuristic metrics (LevenshteinRatio, Equals, RegexMatch) crash with unhelpful errors when output or reference is None. This commonly happens when LLM calls fail during evaluation and return None for the output field.

Changes

  • LevenshteinRatio: Add None check for output and reference before calling .lower(), raising MetricComputationError with a descriptive message
  • Equals: Add None check for output and reference before str() conversion (previously None was silently converted to the string "None", producing misleading results)
  • RegexMatch: Add None check for output before regex search, raising MetricComputationError

All three now follow the same pattern used by existing metrics like Sentiment, ROUGE, and BLEU that already validate their inputs.

Testing

  • The validation raises MetricComputationError (from opik.exceptions), which is the standard exception type used across the metrics module
  • Normal string inputs continue to work identically
  • None inputs now produce a clear error message instead of AttributeError: 'NoneType' object has no attribute 'lower'

Related Issues

None (discovered during code review)


This PR was authored with AI assistance. All changes have been reviewed for correctness.

LevenshteinRatio.score() crashed with an unhelpful AttributeError when
output or reference was None. Add explicit validation that raises
MetricComputationError with a clear message, consistent with how other
metrics like Sentiment and BLEU handle invalid inputs.
Equals.score() silently converted None to the string "None" via str(),
producing misleading comparison results. Add explicit validation that
raises MetricComputationError with a clear message.
RegexMatch.score() crashed with TypeError when output was None. Add
explicit validation that raises MetricComputationError with a clear
message.
@zhoufengen zhoufengen requested a review from a team as a code owner April 29, 2026 10:30
@github-actions github-actions Bot added python Pull requests that update Python code Python SDK labels Apr 29, 2026
Comment thread sdks/python/src/opik/evaluation/metrics/heuristics/equals.py
Copy link
Copy Markdown
Collaborator

@alexkuzmik alexkuzmik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution. Could you please attach the unit test outputs?

Add tests verifying that Equals, LevenshteinRatio, and RegexMatch
raise MetricComputationError when given None inputs, covering the
validation logic added in this PR.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@github-actions github-actions Bot added the tests Including test files, or tests related like configuration. label Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Python SDK python Pull requests that update Python code tests Including test files, or tests related like configuration.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants