Skip to content

FIX: write undefined scorer statistics as null instead of bare NaN - #2876

Merged
Richard Lundeen (richlundeen) merged 2 commits into
microsoft:mainfrom
feiiiiii5:fix/scorer-metrics-nonfinite-json
Oct 6, 2026
Merged

Richard Lundeen (richlundeen) merged 2 commits into
microsoft:mainfrom
feiiiiii5:fix/scorer-metrics-nonfinite-json

Conversation

@feiiiiii5

Copy link
Copy Markdown
Contributor

Fixes #2875

Description

_compute_metrics reports an undefined t-test as NaN on purpose (scorer_evaluator.py:711, pinned by test_compute_harm_metrics_partial_agreement), but ScorerMetrics.to_json and both registry writers call json.dumps with its default allow_nan=True, so the row goes out with bare NaN tokens. RFC 8259 has no such literal: Python reads it back and everything looks fine here, while JSON.parse, .NET and Go reject the whole *_metrics.jsonl document — and a reader that does get through evaluates p_value < 0.05 to False, so "undefined" is misread as "not significant".

Serialization now maps every non-finite float to null (non_finite_to_none(), applied at to_json and _metrics_to_registry_dict) and passes allow_nan=False at the two registry write points, so a value that escapes the mapping fails loudly instead of emitting a document other parsers reject. t_statistic and p_value are typed float | None, and the HarmScorerMetrics docstring says plainly that NaN is the in-memory signal and None is what survives a round trip. Nothing about the computation changes.

Stated rather than hidden: doc/code/scoring/4_scorer_metrics.py:184 formats p_value with :.4f, which raises TypeError on None; today it prints nan. I left that file alone because #2860 is editing the same notebook pair — a two-line guard in either place fixes it, and I am happy to add it here if you want it in this PR.

Tests and Documentation

Four new tests, all red on base 7b533109 and green here:

  • test_harm_metrics_to_json_writes_null_for_undefined_statistics, test_to_json_flattens_numpy_scalars_and_drops_infinite_ones (tests/unit/score/test_scorer_metrics.py) — the first fails on base with "t_statistic": NaN present in the output.
  • test_registry_entry_with_undefined_statistics_is_valid_json[add] and [replace] (tests/unit/score/test_scorer_metrics_io.py) — assert the written line contains no NaN token, parses under json.loads(..., parse_constant=...) with a constant handler that raises, and still loads back as a metrics row with t_statistic is None.

Commands run locally, offline, no API keys:

  • PYTHONPATH=. python -m pytest -p no:opik tests/unit/score/test_scorer_metrics.py tests/unit/score/test_scorer_metrics_io.py -q → base (source files reverted, new tests kept): 4 failed, 67 passed; this branch: 71 passed.
  • PYTHONPATH=. python -m pytest -p no:opik tests/unit/score -q → 2796 passed.
  • ruff@0.16.8 check on pyrit/score/scorer_evaluation/ and both test files, and format --check on the four changed files: clean.
  • Not run: ty type checking (its pre-commit hook needs the project's --extra all venv, a torch-sized install that does not complete here) and JupyText (no notebook or documentation file changed). Coverage of the changed lines is 100% by coverage report -m term-missing.

ScorerEvaluator reports an undefined t-test as NaN, and both serialization
paths (ScorerMetrics.to_json and the metrics registry) handed that straight to
json.dumps, which writes the bare NaN token. Python reads it back, so the
round trip looks fine here, but the checked-in *_metrics.jsonl registries are no
longer valid JSON for strict parsers (JSON.parse, .NET, Go).

Serialize non-finite floats as null at both boundaries, with allow_nan=False so
a future NaN fails loudly instead of corrupting a registry, and let
t_statistic/p_value load back as None (the key stays present on purpose).
@feiiiiii5

Copy link
Copy Markdown
Contributor Author

Hi — #2876 has been open since 26 September with CI green and no reply yet, so this is the one nudge I planned.

The short version: _compute_metrics reports an undefined t-test as NaN on purpose (scorer_evaluator.py:711, which test_compute_harm_metrics_partial_agreement pins), but ScorerMetrics.to_json and both registry writers hand that to json.dumps with the default allow_nan=True, so the row goes out with bare NaN tokens. RFC 8259 has no such literal: a strict reader rejects it, and a lenient one turns a deliberately-undefined statistic into a value. Writing null instead keeps the "undefined" distinguishable from "zero" all the way through the registry.

Two things I would rather ask than assume. First, allow_nan=False would make json.dumps raise rather than emit null, which I did not choose because to_json is also used for display paths where raising would be a behaviour change nobody asked for — tell me if you would rather it be strict. Second, the branch is behind main; I have deliberately not rebased, since the base is unrelated to the two files this touches and a rebase here would only make the diff harder to read. Say the word if you want it current.

Happy to close it if you would rather make this change yourself.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@richlundeen
Richard Lundeen (richlundeen) added this pull request to the merge queue Oct 6, 2026
Merged via the queue into microsoft:main with commit f5c442d Oct 6, 2026
50 checks passed
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.

Undefined t-statistic is written as bare NaN, making the metrics registry invalid JSON

2 participants