Skip to content

Commit 09675e3

Browse files
fix(openai): Attach response model with streamed Responses API (#5554)
Add the `gen_ai.response.model` attribute in the patch for the streaming Responses API.
1 parent 678e9a7 commit 09675e3

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

sentry_sdk/integrations/openai.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,8 @@ def new_iterator() -> "Iterator[ChatCompletionChunk]":
751751
data_buf[0].append(x.delta or "")
752752

753753
if isinstance(x, ResponseCompletedEvent):
754+
span.set_data(SPANDATA.GEN_AI_RESPONSE_MODEL, x.response.model)
755+
754756
_calculate_token_usage(
755757
input,
756758
x.response,
@@ -798,6 +800,8 @@ async def new_iterator_async() -> "AsyncIterator[ChatCompletionChunk]":
798800
data_buf[0].append(x.delta or "")
799801

800802
if isinstance(x, ResponseCompletedEvent):
803+
span.set_data(SPANDATA.GEN_AI_RESPONSE_MODEL, x.response.model)
804+
801805
_calculate_token_usage(
802806
input,
803807
x.response,

tests/integrations/openai/test_openai.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,6 +2445,7 @@ async def test_ai_client_span_streaming_responses_async_api(
24452445

24462446
expected_data = {
24472447
"gen_ai.operation.name": "responses",
2448+
"gen_ai.response.model": "response-model-id",
24482449
"gen_ai.response.streaming": True,
24492450
"gen_ai.system": "openai",
24502451
"gen_ai.response.time_to_first_token": mock.ANY,
@@ -2769,6 +2770,8 @@ def test_streaming_responses_api(
27692770
(span,) = transaction["spans"]
27702771
assert span["op"] == "gen_ai.responses"
27712772

2773+
assert span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "response-model-id"
2774+
27722775
if send_default_pii and include_prompts:
27732776
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '["hello"]'
27742777
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "hello world"
@@ -2824,6 +2827,8 @@ async def test_streaming_responses_api_async(
28242827
(span,) = transaction["spans"]
28252828
assert span["op"] == "gen_ai.responses"
28262829

2830+
assert span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "response-model-id"
2831+
28272832
if send_default_pii and include_prompts:
28282833
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '["hello"]'
28292834
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "hello world"

0 commit comments

Comments
 (0)