Repository navigation
fix(schema): keep structured output JSON serializable - #7591
BlueRaddish wants to merge 4 commits into
Conversation
Serialize validated model output in JSON mode so date-valued AgentTool results survive the real Runner and tool-result serialization. Keep the existing exclusion of None fields for model and list output schemas. Related to google#7480. AI-assisted with OpenAI Codex.
Somay-kousis
left a comment
There was a problem hiding this comment.
Checked the three new tests against the base (a01591c78~1) and the head: test_structured_output_preserves_json_date[False], [True] and test_agent_tool_structured_date_output_survives_anthropic_conversion fail on the base and pass on the head.
One sibling path still has the bug. When an agent has both output_schema and tools and its model can't do both natively (capabilities.output_schema_and_tools is False, which is the case for claude-* names and the LiteLlm providers listed in lite_llm.py), the final answer goes through SetModelResponseTool.run_async, not validate_schema. That tool still dumps in Python mode (set_model_response_tool.py:304 and :313), and get_structured_model_response then does json.dumps(response) (flows/llm_flows/prompt/_schema.py:120). So a date field makes the run raise, on any model, before AgentTool or the Anthropic converter come into it.
Repro: one LlmAgent with output_schema=DatedOutput (day: date), output_key="out", a fake model, InMemoryRunner. With tools=[lookup] the model answers by calling set_model_response with {"day": "2026-10-17"}.
On this branch:
--- output_schema=DatedOutput, tools=[]
capabilities.output_schema_and_tools = False
final text: {"day": "2026-10-17"}
state out: {'day': '2026-10-17'}
--- output_schema=DatedOutput, tools=[lookup]
capabilities.output_schema_and_tools = False
run raised: TypeError: Object of type date is not JSON serializable
Adding mode="json" to the two model_dump calls in SetModelResponseTool.run_async fixes it (I tried it on your branch and reverted):
--- output_schema=DatedOutput, tools=[lookup]
capabilities.output_schema_and_tools = False
final text: {"day": "2026-10-17"}
state out: {'day': '2026-10-17'}
With that change tests/unittests/tools/test_set_model_response_tool.py, tests/unittests/utils, tests/unittests/tools/test_agent_tool.py and tests/unittests/flows give 1900 passed. Might be worth folding in here with a test in test_set_model_response_tool.py, since it's the same dump and the same #7480 case.
Probe script: probe_7591.py (outputs probe_7591_head.out, probe_7591_base.out, probe_7591_suggested.out).
Serialize validated BaseModel output and list items in JSON mode before set_model_response enters event and final-response JSON serialization. Preserve validation feedback and omission of None fields. Refs google#7480 AI-assisted with OpenAI Codex.
Resolve the validate_schema conflict by retaining upstream Annotated unwrapping and type narrowing together with JSON-mode output. Cover annotated scalar, list item and outer list dates and omitted None fields. Refs google#7480 AI-assisted with OpenAI Codex.
Preserve upstream field-name validation with JSON-mode output dumps. AI-assisted with OpenAI Codex. NO_UNIT_GUIDE=This merge introduces no new Python units relative to upstream main 36873b3; callback_metadata.py and fsspec_artifact_service.py are unchanged upstream files. Our delta modifies five existing files.
Link to Issue or Description of Change
Related to #7480. Preserve JSON-compatible structured output when Pydantic turns JSON date strings into Python
dateobjects, including scalar/list validation, AgentTool responses and theset_model_responsefallback used when tools cannot be combined with an output schema.This two-parent integration preserves current upstream Annotated handling, field-name validation,
exclude_none=Trueand validation feedback while retaining JSON-mode model dumping. It merges current main36873b39e918038814f7ad615868f471541d269dinto the existing contribution.Testing Plan
Actual Linux verification on December 2 for native merge
2275e571c02e55529fd9e9341ca4122ffe2b3523:Each lane also passes two unchanged-main controls and all 12 fixed date/Runner/AgentTool/Annotated cases with zero skips. The original
pytest tests/unitteststox command is preserved; only JUnit reporting is added. Source/configuration integrity, ordinary scoped hooks before and after the matrix, and the actual native Git pre-commit hook passed.The official new-unit guide checker passes against exact current main without a waiver. The native merge records the documented
NO_UNIT_GUIDEreason for unchanged Python units imported from upstream; this contribution adds no new Python unit relative to main.The consuming tests use the repository scripted model with real Runner, tools and session state. No live model/provider, browser UI, or cloud acceptance test was run.
Additional context
OpenAI Codex assisted with diagnosis, implementation, verification and this description. Earlier test counts are not presented as validation of this merge.