Skip to content

fix(schema): keep structured output JSON serializable - #7591

Open
BlueRaddish wants to merge 4 commits into
google:mainfrom
BlueRaddish:fix/oct17-ao-adk
Open

BlueRaddish wants to merge 4 commits into
google:mainfrom
BlueRaddish:fix/oct17-ao-adk

Conversation

@BlueRaddish

@BlueRaddish BlueRaddish commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

Related to #7480. Preserve JSON-compatible structured output when Pydantic turns JSON date strings into Python date objects, including scalar/list validation, AgentTool responses and the set_model_response fallback used when tools cannot be combined with an output schema.

This two-parent integration preserves current upstream Annotated handling, field-name validation, exclude_none=True and validation feedback while retaining JSON-mode model dumping. It merges current main 36873b39e918038814f7ad615868f471541d269d into the existing contribution.

Testing Plan

Actual Linux verification on December 2 for native merge 2275e571c02e55529fd9e9341ca4122ffe2b3523:

Python Date failures on unchanged main (of 10) Fixed cases Owning tests Full tox passed Skipped Xfailed Xpassed
3.11 10 12 227 26728 100 66 2
3.12 10 12 227 26719 101 66 2
3.13 10 12 227 26719 101 66 2
3.14 10 12 227 26721 99 66 2

Each lane also passes two unchanged-main controls and all 12 fixed date/Runner/AgentTool/Annotated cases with zero skips. The original pytest tests/unittests tox 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_GUIDE reason 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.

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 Somay-kousis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.
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.

3 participants