Skip to content

Commit d29d509

Browse files
fix(google-genai): Set response ID and model on streaming spans
accumulate_streaming_response() initialized response_id and model to None but never read them from the chunks, so streaming chat spans never had gen_ai.response.id or gen_ai.response.model. Fixes #5812
1 parent 860a060 commit d29d509

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎sentry_sdk/integrations/google_genai/streaming.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ def accumulate_streaming_response(
5959
model = None
6060

6161
for chunk in chunks:
62+
# Keep the last non-empty value, so a chunk without these fields
63+
# doesn't clear one seen earlier.
64+
response_id = getattr(chunk, "response_id", None) or response_id
65+
model = getattr(chunk, "model_version", None) or model
66+
6267
# Extract text and tool calls
6368
if getattr(chunk, "candidates", None):
6469
for candidate in getattr(chunk, "candidates", []):

‎tests/integrations/google_genai/test_google_genai.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,14 @@ def test_streaming_generate_content(
866866
assert (
867867
chat_span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash"
868868
)
869+
assert (
870+
chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID]
871+
== "response-id-stream-123"
872+
)
873+
assert (
874+
chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL]
875+
== "gemini-1.5-flash"
876+
)
869877
else:
870878
events = capture_events()
871879

@@ -916,6 +924,10 @@ def test_streaming_generate_content(
916924

917925
# Verify model name
918926
assert chat_span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash"
927+
assert (
928+
chat_span["data"][SPANDATA.GEN_AI_RESPONSE_ID] == "response-id-stream-123"
929+
)
930+
assert chat_span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "gemini-1.5-flash"
919931

920932

921933
@pytest.mark.parametrize("span_streaming", [True, False])

0 commit comments

Comments
 (0)