fix: propagate grounding and citation metadata in streaming responses

Co-authored-by: Sasha Sobran <asobran@google.com>
PiperOrigin-RevId: 868324488
This commit is contained in:
Sasha Sobran
2026-02-10 14:36:21 -08:00
committed by Copybara-Service
parent 6ee5126d1c
commit e6da417292
3 changed files with 448 additions and 0 deletions
+10
View File
@@ -36,6 +36,8 @@ class StreamingResponseAggregator:
self._text = ''
self._thought_text = ''
self._usage_metadata = None
self._grounding_metadata: Optional[types.GroundingMetadata] = None
self._citation_metadata: Optional[types.CitationMetadata] = None
self._response = None
# For progressive SSE streaming mode: accumulate parts in order
@@ -251,6 +253,10 @@ class StreamingResponseAggregator:
self._response = response
llm_response = LlmResponse.create(response)
self._usage_metadata = llm_response.usage_metadata
if llm_response.grounding_metadata:
self._grounding_metadata = llm_response.grounding_metadata
if llm_response.citation_metadata:
self._citation_metadata = llm_response.citation_metadata
# ========== Progressive SSE Streaming (new feature) ==========
# Save finish_reason for final aggregation
@@ -347,6 +353,8 @@ class StreamingResponseAggregator:
return LlmResponse(
content=types.ModelContent(parts=final_parts),
grounding_metadata=self._grounding_metadata,
citation_metadata=self._citation_metadata,
error_code=None
if finish_reason == types.FinishReason.STOP
else finish_reason,
@@ -374,6 +382,8 @@ class StreamingResponseAggregator:
candidate = self._response.candidates[0]
return LlmResponse(
content=types.ModelContent(parts=parts),
grounding_metadata=self._grounding_metadata,
citation_metadata=self._citation_metadata,
error_code=None
if candidate.finish_reason == types.FinishReason.STOP
else candidate.finish_reason,