Skip to content

Conversation

@VedantMadane
Copy link

Summary

Fixes #3542 - VertexAiSearchTool query instability where grounding metadata is intermittently missing from Live API responses.

Root Cause

The Live API's receive() method in gemini_llm_connection.py extracted various fields from Live API messages (usage_metadata, server_content, tool_call, session_resumption_update) but never extracted grounding_metadata from server_content. This prevented agents from accessing grounding data from Vertex AI Search, even when the backend provided it.

Changes

Modified src/google/adk/models/gemini_llm_connection.py

  • Added tracking variable last_grounding_metadata to accumulate grounding across messages
  • Extract grounding_metadata from message.server_content.grounding_metadata
  • Include grounding_metadata in LlmResponse when yielding:
    • Content responses with parts
    • Turn complete responses
    • Interrupted responses
    • Full text responses (via __build_full_text_response)
  • Added warning log when incomplete grounding_metadata is detected (has retrieval_queries but missing grounding_chunks)

Updated tests/unittests/models/test_gemini_llm_connection.py

  • Fixed existing tests to explicitly set grounding_metadata = None on mock server_content objects
  • Added two new tests:
    • test_receive_extracts_grounding_metadata - verifies grounding_metadata is extracted and included in content responses
    • test_receive_grounding_metadata_at_turn_complete - verifies grounding_metadata is carried over to turn_complete response

Test Results

All 25 tests pass, including the 2 new grounding_metadata tests.

Impact

This fix ensures that grounding data from Vertex AI Search is properly extracted and attached to LlmResponse events, allowing agents to access event.grounding_metadata.grounding_chunks when available. The ~10% failure rate mentioned in #3542 was due to the Vertex AI Search backend sometimes returning incomplete responses with only retrieval_queries but no grounding_chunks. The new warning log will help identify such backend issues.

Fixes google#3542

Extract grounding_metadata from message.server_content.grounding_metadata
in the Live API receive() method and include it in LlmResponse events.
This allows VertexAiSearchTool grounding data to be accessible to agents.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @VedantMadane, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical bug that caused instability in Vertex AI Search queries by failing to extract grounding_metadata from Live API responses. The changes ensure that this essential data is now correctly captured and propagated through the LLM response events, allowing agents to leverage grounding information for improved accuracy. Additionally, diagnostic logging has been implemented to flag incomplete grounding data, and comprehensive unit tests confirm the fix's effectiveness.

Highlights

  • Grounding Metadata Extraction: The core issue of grounding_metadata intermittently missing from Live API responses, causing Vertex AI Search query instability, has been fixed by ensuring its proper extraction from message.server_content.grounding_metadata.
  • Metadata Propagation: The extracted grounding_metadata is now correctly included in LlmResponse events for content responses with parts, turn complete responses, interrupted responses, and full text responses, making it accessible to agents.
  • Diagnostic Logging: A warning log has been added to identify cases where grounding_metadata is incomplete (i.e., contains retrieval_queries but lacks grounding_chunks), which helps in diagnosing transient backend issues with Vertex AI Search.
  • Test Coverage: Existing unit tests were updated to explicitly handle grounding_metadata, and two new tests were introduced to specifically verify the extraction and propagation of grounding_metadata in various response scenarios.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added the live [Component] This issue is related to live, voice and video chat label Jan 20, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly identifies and fixes an issue where grounding_metadata was not being extracted from Live API responses. The approach of using last_grounding_metadata to persist the data across messages within a turn is sound.

However, I've identified a few issues with the implementation of the state reset logic:

  • A critical bug where grounding_metadata is prematurely cleared before being added to a turn_complete response.
  • An omission where grounding_metadata is not added to tool_call responses.
  • Some code duplication in the state reset logic that can be simplified.

I've left specific comments with suggestions to address these points. Additionally, I've suggested expanding the new tests to cover these edge cases to prevent future regressions. Once these issues are addressed, this will be a solid fix.

- Fix critical bug: Remove premature reset of last_grounding_metadata before turn_complete response to prevent data loss

- Simplify duplicate reset logic in interrupted handling

- Add grounding_metadata propagation to tool_call responses

- Add test for grounding_metadata with text content + turn_complete

- Add test for grounding_metadata with tool_call responses

All 27 tests pass.
@VedantMadane
Copy link
Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly identifies and fixes an issue where grounding_metadata was not being extracted from Live API responses. The changes introduce logic to track this metadata across messages within a turn. However, there are several places where this new metadata state is reset prematurely, which could lead to it being dropped before the turn is complete. I've added specific comments with suggestions to address this state management issue. Once that is fixed, this will be a solid improvement.

Comment on lines 318 to +330
if message.tool_call:
if text:
yield self.__build_full_text_response(text)
yield self.__build_full_text_response(text, last_grounding_metadata)
text = ''
parts = [
types.Part(function_call=function_call)
for function_call in message.tool_call.function_calls
]
yield LlmResponse(content=types.Content(role='model', parts=parts))
yield LlmResponse(
content=types.Content(role='model', parts=parts),
grounding_metadata=last_grounding_metadata,
)
last_grounding_metadata = None # Reset after yielding
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The last_grounding_metadata is being reset to None prematurely. A tool_call is part of a turn, not a terminal event. Resetting the metadata here will cause any subsequent messages in the same turn (like another tool_call or the final turn_complete message) to lose the grounding information.

Suggested change
if message.tool_call:
if text:
yield self.__build_full_text_response(text)
yield self.__build_full_text_response(text, last_grounding_metadata)
text = ''
parts = [
types.Part(function_call=function_call)
for function_call in message.tool_call.function_calls
]
yield LlmResponse(content=types.Content(role='model', parts=parts))
yield LlmResponse(
content=types.Content(role='model', parts=parts),
grounding_metadata=last_grounding_metadata,
)
last_grounding_metadata = None # Reset after yielding
if message.tool_call:
if text:
yield self.__build_full_text_response(text, last_grounding_metadata)
text = ''
parts = [
types.Part(function_call=function_call)
for function_call in message.tool_call.function_calls
]
yield LlmResponse(
content=types.Content(role='model', parts=parts),
grounding_metadata=last_grounding_metadata,
)

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

Labels

live [Component] This issue is related to live, voice and video chat

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VertexAiSearchTool query instability

2 participants