Repository navigation
Description
🔴 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):
- Each grounding tool writes its
GroundingMetadatainto the same temporary state keytemp:_adk_grounding_metadata(src/google/adk/tools/agent_tool.py:531-534,src/google/adk/tools/mcp_tool/mcp_tool.py:887). merge_parallel_function_response_eventsdumps each event'sEventActionswithmodel_dump(exclude_none=True, by_alias=True)and combines them recursively withdeep_merge_dicts(src/google/adk/flows/llm_flows/tools/_batch_executor.py:52-98)._maybe_add_grounding_metadata(src/google/adk/flows/llm_flows/core/_finalizer.py:310-331) then attaches that single mergedtemp:_adk_grounding_metadatavalue to the finalLlmResponse.
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
webSearchQueriesandgroundingChunksare 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
groundingSupportsorsearchEntryPointwhen the later call omits them) are retained alongside the later call's fields. When call 1 hasgroundingSupportsreferencing call 1'sgroundingChunksand call 2 overwritesgroundingChunkswithoutgroundingSupports, the mergedGroundingMetadataends up with call 1'sgrounding_supportspointing at call 2'sgrounding_chunks(wrong citations or out-of-boundsgrounding_chunk_indices).
Steps to Reproduce:
- Install
google-adk. - 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, offsettinggrounding_chunk_indicesin later supports by the prior chunk count, and define handling for non-list fields likesearch_entry_pointandretrieval_metadata), - Or replace whole-object (last-write-wins at the
GroundingMetadatalevel) 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 on2.14.1, verified code path unchanged onmainat30737fa45(2.15.1) - Desktop OS: Windows 11 (code path is OS-independent)
- Python Version (
python -V): 3.12.10 google-genai2.34.0,pydantic2.14.3
Model Information:
- Are you using LiteLLM: No
- Which model is being used: N/A (reproduced directly with
merge_parallel_function_response_eventsandtypes.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_metadatain the same parallel tool-call batch
Activity
chelsealong commentedon Dec 1, 2026
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.
SoroushRF commentedon Dec 1, 2026
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?
web_search_queries,grounding_chunksandgrounding_supportsin call order, shifting each later support'sgrounding_chunk_indicesby the number of chunks before it, withsearch_entry_point/retrieval_metadatataken from the last call.GroundingMetadatawins 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_eventsand won't open the PR until #8206 lands.sanketpatil06 commentedon Dec 4, 2026
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.