From f1fee876c30923389fc04d73985b2f9acaf45a4a Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Mon, 5 Oct 2026 09:34:51 +0200 Subject: [PATCH] chore(openai-agents): Only set request parameters on AI Client spans --- .../openai_agents/spans/ai_client.py | 27 ++++++++++++++++ .../integrations/openai_agents/utils.py | 25 --------------- .../openai_agents/test_openai_agents.py | 31 ------------------- 3 files changed, 27 insertions(+), 56 deletions(-) diff --git a/sentry_sdk/integrations/openai_agents/spans/ai_client.py b/sentry_sdk/integrations/openai_agents/spans/ai_client.py index 5c095b6b6c..3b6717c588 100644 --- a/sentry_sdk/integrations/openai_agents/spans/ai_client.py +++ b/sentry_sdk/integrations/openai_agents/spans/ai_client.py @@ -218,6 +218,33 @@ def ai_client_context( _set_agent_data(context.span, agent) + if agent.model_settings.max_tokens: + context.span.set_attribute( + SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens + ) + + if agent.model_settings.presence_penalty: + context.span.set_attribute( + SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, + agent.model_settings.presence_penalty, + ) + + if agent.model_settings.temperature: + context.span.set_attribute( + SPANDATA.GEN_AI_REQUEST_TEMPERATURE, agent.model_settings.temperature + ) + + if agent.model_settings.top_p: + context.span.set_attribute( + SPANDATA.GEN_AI_REQUEST_TOP_P, agent.model_settings.top_p + ) + + if agent.model_settings.frequency_penalty: + context.span.set_attribute( + SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, + agent.model_settings.frequency_penalty, + ) + if client_options["data_collection"]["gen_ai"]["inputs"]: context.span.set_attribute( SPANDATA.GEN_AI_TOOL_DEFINITIONS, diff --git a/sentry_sdk/integrations/openai_agents/utils.py b/sentry_sdk/integrations/openai_agents/utils.py index 7d631aff27..1fdd0d4dd0 100644 --- a/sentry_sdk/integrations/openai_agents/utils.py +++ b/sentry_sdk/integrations/openai_agents/utils.py @@ -51,11 +51,6 @@ def _set_agent_data(span: "Span", agent: "agents.Agent") -> None: span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name) - if agent.model_settings.max_tokens: - span.set_attribute( - SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens - ) - model_name: "Optional[str]" = None if isinstance(agent.model, Model) and hasattr(agent.model, "model"): model_name = agent.model.model @@ -67,26 +62,6 @@ def _set_agent_data(span: "Span", agent: "agents.Agent") -> None: if model_name: span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name) - if agent.model_settings.presence_penalty: - span.set_attribute( - SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, - agent.model_settings.presence_penalty, - ) - - if agent.model_settings.temperature: - span.set_attribute( - SPANDATA.GEN_AI_REQUEST_TEMPERATURE, agent.model_settings.temperature - ) - - if agent.model_settings.top_p: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TOP_P, agent.model_settings.top_p) - - if agent.model_settings.frequency_penalty: - span.set_attribute( - SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, - agent.model_settings.frequency_penalty, - ) - def _set_usage_data(span: "Span", usage: "Usage") -> None: span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, usage.input_tokens) diff --git a/tests/integrations/openai_agents/test_openai_agents.py b/tests/integrations/openai_agents/test_openai_agents.py index 4b3048dc3d..6bdbc9e635 100644 --- a/tests/integrations/openai_agents/test_openai_agents.py +++ b/tests/integrations/openai_agents/test_openai_agents.py @@ -483,10 +483,6 @@ async def test_agent_invocation_span_no_sensitive_data( assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent" assert invoke_agent_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent" - assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100 - assert invoke_agent_span["attributes"]["gen_ai.request.model"] == "gpt-4" - assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7 - assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0 assert ai_client_span["name"] == "chat gpt-4" assert ai_client_span["attributes"]["gen_ai.operation.name"] == "chat" @@ -764,10 +760,6 @@ async def test_agent_invocation_span( assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent" assert invoke_agent_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent" - assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100 - assert invoke_agent_span["attributes"]["gen_ai.request.model"] == "gpt-4" - assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7 - assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0 assert ai_client_span["name"] == "chat gpt-4" assert ai_client_span["attributes"]["gen_ai.operation.name"] == "chat" @@ -887,10 +879,6 @@ def test_agent_invocation_span_sync_no_sensitive_data( assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent" assert invoke_agent_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent" - assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100 - assert invoke_agent_span["attributes"]["gen_ai.request.model"] == "gpt-4" - assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7 - assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0 assert ai_client_span["name"] == "chat gpt-4" assert ai_client_span["attributes"]["gen_ai.operation.name"] == "chat" @@ -1152,10 +1140,6 @@ def test_agent_invocation_span_sync( assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent" assert invoke_agent_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent" - assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100 - assert invoke_agent_span["attributes"]["gen_ai.request.model"] == "gpt-4" - assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7 - assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0 assert ai_client_span["name"] == "chat gpt-4" assert ai_client_span["attributes"]["gen_ai.operation.name"] == "chat" @@ -1302,10 +1286,6 @@ async def test_tool_execution_span( assert agent_span["attributes"]["gen_ai.agent.name"] == "test_agent" assert agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent" - assert agent_span["attributes"]["gen_ai.request.max_tokens"] == 100 - assert agent_span["attributes"]["gen_ai.request.model"] == "gpt-4" - assert agent_span["attributes"]["gen_ai.request.temperature"] == 0.7 - assert agent_span["attributes"]["gen_ai.request.top_p"] == 1.0 assert agent_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" assert ai_client_span1["name"] == "chat gpt-4" @@ -1369,10 +1349,6 @@ async def test_tool_execution_span( assert tool_span["attributes"]["gen_ai.agent.name"] == "test_agent" assert tool_span["attributes"]["gen_ai.operation.name"] == "execute_tool" - assert tool_span["attributes"]["gen_ai.request.max_tokens"] == 100 - assert tool_span["attributes"]["gen_ai.request.model"] == "gpt-4" - assert tool_span["attributes"]["gen_ai.request.temperature"] == 0.7 - assert tool_span["attributes"]["gen_ai.request.top_p"] == 1.0 assert tool_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" assert tool_span["attributes"]["gen_ai.tool.description"] == "A simple tool" assert tool_span["attributes"]["gen_ai.tool.input"] == '{"message": "hello"}' @@ -1562,20 +1538,13 @@ async def test_run_streamed_tool_execution_span( assert agent_span["attributes"]["gen_ai.agent.name"] == "test_agent" assert agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent" - assert agent_span["attributes"]["gen_ai.request.max_tokens"] == 100 - assert agent_span["attributes"]["gen_ai.request.model"] == "gpt-4" - assert agent_span["attributes"]["gen_ai.request.temperature"] == 0.7 - assert agent_span["attributes"]["gen_ai.request.top_p"] == 1.0 assert agent_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" assert tool_span["name"] == "execute_tool simple_test_tool" assert tool_span["attributes"]["gen_ai.agent.name"] == "test_agent" assert tool_span["attributes"]["gen_ai.operation.name"] == "execute_tool" - assert tool_span["attributes"]["gen_ai.request.max_tokens"] == 100 assert tool_span["attributes"]["gen_ai.request.model"] == "gpt-4" - assert tool_span["attributes"]["gen_ai.request.temperature"] == 0.7 - assert tool_span["attributes"]["gen_ai.request.top_p"] == 1.0 assert tool_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" assert tool_span["attributes"]["gen_ai.tool.description"] == "A simple tool" assert tool_span["attributes"]["gen_ai.tool.input"] == '{"message": "hello"}'