Skip to content

Commit 83607b9

Browse files
.
1 parent 203662a commit 83607b9

3 files changed

Lines changed: 46 additions & 17 deletions

File tree

sentry_sdk/_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,7 @@ class SDKInfo(TypedDict):
359359
)
360360

361361
HttpStatusCodeRange = Union[int, Container[int]]
362+
363+
class TextPart(TypedDict):
364+
type: Literal["text"]
365+
content: str

sentry_sdk/integrations/openai_agents/spans/invoke_agent.py

Lines changed: 16 additions & 2 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,13 +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
)
4354
set_data_normalized(
44-
span, SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, message, unpack=False
55+
span,
56+
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
57+
_transform_system_instruction(system_instruction),
58+
unpack=False,
4559
)
4660

4761
original_input = kwargs.get("original_input")

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 26 additions & 15 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,19 +191,26 @@ 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 (
191-
invoke_agent_span["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]
192-
== "You are a helpful test assistant."
193-
)
194-
assert invoke_agent_span["data"]["gen_ai.request.messages"] == safe_serialize(
195-
[
196-
{"content": [{"text": "Test input", "type": "text"}], "role": "user"},
197-
]
198-
)
199-
assert (
200-
invoke_agent_span["data"]["gen_ai.response.text"]
201-
== "Hello, how can I help you?"
202-
)
194+
195+
if send_default_pii:
196+
assert (
197+
invoke_agent_span["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]
198+
== '[{"type": "text", "content": "You are a helpful test assistant."}]'
199+
)
200+
assert invoke_agent_span["data"]["gen_ai.request.messages"] == safe_serialize(
201+
[
202+
{"content": [{"text": "Test input", "type": "text"}], "role": "user"},
203+
]
204+
)
205+
assert (
206+
invoke_agent_span["data"]["gen_ai.response.text"]
207+
== "Hello, how can I help you?"
208+
)
209+
else:
210+
assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in invoke_agent_span["data"]
211+
assert "gen_ai.request.messages" not in invoke_agent_span["data"]
212+
assert "gen_ai.response.text" not in invoke_agent_span["data"]
213+
203214
assert invoke_agent_span["data"]["gen_ai.operation.name"] == "invoke_agent"
204215
assert invoke_agent_span["data"]["gen_ai.system"] == "openai"
205216
assert invoke_agent_span["data"]["gen_ai.agent.name"] == "test_agent"

0 commit comments

Comments
 (0)