Skip to content

Commit 1bbb772

Browse files
committed
updated some tests
1 parent b54b8dc commit 1bbb772

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

sentry_sdk/integrations/openai.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,15 @@ def _set_request_data(span, kwargs, operation, integration):
174174

175175
# Common attributes
176176
model = kwargs.get("model")
177-
streaming = kwargs.get("stream")
178177
set_data_normalized(span, SPANDATA.GEN_AI_SYSTEM, "openai")
179178
set_data_normalized(span, SPANDATA.GEN_AI_REQUEST_MODEL, model)
180179
set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, operation)
181-
set_data_normalized(span, SPANDATA.AI_STREAMING, streaming)
182180

183181
# Optional attributes
182+
streaming = kwargs.get("stream")
183+
if streaming is not None:
184+
set_data_normalized(span, SPANDATA.AI_STREAMING, streaming)
185+
184186
max_tokens = kwargs.get("max_tokens")
185187
if max_tokens is not None:
186188
set_data_normalized(span, SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens)

tests/integrations/openai/test_openai.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import pytest
23
from openai import AsyncOpenAI, OpenAI, AsyncStream, Stream, OpenAIError
34
from openai.types import CompletionUsage, CreateEmbeddingResponse, Embedding
@@ -52,7 +53,7 @@ async def __call__(self, *args, **kwargs):
5253
)
5354
],
5455
created=10000000,
55-
model="model-id",
56+
model="response-model-id",
5657
object="chat.completion",
5758
usage=CompletionUsage(
5859
completion_tokens=10,
@@ -86,7 +87,7 @@ async def __call__(self, *args, **kwargs):
8687
tool_choice="none",
8788
tools=[],
8889
created_at=10000000,
89-
model="model-id",
90+
model="response-model-id",
9091
object="response",
9192
usage=ResponseUsage(
9293
input_tokens=20,
@@ -143,7 +144,7 @@ def test_nonstreaming_chat_completion(
143144
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]["content"]
144145
assert (
145146
"the model response"
146-
in span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]["content"]
147+
in json.loads(span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT])[0]["content"]
147148
)
148149
else:
149150
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -188,7 +189,7 @@ async def test_nonstreaming_chat_completion_async(
188189
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]["content"]
189190
assert (
190191
"the model response"
191-
in span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]["content"]
192+
in json.loads(span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT])[0]["content"]
192193
)
193194
else:
194195
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -986,7 +987,10 @@ def test_ai_client_span_responses_api_no_pii(sentry_init, capture_events):
986987
assert spans[0]["op"] == "gen_ai.responses"
987988
assert spans[0]["origin"] == "auto.ai.openai"
988989
assert spans[0]["data"] == {
990+
"gen_ai.operation.name": "responses",
989991
"gen_ai.request.model": "gpt-4o",
992+
"gen_ai.response.model": "response-model-id",
993+
"gen_ai.system": "openai",
990994
"gen_ai.usage.input_tokens": 20,
991995
"gen_ai.usage.input_tokens.cached": 5,
992996
"gen_ai.usage.output_tokens": 10,
@@ -1026,8 +1030,11 @@ def test_ai_client_span_responses_api(sentry_init, capture_events):
10261030
assert spans[0]["op"] == "gen_ai.responses"
10271031
assert spans[0]["origin"] == "auto.ai.openai"
10281032
assert spans[0]["data"] == {
1033+
"gen_ai.operation.name": "responses",
10291034
"gen_ai.request.messages": "How do I check if a Python object is an instance of a class?",
10301035
"gen_ai.request.model": "gpt-4o",
1036+
"gen_ai.system": "openai",
1037+
"gen_ai.response.model": "response-model-id",
10311038
"gen_ai.usage.input_tokens": 20,
10321039
"gen_ai.usage.input_tokens.cached": 5,
10331040
"gen_ai.usage.output_tokens": 10,
@@ -1103,8 +1110,11 @@ async def test_ai_client_span_responses_async_api(sentry_init, capture_events):
11031110
assert spans[0]["op"] == "gen_ai.responses"
11041111
assert spans[0]["origin"] == "auto.ai.openai"
11051112
assert spans[0]["data"] == {
1113+
"gen_ai.operation.name": "responses",
11061114
"gen_ai.request.messages": "How do I check if a Python object is an instance of a class?",
11071115
"gen_ai.request.model": "gpt-4o",
1116+
"gen_ai.response.model": "response-model-id",
1117+
"gen_ai.system": "openai",
11081118
"gen_ai.usage.input_tokens": 20,
11091119
"gen_ai.usage.input_tokens.cached": 5,
11101120
"gen_ai.usage.output_tokens": 10,

0 commit comments

Comments
 (0)