fix: make EvaluationReport.to_file() reject non-strict JSON - #393
Open
serenearyal wants to merge 1 commit into
Open
fix: make EvaluationReport.to_file() reject non-strict JSON#393serenearyal wants to merge 1 commit into
serenearyal wants to merge 1 commit into
Conversation
…f writing invalid files
serenearyal
requested a deployment
to
manual-approval
September 4, 2026 04:48 — with
GitHub Actions
Waiting
serenearyal
requested a deployment
to
manual-approval
September 4, 2026 04:48 — with
GitHub Actions
Waiting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
EvaluationReport.to_file()had the same three problems that #383 fixed forExperiment.to_file():json.dumpran with defaults, so a NaN or Infinity score was written as the bare literalNaN. Python's lenientjson.loadsreads it back, but strict parsers (jq, JavaScriptJSON.parse, serde) reject the file.overall_score: floatandscores: list[float]make NaN reachable from ordinary aggregation."w"before serialization, so a failure mid-write truncated the file. On main, a valid 224-byte report was cut to 106 bytes of invalid JSON after a save with a non-serializable case value. Ifto_dict()itself raised, the file was left at 0 bytes.encoding=. This was harmless whileensure_asciidefaulted to True, but it becomes a real bug on non-UTF-8 locales once the writer emits UTF-8 text like fix: make Experiment.to_file() reject non-strict JSON instead of writing invalid files #383 does.Changes
EvaluationReport.to_file()now mirrorsExperiment.to_file(): serialize first withensure_ascii=False, allow_nan=False, encode to UTF-8, and write the bytes only on success. NaN, Infinity, and unpaired surrogates raise aValueErrorwith the original error as__cause__. Non-serializable objects raiseTypeErrorbefore the file is touched. TheRaises:docstring is extended to match.EvaluationReport.from_file()reads withencoding="utf-8"so the read side matches the write side, the same asExperiment.from_file().tests/strands_evals/types/test_evaluation_report.py: NaN rejected with the existing file intact, unpaired surrogates rejected without writing, non-serializable value rejected with the existing file intact, and a UTF-8 round trip.This is a plain mirror of #383 rather than a shared helper. Happy to extract one if you prefer; the issue notes this is the third copy of the pattern.
Related Issues
Fixes #384. Same pattern as #383 (Experiment.to_file), which also covers #382.
Documentation PR
None needed. Behaviour change is limited to raising on data that could not produce valid JSON.
Type of Change
Bug fix
Testing
Added four regression tests; all fail on main and pass with this change.
hatch teston Python 3.10, 3.11, 3.12, 3.13, 3.14: 2144 passed on each.hatch fmt --formatter,hatch fmt --linter, mypy, andpre-commit runon the changed files: clean.hatch run test-integnot run: no integration test exercisesEvaluationReport.to_file(), and the suite needs AWS credentials I do not have locally.Reproduced the truncation and NaN cases with a scratch script on main, and confirmed the fixed writer raises before touching the file.
I ran
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.