Skip to content

Commit f86880c

Browse files
chore(openai-agents): Only set gen_ai.request.model on AI Client spans (#7859)
Remove the request model from Invoke Agent spans because it is ambiguous for an agent that can call different models in the course of its execution.
1 parent 0cc3c72 commit f86880c

3 files changed

Lines changed: 13 additions & 21 deletions

File tree

‎sentry_sdk/integrations/openai_agents/spans/ai_client.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
HostedMCPTool,
1616
ImageGenerationTool,
1717
LocalShellTool,
18+
Model,
1819
WebSearchTool,
1920
)
2021
except ImportError:
@@ -218,6 +219,17 @@ def ai_client_context(
218219

219220
context.span.set_attribute(SPANDATA.GEN_AI_PROVIDER_NAME, "openai")
220221

222+
request_model_name: "Optional[str]" = None
223+
if isinstance(agent.model, Model) and hasattr(agent.model, "model"):
224+
request_model_name = agent.model.model
225+
elif isinstance(agent.model, str):
226+
request_model_name = agent.model
227+
elif hasattr(agent, "_sentry_request_model"):
228+
request_model_name = agent._sentry_request_model
229+
230+
if request_model_name:
231+
context.span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, request_model_name)
232+
221233
_set_agent_data(context.span, agent)
222234

223235
if client_options["data_collection"]["gen_ai"]["inputs"]:

‎sentry_sdk/integrations/openai_agents/utils.py‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@
2222
)
2323

2424
if TYPE_CHECKING:
25-
from typing import Any, Optional
25+
from typing import Any
2626

2727
from agents import TResponseInputItem, Usage
2828

2929
from sentry_sdk._types import TextPart
3030

3131
try:
3232
import agents
33-
from agents import Model
3433
except ImportError:
3534
raise DidNotEnable("OpenAI Agents not installed")
3635

@@ -53,17 +52,6 @@ def _set_agent_data(span: "Span", agent: "agents.Agent") -> None:
5352
SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens
5453
)
5554

56-
model_name: "Optional[str]" = None
57-
if isinstance(agent.model, Model) and hasattr(agent.model, "model"):
58-
model_name = agent.model.model
59-
elif isinstance(agent.model, str):
60-
model_name = agent.model
61-
elif hasattr(agent, "_sentry_request_model"):
62-
model_name = agent._sentry_request_model
63-
64-
if model_name:
65-
span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
66-
6755
if agent.model_settings.presence_penalty:
6856
span.set_attribute(
6957
SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY,

‎tests/integrations/openai_agents/test_openai_agents.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,6 @@ async def test_agent_invocation_span_no_sensitive_data(
483483
assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
484484
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
485485
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
486-
assert invoke_agent_span["attributes"]["gen_ai.request.model"] == "gpt-4"
487486
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
488487
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
489488

@@ -763,7 +762,6 @@ async def test_agent_invocation_span(
763762
assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
764763
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
765764
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
766-
assert invoke_agent_span["attributes"]["gen_ai.request.model"] == "gpt-4"
767765
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
768766
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
769767

@@ -885,7 +883,6 @@ def test_agent_invocation_span_sync_no_sensitive_data(
885883
assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
886884
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
887885
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
888-
assert invoke_agent_span["attributes"]["gen_ai.request.model"] == "gpt-4"
889886
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
890887
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
891888

@@ -1149,7 +1146,6 @@ def test_agent_invocation_span_sync(
11491146
assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
11501147
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
11511148
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
1152-
assert invoke_agent_span["attributes"]["gen_ai.request.model"] == "gpt-4"
11531149
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
11541150
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
11551151

@@ -1299,7 +1295,6 @@ async def test_tool_execution_span(
12991295
assert agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
13001296

13011297
assert agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
1302-
assert agent_span["attributes"]["gen_ai.request.model"] == "gpt-4"
13031298
assert agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
13041299
assert agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
13051300

@@ -1365,7 +1360,6 @@ async def test_tool_execution_span(
13651360
assert tool_span["attributes"]["gen_ai.operation.name"] == "execute_tool"
13661361

13671362
assert tool_span["attributes"]["gen_ai.request.max_tokens"] == 100
1368-
assert tool_span["attributes"]["gen_ai.request.model"] == "gpt-4"
13691363
assert tool_span["attributes"]["gen_ai.request.temperature"] == 0.7
13701364
assert tool_span["attributes"]["gen_ai.request.top_p"] == 1.0
13711365
assert tool_span["attributes"]["gen_ai.tool.description"] == "A simple tool"
@@ -1557,7 +1551,6 @@ async def test_run_streamed_tool_execution_span(
15571551
assert agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
15581552

15591553
assert agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
1560-
assert agent_span["attributes"]["gen_ai.request.model"] == "gpt-4"
15611554
assert agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
15621555
assert agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
15631556

@@ -1566,7 +1559,6 @@ async def test_run_streamed_tool_execution_span(
15661559
assert tool_span["attributes"]["gen_ai.operation.name"] == "execute_tool"
15671560

15681561
assert tool_span["attributes"]["gen_ai.request.max_tokens"] == 100
1569-
assert tool_span["attributes"]["gen_ai.request.model"] == "gpt-4"
15701562
assert tool_span["attributes"]["gen_ai.request.temperature"] == 0.7
15711563
assert tool_span["attributes"]["gen_ai.request.top_p"] == 1.0
15721564
assert tool_span["attributes"]["gen_ai.tool.description"] == "A simple tool"

0 commit comments

Comments
 (0)