From 9fb4bff88c06be8b482646dbc9504abfd8583b83 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Wed, 18 Mar 2026 13:15:49 +0100 Subject: [PATCH 1/2] fix(openai): Use max_output_tokens for Responses API --- sentry_sdk/integrations/openai.py | 4 ++-- tests/integrations/openai/test_openai.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/integrations/openai.py b/sentry_sdk/integrations/openai.py index f47cd46384..97a69eda77 100644 --- a/sentry_sdk/integrations/openai.py +++ b/sentry_sdk/integrations/openai.py @@ -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(span, 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): diff --git a/tests/integrations/openai/test_openai.py b/tests/integrations/openai/test_openai.py index 249a32b768..aac6ded13d 100644 --- a/tests/integrations/openai/test_openai.py +++ b/tests/integrations/openai/test_openai.py @@ -1766,6 +1766,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 @@ -1776,6 +1777,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", @@ -1876,6 +1878,7 @@ def test_ai_client_span_responses_api( model="gpt-4o", instructions=instructions, input=input, + max_output_tokens=100, ) (transaction,) = events @@ -1887,6 +1890,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, @@ -2178,6 +2182,7 @@ async def test_ai_client_span_responses_async_api( model="gpt-4o", instructions=instructions, input=input, + max_output_tokens=100, ) (transaction,) = events @@ -2189,6 +2194,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", @@ -2447,6 +2453,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 @@ -2460,6 +2467,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", @@ -2772,6 +2780,7 @@ def test_streaming_responses_api( model="some-model", input="hello", stream=True, + max_output_tokens=100, ) response_string = "" @@ -2785,6 +2794,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" @@ -2830,6 +2840,7 @@ async def test_streaming_responses_api_async( model="some-model", input="hello", stream=True, + max_output_tokens=100, ) response_string = "" @@ -2843,6 +2854,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" From 9935178bf44b090fd029053a06ff964ce52d94b0 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Wed, 18 Mar 2026 13:18:36 +0100 Subject: [PATCH 2/2] . --- sentry_sdk/integrations/openai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/openai.py b/sentry_sdk/integrations/openai.py index 97a69eda77..a637e4fc47 100644 --- a/sentry_sdk/integrations/openai.py +++ b/sentry_sdk/integrations/openai.py @@ -243,7 +243,7 @@ def _set_responses_api_input_data( max_tokens = kwargs.get("max_output_tokens") if max_tokens is not None and _is_given(max_tokens): - span.set_data(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):