Skip to content

Commit e6bb84e

Browse files
chore(openai-agents): Only set request parameters on AI Client spans (#7858)
Remove model request parameters from Invoke Agent spans because the attributes are ambiguous for an agent that can make multiple model calls in the course of its execution. Each model call may have different request parameters.
1 parent 9b7bf29 commit e6bb84e

5 files changed

Lines changed: 29 additions & 68 deletions

File tree

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545

4646
from ..consts import SPAN_ORIGIN
4747
from ..utils import (
48-
_set_agent_data,
4948
_set_input_data,
5049
_set_output_data,
5150
_set_usage_data,
@@ -218,6 +217,7 @@ def ai_client_context(
218217
)
219218

220219
context.span.set_attribute(SPANDATA.GEN_AI_PROVIDER_NAME, "openai")
220+
context.span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)
221221

222222
request_model_name: "Optional[str]" = None
223223
if isinstance(agent.model, Model) and hasattr(agent.model, "model"):
@@ -230,7 +230,32 @@ def ai_client_context(
230230
if request_model_name:
231231
context.span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, request_model_name)
232232

233-
_set_agent_data(context.span, agent)
233+
if agent.model_settings.max_tokens:
234+
context.span.set_attribute(
235+
SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens
236+
)
237+
238+
if agent.model_settings.presence_penalty:
239+
context.span.set_attribute(
240+
SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY,
241+
agent.model_settings.presence_penalty,
242+
)
243+
244+
if agent.model_settings.temperature:
245+
context.span.set_attribute(
246+
SPANDATA.GEN_AI_REQUEST_TEMPERATURE, agent.model_settings.temperature
247+
)
248+
249+
if agent.model_settings.top_p:
250+
context.span.set_attribute(
251+
SPANDATA.GEN_AI_REQUEST_TOP_P, agent.model_settings.top_p
252+
)
253+
254+
if agent.model_settings.frequency_penalty:
255+
context.span.set_attribute(
256+
SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY,
257+
agent.model_settings.frequency_penalty,
258+
)
234259

235260
if client_options["data_collection"]["gen_ai"]["inputs"]:
236261
context.span.set_attribute(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from sentry_sdk.traces import Span, SpanStatus
66

77
from ..consts import SPAN_ORIGIN
8-
from ..utils import _set_agent_data
98

109
if TYPE_CHECKING:
1110
from typing import Any
@@ -36,7 +35,7 @@ def update_execute_tool_span(
3635
) -> None:
3736
client = sentry_sdk.get_client()
3837

39-
_set_agent_data(span, agent)
38+
span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)
4039

4140
if isinstance(result, str) and result.startswith(
4241
"An error occurred while running the tool"

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from sentry_sdk.utils import safe_serialize
1111

1212
from ..consts import SPAN_ORIGIN
13-
from ..utils import _set_agent_data
1413

1514
if TYPE_CHECKING:
1615
from typing import Any, Optional
@@ -70,7 +69,7 @@ def invoke_agent_span(
7069
unpack=False,
7170
)
7271

73-
_set_agent_data(span, agent)
72+
span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)
7473

7574
return span
7675

‎sentry_sdk/integrations/openai_agents/utils.py‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
set_data_normalized,
1515
)
1616
from sentry_sdk.consts import SPANDATA
17-
from sentry_sdk.integrations import DidNotEnable
1817
from sentry_sdk.traces import Span
1918
from sentry_sdk.utils import (
2019
event_from_exception,
@@ -28,11 +27,6 @@
2827

2928
from sentry_sdk._types import TextPart
3029

31-
try:
32-
import agents
33-
except ImportError:
34-
raise DidNotEnable("OpenAI Agents not installed")
35-
3630

3731
def _capture_exception(exc: "Any") -> None:
3832
event, hint = event_from_exception(
@@ -43,36 +37,6 @@ def _capture_exception(exc: "Any") -> None:
4337
sentry_sdk.capture_event(event, hint=hint)
4438

4539

46-
def _set_agent_data(span: "Span", agent: "agents.Agent") -> None:
47-
48-
span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)
49-
50-
if agent.model_settings.max_tokens:
51-
span.set_attribute(
52-
SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens
53-
)
54-
55-
if agent.model_settings.presence_penalty:
56-
span.set_attribute(
57-
SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY,
58-
agent.model_settings.presence_penalty,
59-
)
60-
61-
if agent.model_settings.temperature:
62-
span.set_attribute(
63-
SPANDATA.GEN_AI_REQUEST_TEMPERATURE, agent.model_settings.temperature
64-
)
65-
66-
if agent.model_settings.top_p:
67-
span.set_attribute(SPANDATA.GEN_AI_REQUEST_TOP_P, agent.model_settings.top_p)
68-
69-
if agent.model_settings.frequency_penalty:
70-
span.set_attribute(
71-
SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY,
72-
agent.model_settings.frequency_penalty,
73-
)
74-
75-
7640
def _set_usage_data(span: "Span", usage: "Usage") -> None:
7741
span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, usage.input_tokens)
7842
span.set_attribute(

‎tests/integrations/openai_agents/test_openai_agents.py‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,6 @@ async def test_agent_invocation_span_no_sensitive_data(
482482

483483
assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
484484
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
485-
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
486-
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
487-
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
488485

489486
assert ai_client_span["name"] == "chat gpt-4"
490487
assert ai_client_span["attributes"]["gen_ai.operation.name"] == "chat"
@@ -761,9 +758,6 @@ async def test_agent_invocation_span(
761758

762759
assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
763760
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
764-
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
765-
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
766-
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
767761

768762
assert ai_client_span["name"] == "chat gpt-4"
769763
assert ai_client_span["attributes"]["gen_ai.operation.name"] == "chat"
@@ -882,9 +876,6 @@ def test_agent_invocation_span_sync_no_sensitive_data(
882876
assert invoke_agent_span["name"] == "invoke_agent test_agent"
883877
assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
884878
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
885-
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
886-
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
887-
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
888879

889880
assert ai_client_span["name"] == "chat gpt-4"
890881
assert ai_client_span["attributes"]["gen_ai.operation.name"] == "chat"
@@ -1145,9 +1136,6 @@ def test_agent_invocation_span_sync(
11451136
assert invoke_agent_span["name"] == "invoke_agent test_agent"
11461137
assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
11471138
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
1148-
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
1149-
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
1150-
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
11511139

11521140
assert ai_client_span["name"] == "chat gpt-4"
11531141
assert ai_client_span["attributes"]["gen_ai.operation.name"] == "chat"
@@ -1294,10 +1282,6 @@ async def test_tool_execution_span(
12941282
assert agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
12951283
assert agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
12961284

1297-
assert agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
1298-
assert agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
1299-
assert agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
1300-
13011285
assert ai_client_span1["name"] == "chat gpt-4"
13021286
assert ai_client_span1["attributes"]["gen_ai.operation.name"] == "chat"
13031287
assert ai_client_span1["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai"
@@ -1359,9 +1343,6 @@ async def test_tool_execution_span(
13591343
assert tool_span["attributes"]["gen_ai.agent.name"] == "test_agent"
13601344
assert tool_span["attributes"]["gen_ai.operation.name"] == "execute_tool"
13611345

1362-
assert tool_span["attributes"]["gen_ai.request.max_tokens"] == 100
1363-
assert tool_span["attributes"]["gen_ai.request.temperature"] == 0.7
1364-
assert tool_span["attributes"]["gen_ai.request.top_p"] == 1.0
13651346
assert tool_span["attributes"]["gen_ai.tool.description"] == "A simple tool"
13661347
assert tool_span["attributes"]["gen_ai.tool.input"] == '{"message": "hello"}'
13671348
assert tool_span["attributes"]["gen_ai.tool.name"] == "simple_test_tool"
@@ -1550,17 +1531,10 @@ async def test_run_streamed_tool_execution_span(
15501531
assert agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
15511532
assert agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
15521533

1553-
assert agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
1554-
assert agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
1555-
assert agent_span["attributes"]["gen_ai.request.top_p"] == 1.0
1556-
15571534
assert tool_span["name"] == "execute_tool simple_test_tool"
15581535
assert tool_span["attributes"]["gen_ai.agent.name"] == "test_agent"
15591536
assert tool_span["attributes"]["gen_ai.operation.name"] == "execute_tool"
15601537

1561-
assert tool_span["attributes"]["gen_ai.request.max_tokens"] == 100
1562-
assert tool_span["attributes"]["gen_ai.request.temperature"] == 0.7
1563-
assert tool_span["attributes"]["gen_ai.request.top_p"] == 1.0
15641538
assert tool_span["attributes"]["gen_ai.tool.description"] == "A simple tool"
15651539
assert tool_span["attributes"]["gen_ai.tool.input"] == '{"message": "hello"}'
15661540
assert tool_span["attributes"]["gen_ai.tool.name"] == "simple_test_tool"

0 commit comments

Comments
 (0)