Skip to content

fix(events): serialize deferred Pydantic state models - #6849

Open
andrewrfitz wants to merge 1 commit into
google:mainfrom
andrewrfitz:codex/fix-grounding-metadata-serialization
Open

fix(events): serialize deferred Pydantic state models#6849
andrewrfitz wants to merge 1 commit into
google:mainfrom
andrewrfitz:codex/fix-grounding-metadata-serialization

Conversation

@andrewrfitz

Copy link
Copy Markdown

Link to Issue or Description of Change

1. Link to an existing issue:

Problem:

A Pydantic model configured with defer_build=True can retain a deferred
MockValSer after nested validation. When such a model is stored under
EventActions.state_delta, duck-typed serialization fails before the existing
unknown-value fallback can run. This aborts parallel function-response merging
when propagated grounding metadata is present.

Solution:

When normal state serialization fails, recursively build serializers for
Pydantic models in the state container and retry the original handler. Retrying
the handler preserves the caller's aliases and include/exclude directives. If
serialization still fails, the existing JSON-sanitizing fallback remains
unchanged for genuinely unsupported values such as callables.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally (see documented baseline failures below).

Passing checks:

  • pytest tests/unittests/events/test_event_actions.py tests/unittests/flows/llm_flows/test_functions_simple.py -q
    • 95 passed
  • pytest tests/unittests/events/test_event_actions.py -q under Python 3.10,
    3.11, 3.12, 3.13, and 3.14
    • 17 passed on each version
  • mypy src/google/adk/events/event_actions.py
    • Success: no issues found in 1 source file
  • pre-commit run --files src/google/adk/events/event_actions.py tests/unittests/events/test_event_actions.py
    • all hooks passed
  • uv build
    • source distribution and wheel built successfully

Full-suite evidence:

  • pytest tests/unittests -n auto -q: 12692 passed; the only two failures
    were both cases of
    test_entry_point_loads_only_allowlisted_packages, because the current
    locked environment eagerly loads httpx2.
  • The same two failures reproduce on a detached, unmodified origin/main
    worktree.
  • tox -p auto ran the full suite under Python 3.10 through 3.13. Each version
    had only those same two baseline failures, with 12,627–12,636 tests passing.
  • Python 3.14 tox setup is currently blocked because locked scipy==1.15.3
    has no compatible wheel and requires a Fortran compiler. The affected test
    file passes in an isolated Python 3.14.6 environment using the project's
    default dependencies.

Manual End-to-End (E2E) Tests:

Before the merge, the nested grounding model had a MockValSer. After this
change, the merge completes, its serializer is a SchemaSerializer, and the
merged state preserves both values:

serializer_before=MockValSer
serializer_after=SchemaSerializer
{'temp:_adk_grounding_metadata': {'webSearchQueries': ['some query']},
 'temp:other': 1}

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective.
  • New and existing unit tests pass locally with my changes (two unrelated
    baseline failures are documented above).
  • I have manually tested my changes end-to-end.
  • No downstream dependency changes are required.

Additional context

The regression test defines a fresh deferred Pydantic model and obtains the
nested value through parent-model validation. This avoids an order-dependent
test that could pass accidentally after another test has already built
GroundingMetadata's class serializer.

Deferred Pydantic serializers can remain unbuilt after nested validation, causing EventActions serialization to abort parallel tool response merges. Build nested model serializers before retrying serialization.

Fixes google#6848
@andrewrfitz
andrewrfitz marked this pull request as ready for review August 21, 2026 20:07
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.

GoogleSearchAgentTool parallel responses fail to serialize deferred GroundingMetadata (MockValSer)

2 participants