Skip to content

Commit 54034dc

Browse files
feat(openai-agents): Set system instruction attribute (#5355)
Set the system instruction attribute on `invoke_agent` spans in the `OpenAIAgentsIntegration`. Stringify the `instructions` parameter before setting the attribute.
1 parent 26cf31d commit 54034dc

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

sentry_sdk/integrations/openai_agents/spans/invoke_agent.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
import agents
1919
from typing import Any, Optional
2020

21+
from sentry_sdk._types import TextPart
22+
23+
24+
def _transform_system_instruction(system_instructions: "str") -> "list[TextPart]":
25+
return [
26+
{
27+
"type": "text",
28+
"content": system_instructions,
29+
}
30+
]
31+
2132

2233
def invoke_agent_span(
2334
context: "agents.RunContextWrapper", agent: "agents.Agent", kwargs: "dict[str, Any]"
@@ -35,16 +46,16 @@ def invoke_agent_span(
3546
if should_send_default_pii():
3647
messages = []
3748
if agent.instructions:
38-
message = (
49+
system_instruction = (
3950
agent.instructions
4051
if isinstance(agent.instructions, str)
4152
else safe_serialize(agent.instructions)
4253
)
43-
messages.append(
44-
{
45-
"content": [{"text": message, "type": "text"}],
46-
"role": "system",
47-
}
54+
set_data_normalized(
55+
span,
56+
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
57+
_transform_system_instruction(system_instruction),
58+
unpack=False,
4859
)
4960

5061
original_input = kwargs.get("original_input")

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ def test_agent_custom_model():
151151

152152

153153
@pytest.mark.asyncio
154+
@pytest.mark.parametrize(
155+
"send_default_pii",
156+
(True, False),
157+
)
154158
async def test_agent_invocation_span(
155-
sentry_init, capture_events, test_agent, mock_model_response
159+
sentry_init, capture_events, test_agent, mock_model_response, send_default_pii
156160
):
157161
"""
158162
Test that the integration creates spans for agent invocations.
@@ -167,7 +171,7 @@ async def test_agent_invocation_span(
167171
sentry_init(
168172
integrations=[OpenAIAgentsIntegration()],
169173
traces_sample_rate=1.0,
170-
send_default_pii=True,
174+
send_default_pii=send_default_pii,
171175
)
172176

173177
events = capture_events()
@@ -187,21 +191,27 @@ async def test_agent_invocation_span(
187191
assert transaction["contexts"]["trace"]["origin"] == "auto.ai.openai_agents"
188192

189193
assert invoke_agent_span["description"] == "invoke_agent test_agent"
190-
assert invoke_agent_span["data"]["gen_ai.request.messages"] == safe_serialize(
191-
[
192-
{
193-
"content": [
194-
{"text": "You are a helpful test assistant.", "type": "text"}
195-
],
196-
"role": "system",
197-
},
198-
{"content": [{"text": "Test input", "type": "text"}], "role": "user"},
199-
]
200-
)
201-
assert (
202-
invoke_agent_span["data"]["gen_ai.response.text"]
203-
== "Hello, how can I help you?"
204-
)
194+
195+
if send_default_pii:
196+
assert invoke_agent_span["data"][
197+
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS
198+
] == safe_serialize(
199+
[{"type": "text", "content": "You are a helpful test assistant."}]
200+
)
201+
assert invoke_agent_span["data"]["gen_ai.request.messages"] == safe_serialize(
202+
[
203+
{"content": [{"text": "Test input", "type": "text"}], "role": "user"},
204+
]
205+
)
206+
assert (
207+
invoke_agent_span["data"]["gen_ai.response.text"]
208+
== "Hello, how can I help you?"
209+
)
210+
else:
211+
assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in invoke_agent_span["data"]
212+
assert "gen_ai.request.messages" not in invoke_agent_span["data"]
213+
assert "gen_ai.response.text" not in invoke_agent_span["data"]
214+
205215
assert invoke_agent_span["data"]["gen_ai.operation.name"] == "invoke_agent"
206216
assert invoke_agent_span["data"]["gen_ai.system"] == "openai"
207217
assert invoke_agent_span["data"]["gen_ai.agent.name"] == "test_agent"

0 commit comments

Comments
 (0)