Skip to content

Parallel grounding tool calls corrupt or drop grounding_metadata during state_delta merge #8466

Todo

Description

@SoroushRF

🔴 Required Information

Describe the Bug:

Follow-up to #8205 and #8206. While #8206 restores the types.GroundingMetadata type after parallel tool calls, there is still no defined merge policy when two grounding tools (such as two GoogleSearchAgentTool / AgentTool(propagate_grounding_metadata=True) calls, or McpTool with propagate_grounding_metadata=True) run in the same parallel batch.

On current main (30737fa45, version 2.15.1):

  1. Each grounding tool writes its GroundingMetadata into the same temporary state key temp:_adk_grounding_metadata (src/google/adk/tools/agent_tool.py:531-534, src/google/adk/tools/mcp_tool/mcp_tool.py:887).
  2. merge_parallel_function_response_events dumps each event's EventActions with model_dump(exclude_none=True, by_alias=True) and combines them recursively with deep_merge_dicts (src/google/adk/flows/llm_flows/tools/_batch_executor.py:52-98).
  3. _maybe_add_grounding_metadata (src/google/adk/flows/llm_flows/core/_finalizer.py:310-331) then attaches that single merged temp:_adk_grounding_metadata value to the final LlmResponse.

Because deep_merge_dicts overwrites non-dict values (including lists) on key collision while keeping non-overlapping keys from earlier events:

  • Overlapping fields such as webSearchQueries and groundingChunks are overwritten by the later tool call (last-write-wins), dropping the earlier search's queries and sources.
  • Non-overlapping fields from the earlier tool call (for example groundingSupports or searchEntryPoint when the later call omits them) are retained alongside the later call's fields. When call 1 has groundingSupports referencing call 1's groundingChunks and call 2 overwrites groundingChunks without groundingSupports, the merged GroundingMetadata ends up with call 1's grounding_supports pointing at call 2's grounding_chunks (wrong citations or out-of-bounds grounding_chunk_indices).

Steps to Reproduce:

  1. Install google-adk.
  2. Run the snippet under Minimal Reproduction Code below.

Expected Behavior:

merge_parallel_function_response_events should apply an explicit merge policy for temp:_adk_grounding_metadata (similar to how renderUiWidgets is handled separately at src/google/adk/flows/llm_flows/tools/_batch_executor.py:89-101), rather than field-by-field deep_merge_dicts:

  • Either merge citations consistently (e.g., concatenate web_search_queries / grounding_chunks / grounding_supports, offsetting grounding_chunk_indices in later supports by the prior chunk count, and define handling for non-list fields like search_entry_point and retrieval_metadata),
  • Or replace whole-object (last-write-wins at the GroundingMetadata level) so fields from two searches are never mixed into invalid chunk references.

Observed Behavior:

web_search_queries: ['query 2']
grounding_chunks: ['https://c.example']
grounding_supports: [('claim from search 1', [0, 1])]

Search 1's query and chunks are dropped, while search 1's grounding_supports remain with grounding_chunk_indices=[0, 1] against search 2's 1-element grounding_chunks list (index 0 points to the wrong source; index 1 is out of bounds).

Environment Details:

  • ADK Library Version (pip show google-adk): reproduced locally on 2.14.1, verified code path unchanged on main at 30737fa45 (2.15.1)
  • Desktop OS: Windows 11 (code path is OS-independent)
  • Python Version (python -V): 3.12.10
  • google-genai 2.34.0, pydantic 2.14.3

Model Information:

  • Are you using LiteLLM: No
  • Which model is being used: N/A (reproduced directly with merge_parallel_function_response_events and types.GroundingMetadata)

🟡 Optional Information

Regression:

No; this is a consequence of merging serialized EventActions dicts with deep_merge_dicts.

Additional Context:

Opened separately from #8205 / #8206 as requested in #8205 so #8206 can stay focused on restoring the GroundingMetadata type.

Note that sequential grounding tool calls across turns in the same invocation also overwrite temp:_adk_grounding_metadata directly in tool_context.state, so any merge policy may want to clarify whether it applies only within a parallel batch or across multiple grounding calls in a turn/invocation.

Minimal Reproduction Code:

from google.adk.events.event import Event
from google.adk.events.event_actions import EventActions
from google.adk.flows.llm_flows.tools._batch_executor import (
    merge_parallel_function_response_events,
)
from google.genai import types

gm1 = types.GroundingMetadata(
    web_search_queries=['query 1'],
    grounding_chunks=[
        types.GroundingChunk(
            web=types.GroundingChunkWeb(uri='https://a.example', title='A')
        ),
        types.GroundingChunk(
            web=types.GroundingChunkWeb(uri='https://b.example', title='B')
        ),
    ],
    grounding_supports=[
        types.GroundingSupport(
            segment=types.Segment(text='claim from search 1'),
            grounding_chunk_indices=[0, 1],
        )
    ],
)
gm2 = types.GroundingMetadata(
    web_search_queries=['query 2'],
    grounding_chunks=[
        types.GroundingChunk(
            web=types.GroundingChunkWeb(uri='https://c.example', title='C')
        ),
    ],
)

e1 = Event(
    invocation_id='inv-1',
    author='root',
    actions=EventActions(state_delta={'temp:_adk_grounding_metadata': gm1}),
)
e2 = Event(
    invocation_id='inv-1',
    author='root',
    actions=EventActions(state_delta={'temp:_adk_grounding_metadata': gm2}),
)

merged = merge_parallel_function_response_events([e1, e2])
raw = merged.actions.state_delta['temp:_adk_grounding_metadata']
validated = types.GroundingMetadata.model_validate(raw)
print('web_search_queries:', validated.web_search_queries)
print('grounding_chunks:', [c.web.uri for c in validated.grounding_chunks])
print(
    'grounding_supports:',
    [(s.segment.text, s.grounding_chunk_indices) for s in validated.grounding_supports],
)

How often has this issue occurred?:

  • Always (100%), whenever two or more grounding tools write temp:_adk_grounding_metadata in the same parallel tool-call batch

Activity

  1. chelsealong commented on Dec 1, 2026

    @chelsealong
    Contributor

    May I work on this issue (#8466: Parallel grounding tool calls corrupt or drop groundingmetadata during statedelta merge) and, if I can reproduce it, submit a focused fix with regression coverage? Please let me know if someone is already handling it or if you would prefer a different approach. I would use Codex assistance and disclose that in any PR.

  2. SoroushRF commented on Dec 1, 2026

    @SoroushRF
    ContributorAuthor

    Thanks for the offer, @chelsealong. I'm planning to send the fix for this one myself, since it follows on from #8205/#8206 and I already have the repro and context.

    @surajksharma07, which policy would you prefer?

    1. Merge citations: combine web_search_queries, grounding_chunks and grounding_supports in call order, shifting each later support's grounding_chunk_indices by the number of chunks before it, with search_entry_point / retrieval_metadata taken from the last call.
    2. Replace the whole object: the last call's GroundingMetadata wins as one unit, so fields from two searches are never mixed.

    I'd lean toward (1) so no sources are lost, but (2) is the smaller change. I'll keep it scoped to merge_parallel_function_response_events and won't open the PR until #8206 lands.

  3. added
    tools
    on Dec 4, 2026
  4. sanketpatil06 commented on Dec 4, 2026

    @sanketpatil06

    Thanks @SoroushRF. Let's go with option 1 (merge citations) so no sources are dropped. Take non-list fields from the last call, keep the change scoped to merge_parallel_function_response_events, and add regression tests. Please rebase on #8206 once it lands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

tools

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions