diff --git a/sentry_sdk/integrations/google_genai/streaming.py b/sentry_sdk/integrations/google_genai/streaming.py index 86cdcf29ba..64d5a354d2 100644 --- a/sentry_sdk/integrations/google_genai/streaming.py +++ b/sentry_sdk/integrations/google_genai/streaming.py @@ -59,6 +59,11 @@ def accumulate_streaming_response( model = None for chunk in chunks: + # Keep the last non-empty value, so a chunk without these fields + # doesn't clear one seen earlier. + response_id = getattr(chunk, "response_id", None) or response_id + model = getattr(chunk, "model_version", None) or model + # Extract text and tool calls if getattr(chunk, "candidates", None): for candidate in getattr(chunk, "candidates", []): diff --git a/tests/integrations/google_genai/test_google_genai.py b/tests/integrations/google_genai/test_google_genai.py index 444333e45f..997162b329 100644 --- a/tests/integrations/google_genai/test_google_genai.py +++ b/tests/integrations/google_genai/test_google_genai.py @@ -866,6 +866,14 @@ def test_streaming_generate_content( assert ( chat_span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash" ) + assert ( + chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + == "response-id-stream-123" + ) + assert ( + chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] + == "gemini-1.5-flash" + ) else: events = capture_events() @@ -916,6 +924,10 @@ def test_streaming_generate_content( # Verify model name assert chat_span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash" + assert ( + chat_span["data"][SPANDATA.GEN_AI_RESPONSE_ID] == "response-id-stream-123" + ) + assert chat_span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "gemini-1.5-flash" @pytest.mark.parametrize("span_streaming", [True, False])