MAINT: Standardize JSON serialization in models#1813
Merged
romanlutz merged 3 commits intoMay 27, 2026
Merged
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
848449a to
67b847c
Compare
jsong468
reviewed
May 26, 2026
jsong468
approved these changes
May 26, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
Cleanup pass that standardizes how models do JSON (de)serialization. Two parts.
Part A — Pydantic models: forward to Pydantic built-ins, deprecate the wrappers
ChatMessageandEmbeddingResponseare bothpydantic.BaseModelsubclasses, but each defined ato_json(andChatMessagealso afrom_json) that was a one-line passthrough tomodel_dump_json()/model_validate_json(). The wrappers add no value over the Pydantic built-ins.tests/unit/models/test_chat_message.py(4 call sites in 3 tests; test names renamed to reflect the Pydantic API they exercise)doc/code/memory/embeddings.pyanddoc/code/memory/embeddings.ipynb(1 call site each, kept in sync per the docs sync rule)ChatMessage.to_json,ChatMessage.from_json,EmbeddingResponse.to_json) so external callers get a soft migration window. Each shim now emitsDeprecationWarningviaprint_deprecation_messagewithremoved_in="0.15.0"and forwards to the underlying Pydantic method. Tests added to assert each shim still works and emits the warning.Part B —
ScorerMetrics: stop misnaming the file-based loaderScorerMetricsis a plain@dataclass(not Pydantic) and its serialization helpers have real behavior beyondjson.dumps(asdict(...)):from_jsonaccepted a file path, opened the file, unwrapped a top-level"metrics"key (used by evaluation result files), and filtered out internal underscore-prefixed fields (e.g.,init=Falsecached attrs like_harm_definition_obj).The name
from_jsonstrongly suggested a JSON string argument, which it never accepted. Changes:ScorerMetrics.from_json→ScorerMetrics.from_json_fileto honestly reflect what it does (keptto_jsonas-is — it correctly returns a string).from_jsonas a deprecated shim that forwards tofrom_json_fileand emitsDeprecationWarningviaprint_deprecation_messagewithremoved_in="0.15.0".to_jsonandfrom_json_file— they're now documented as the canonical (de)serialization entry points forScorerMetricssubclasses, andfrom_json_filecalls out that it takes a file path and unwraps the"metrics"key plus filters underscore fields.tests/unit/score/test_scorer_metrics.pyto use the new name, and added a test that asserts the deprecated alias still works and emits the warning.pyrit/score/that hand-rolljson.dumps(asdict(metrics))orjson.load(...)plusScorerMetrics(**...)outside these methods themselves. (scorer_metrics_io.pyparses JSONL with wrapper metadata, which is a different abstraction and unchanged.)from_json_str— no caller needs a string-only round trip today.Verification
uv run pytest tests/unit -x -q— full unit suite passes (8049+ tests); new deprecation tests pass toouv run pre-commit run --all-files— cleangrepconfirms zero remaining non-deprecation in-tree callers of the oldto_json/from_jsonsymbols onChatMessage/EmbeddingResponse/ScorerMetrics.