Skip to content

Commit 9b6b9e9

Browse files
wukathCopybara
authored andcommitted
fix: Fix bug where grounding metadata in Gemini 3.1 live was being silently discarded
In Gemini 3.1 live, LlmResponse packets sometimes contain only grounding metadata - don't skip these responses Change-Id: I9e2c1080395588d61aefffd1b6570102930e1fe0
1 parent e13ada7 commit 9b6b9e9

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/google/adk/flows/llm_flows/base_llm_flow.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,7 @@ async def _postprocess_async(
976976
not llm_response.content
977977
and not llm_response.error_code
978978
and not llm_response.interrupted
979+
and not llm_response.grounding_metadata
979980
):
980981
return
981982

@@ -1040,6 +1041,7 @@ async def _postprocess_live(
10401041
and not llm_response.output_transcription
10411042
and not llm_response.usage_metadata
10421043
and not llm_response.live_session_resumption_update
1044+
and not llm_response.grounding_metadata
10431045
):
10441046
return
10451047

tests/unittests/flows/llm_flows/test_base_llm_flow.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,3 +1069,63 @@ async def mock_run_live_sub_agent(child_ctx, *args, **kwargs):
10691069
assert (
10701070
invocation_context.run_config.session_resumption.handle == 'test_handle'
10711071
)
1072+
1073+
1074+
@pytest.mark.asyncio
1075+
async def test_postprocess_live_yields_grounding_metadata_only():
1076+
"""Test that _postprocess_live yields LlmResponse with only grounding_metadata."""
1077+
agent = Agent(name='test_agent')
1078+
invocation_context = await testing_utils.create_invocation_context(
1079+
agent=agent
1080+
)
1081+
flow = BaseLlmFlowForTesting()
1082+
1083+
llm_request = LlmRequest()
1084+
grounding_metadata = types.GroundingMetadata(
1085+
web_search_queries=['test query'],
1086+
)
1087+
llm_response = LlmResponse(grounding_metadata=grounding_metadata)
1088+
model_response_event = Event(
1089+
id=Event.new_id(),
1090+
invocation_id=invocation_context.invocation_id,
1091+
author=agent.name,
1092+
)
1093+
1094+
events = []
1095+
async for event in flow._postprocess_live(
1096+
invocation_context, llm_request, llm_response, model_response_event
1097+
):
1098+
events.append(event)
1099+
1100+
assert len(events) == 1
1101+
assert events[0].grounding_metadata == grounding_metadata
1102+
1103+
1104+
@pytest.mark.asyncio
1105+
async def test_postprocess_async_yields_grounding_metadata_only():
1106+
"""Test that _postprocess_async yields LlmResponse with only grounding_metadata."""
1107+
agent = Agent(name='test_agent')
1108+
invocation_context = await testing_utils.create_invocation_context(
1109+
agent=agent
1110+
)
1111+
flow = BaseLlmFlowForTesting()
1112+
1113+
llm_request = LlmRequest()
1114+
grounding_metadata = types.GroundingMetadata(
1115+
web_search_queries=['test query'],
1116+
)
1117+
llm_response = LlmResponse(grounding_metadata=grounding_metadata)
1118+
model_response_event = Event(
1119+
id=Event.new_id(),
1120+
invocation_id=invocation_context.invocation_id,
1121+
author=agent.name,
1122+
)
1123+
1124+
events = []
1125+
async for event in flow._postprocess_async(
1126+
invocation_context, llm_request, llm_response, model_response_event
1127+
):
1128+
events.append(event)
1129+
1130+
assert len(events) == 1
1131+
assert events[0].grounding_metadata == grounding_metadata

0 commit comments

Comments
 (0)