Skip to content
4 changes: 2 additions & 2 deletions sentry_sdk/integrations/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ def _set_responses_api_input_data(
if stream is not None and _is_given(stream):
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_STREAMING, stream)

max_tokens = kwargs.get("max_tokens")
max_tokens = kwargs.get("max_output_tokens")
if max_tokens is not None and _is_given(max_tokens):
set_data_normalized(span, SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens)
span.set_data(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens)

presence_penalty = kwargs.get("presence_penalty")
if presence_penalty is not None and _is_given(presence_penalty):
Expand Down
12 changes: 12 additions & 0 deletions tests/integrations/openai/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,7 @@ def test_ai_client_span_responses_api_no_pii(sentry_init, capture_events):
model="gpt-4o",
instructions="You are a coding assistant that talks like a pirate.",
input="How do I check if a Python object is an instance of a class?",
max_output_tokens=100,
)

(transaction,) = events
Expand All @@ -1884,6 +1885,7 @@ def test_ai_client_span_responses_api_no_pii(sentry_init, capture_events):
assert spans[0]["origin"] == "auto.ai.openai"
assert spans[0]["data"] == {
"gen_ai.operation.name": "responses",
"gen_ai.request.max_tokens": 100,
"gen_ai.request.model": "gpt-4o",
"gen_ai.response.model": "response-model-id",
"gen_ai.system": "openai",
Expand Down Expand Up @@ -1984,6 +1986,7 @@ def test_ai_client_span_responses_api(
model="gpt-4o",
instructions=instructions,
input=input,
max_output_tokens=100,
)

(transaction,) = events
Expand All @@ -1995,6 +1998,7 @@ def test_ai_client_span_responses_api(

expected_data = {
"gen_ai.operation.name": "responses",
"gen_ai.request.max_tokens": 100,
"gen_ai.system": "openai",
"gen_ai.response.model": "response-model-id",
"gen_ai.usage.input_tokens": 20,
Expand Down Expand Up @@ -2286,6 +2290,7 @@ async def test_ai_client_span_responses_async_api(
model="gpt-4o",
instructions=instructions,
input=input,
max_output_tokens=100,
)

(transaction,) = events
Expand All @@ -2297,6 +2302,7 @@ async def test_ai_client_span_responses_async_api(

expected_data = {
"gen_ai.operation.name": "responses",
"gen_ai.request.max_tokens": 100,
"gen_ai.request.messages": '["How do I check if a Python object is an instance of a class?"]',
"gen_ai.request.model": "gpt-4o",
"gen_ai.response.model": "response-model-id",
Expand Down Expand Up @@ -2555,6 +2561,7 @@ async def test_ai_client_span_streaming_responses_async_api(
instructions=instructions,
input=input,
stream=True,
max_output_tokens=100,
)
async for _ in result:
pass
Expand All @@ -2568,6 +2575,7 @@ async def test_ai_client_span_streaming_responses_async_api(

expected_data = {
"gen_ai.operation.name": "responses",
"gen_ai.request.max_tokens": 100,
"gen_ai.response.model": "response-model-id",
"gen_ai.response.streaming": True,
"gen_ai.system": "openai",
Expand Down Expand Up @@ -2880,6 +2888,7 @@ def test_streaming_responses_api(
model="some-model",
input="hello",
stream=True,
max_output_tokens=100,
)

response_string = ""
Expand All @@ -2893,6 +2902,7 @@ def test_streaming_responses_api(
(span,) = transaction["spans"]
assert span["op"] == "gen_ai.responses"
assert span["data"][SPANDATA.GEN_AI_SYSTEM] == "openai"
assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100

assert span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "response-model-id"

Expand Down Expand Up @@ -2938,6 +2948,7 @@ async def test_streaming_responses_api_async(
model="some-model",
input="hello",
stream=True,
max_output_tokens=100,
)

response_string = ""
Expand All @@ -2951,6 +2962,7 @@ async def test_streaming_responses_api_async(
(span,) = transaction["spans"]
assert span["op"] == "gen_ai.responses"
assert span["data"][SPANDATA.GEN_AI_SYSTEM] == "openai"
assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100

assert span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "response-model-id"

Expand Down
Loading