Skip to content

fix: reject non-finite Score values - #1189

Open
Chen Yufeiyang (feiiiiii5) wants to merge 1 commit into
braintrustdata:mainfrom
feiiiiii5:feiiiiii5/1126-D304
Open

Chen Yufeiyang (feiiiiii5) wants to merge 1 commit into
braintrustdata:mainfrom
feiiiiii5:feiiiiii5/1126-D304

Conversation

@feiiiiii5

@feiiiiii5 Chen Yufeiyang (feiiiiii5) commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Score.__post_init__ validated only the [0,1] range. float("nan") passed both comparisons and was stored as a valid score, silently poisoning downstream summary averages (total / count → nan). float("inf") / -float("inf") were already rejected by the range check.

Fix

Reject non-finite floats via math.isfinite before the range check, with a descriptive ValueError. The range check and its original message are unchanged.

Boolean scores are not rejected: OneOrMoreScores explicitly includes bool, and await_or_run_scorer passes a scorer's True/False return straight into Score. An earlier revision of this PR rejected booleans; the Codex review correctly flagged that this broke a supported contract (including the True/False runtime cases in type_tests/test_dict_scorers.py), so it was narrowed to non-finite values only.

Verification

  • Two new unittest methods in src/braintrust/test_score.py (the module nox test_core discovers): non-finite values rejected; 0, 1, 0.5, True, False accepted and round-tripped.
  • test_score.py + type_tests/test_dict_scorers.py: 28 passed, 8 subtests passed (with the repo's plugin set; the bare interpreter needs broken typeguard/braintrust/recording entry points disabled).
  • ruff check and ruff format --check clean.
  • No full nox matrix, network, provider, or model calls used.

Behavior change

A Score constructed with NaN now raises ValueError instead of storing a NaN that corrupts aggregates. All previously valid scores, including boolean scorer returns, behave exactly as before.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T08:11:53.671238Z 992aa5d New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: eb140eaea2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment thread py/src/braintrust/score.py Outdated
Comment on lines +50 to +51
if isinstance(self.score, bool) or not isinstance(self.score, (int, float)):
raise ValueError(f"score ({self.score}) must be a number between 0 and 1")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Normalize boolean scorer returns before enforcing this guard

When an Eval/EvalAsync scorer returns a comparison such as output == expected, OneOrMoreScores explicitly permits the resulting boolean, but await_or_run_scorer passes it unchanged to Score at framework.py:1649. This guard therefore turns both valid outcomes into scorer exceptions, leaving scores missing or replacing them with error-handler scores. Normalize boolean results to 0/1 at the scorer boundaries before enforcing this constructor restriction. The existing True/False runtime cases in type_tests/test_dict_scorers.py, executed by test_types, cover this supported contract.

AGENTS.md reference: AGENTS.md:L150-L150

Useful? React with 👍 / 👎.

@feiiiiii5 Chen Yufeiyang (feiiiiii5) changed the title fix: reject bool and non-finite Score values fix: reject non-finite Score values Nov 26, 2026
@feiiiiii5

Copy link
Copy Markdown
Contributor Author

Addressed the Codex finding: OneOrMoreScores explicitly includes bool and await_or_run_scorer passes boolean scorer returns into Score, so rejecting booleans broke a supported contract (including type_tests/test_dict_scorers.py). The fix is now narrowed to rejecting only non-finite floats (NaN/inf), which is the value that actually poisons downstream averages. The PR title and body are updated and the branch head is 992aa5d. Verified: src/braintrust/test_score.py + src/braintrust/type_tests/test_dict_scorers.py → 28 passed, 8 subtests passed, including the True/False runtime cases; ruff clean.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant