Skip to content

fix(google_genai): attach grounding_metadata to streaming responses#155

Open
Abhijeet Prasad (AbhiPrasad) wants to merge 1 commit intomainfrom
abhi-google-genai-grounding-metadata
Open

fix(google_genai): attach grounding_metadata to streaming responses#155
Abhijeet Prasad (AbhiPrasad) wants to merge 1 commit intomainfrom
abhi-google-genai-grounding-metadata

Conversation

@AbhiPrasad
Copy link
Copy Markdown
Member

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) commented Mar 26, 2026

resolves #153

AI Summary

Issue

Google Search grounding metadata (grounding_chunks, grounding_supports, search_entry_point, web_search_queries) was silently dropped from span output when using streaming responses.

Root Cause

In py/src/braintrust/integrations/google_genai/tracing.py, the _aggregate_generate_content_chunks() function manually builds the aggregated output dict for streaming responses. It copied finish_reason and safety_ratings from the last candidate, but not grounding_metadata.

For non-streaming responses, the raw GenerateContentResponse Pydantic model was returned directly and serialized via model_dump(exclude_none=True), which naturally included grounding_metadata. So only the streaming path was broken.

Fix

Added extraction of grounding_metadata from the last chunk's candidate in _aggregate_generate_content_chunks(), serializing it via model_dump(exclude_none=True) to match the non-streaming output format:

Tests

Added 4 VCR-backed tests covering all transport modes with real Google API cassettes:

  • test_google_search_grounding[sync] — non-streaming sync
  • test_google_search_grounding[stream] — streaming sync
  • test_google_search_grounding_async[async] — non-streaming async
  • test_google_search_grounding_async[async_stream] — streaming async

Each test verifies that grounding_metadata on the first candidate contains web_search_queries, grounding_chunks, and grounding_supports.

Red/Green Evidence

  • RED: Before the fix, stream and async_stream tests failed with AssertionError: Expected grounding_metadata on first candidate, got keys: ['content', 'finish_reason', 'safety_ratings']
  • GREEN: After the 5-line fix, all 4 grounding tests pass, plus all 23 existing tests pass with no regressions

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Google GenAI: grounding metadata from Google Search not captured in span output

1 participant