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.

@ryanaiagent ryanaiagent self-assigned this Jan 22, 2026
@ryanaiagent
Copy link
Collaborator

Hi @VedantMadane , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Can you fix the formatting errors before we can proceed with the review. You can use autoformat.sh.

@ryanaiagent ryanaiagent added the request clarification [Status] The maintainer need clarification or more information from the author label Jan 22, 2026
Run autoformat.sh to fix formatting issues as requested in PR review.
tool_call is part of an ongoing turn, not a terminal event. Removing the premature reset of last_grounding_metadata ensures subsequent messages in the same turn (like another tool_call or turn_complete) retain the grounding information.
@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 implements the extraction of grounding_metadata from Live API responses, fixing a key instability issue. The changes are logical and well-supported by new unit tests. My review identified two potential high-severity issues. First, the interrupted flag from the server may be lost when there is pending text to be flushed, which could lead to incorrect state handling by the client. Second, the grounding_metadata is not reset after a tool_call, which could cause stale metadata to leak into subsequent, unrelated model turns. I've provided detailed comments and suggestions for fixing these issues to improve the robustness of the implementation.

- Add interrupted parameter to __build_full_text_response to preserve interrupted signal when flushing pending text
- Pass interrupted flag in turn_complete and interrupted blocks
- Remove premature reset of last_grounding_metadata after interrupted (not a terminal event)
- Add documentation for tool_call metadata persistence design decision

Addresses review comments:
- HIGH: Lost interrupted signal in full text response
- HIGH: Premature reset after interrupted
- MEDIUM: Duplicate reset logic (simplified by removing premature reset)
Add test_receive_interrupted_with_pending_text_preserves_flag to verify:
- interrupted flag is preserved when flushing pending text
- grounding_metadata is carried through to the flushed response
- accumulated text is properly merged before interruption

Addresses MEDIUM priority review comment about missing test coverage for edge cases.
@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 effectively fixes an issue where grounding_metadata was not being extracted from Live API responses. The changes correctly track and propagate this metadata across various message types, and the new unit tests provide good coverage for these scenarios. I have one suggestion to address a potential logic bug where the grounding metadata could be reset prematurely within a turn, ensuring the fix is robust.

text, last_grounding_metadata
)
text = ''
last_grounding_metadata = None
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This line prematurely resets last_grounding_metadata. The grounding metadata should persist for the entire duration of a turn and only be reset when the turn is complete, as is correctly handled in the turn_complete block.

Other parts of the code that flush buffered text (e.g., when handling tool_call or interrupted signals) correctly preserve the metadata for the remainder of the turn. Resetting it here is inconsistent with that logic and could cause grounding information to be lost before the turn ends.

To ensure consistent behavior, this line should be removed.

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 request clarification [Status] The maintainer need clarification or more information from the author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VertexAiSearchTool query instability

3 participants