Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions sentry_sdk/integrations/openai_agents/spans/invoke_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
import agents
from typing import Any, Optional

from sentry_sdk._types import TextPart


def _transform_system_instruction(system_instructions: "str") -> "list[TextPart]":
return [
{
"type": "text",
"content": system_instructions,
}
]


def invoke_agent_span(
context: "agents.RunContextWrapper", agent: "agents.Agent", kwargs: "dict[str, Any]"
Expand All @@ -35,16 +46,16 @@ def invoke_agent_span(
if should_send_default_pii():
messages = []
if agent.instructions:
message = (
system_instruction = (
agent.instructions
if isinstance(agent.instructions, str)
else safe_serialize(agent.instructions)
)
messages.append(
{
"content": [{"text": message, "type": "text"}],
"role": "system",
}
set_data_normalized(
span,
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
_transform_system_instruction(system_instruction),
unpack=False,
)

original_input = kwargs.get("original_input")
Expand Down
44 changes: 27 additions & 17 deletions tests/integrations/openai_agents/test_openai_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,12 @@ def test_agent_custom_model():


@pytest.mark.asyncio
@pytest.mark.parametrize(
"send_default_pii",
(True, False),
)
async def test_agent_invocation_span(
sentry_init, capture_events, test_agent, mock_model_response
sentry_init, capture_events, test_agent, mock_model_response, send_default_pii
):
"""
Test that the integration creates spans for agent invocations.
Expand All @@ -167,7 +171,7 @@ async def test_agent_invocation_span(
sentry_init(
integrations=[OpenAIAgentsIntegration()],
traces_sample_rate=1.0,
send_default_pii=True,
send_default_pii=send_default_pii,
)

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

assert invoke_agent_span["description"] == "invoke_agent test_agent"
assert invoke_agent_span["data"]["gen_ai.request.messages"] == safe_serialize(
[
{
"content": [
{"text": "You are a helpful test assistant.", "type": "text"}
],
"role": "system",
},
{"content": [{"text": "Test input", "type": "text"}], "role": "user"},
]
)
assert (
invoke_agent_span["data"]["gen_ai.response.text"]
== "Hello, how can I help you?"
)

if send_default_pii:
assert invoke_agent_span["data"][
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS
] == safe_serialize(
[{"type": "text", "content": "You are a helpful test assistant."}]
)
assert invoke_agent_span["data"]["gen_ai.request.messages"] == safe_serialize(
[
{"content": [{"text": "Test input", "type": "text"}], "role": "user"},
]
)
assert (
invoke_agent_span["data"]["gen_ai.response.text"]
== "Hello, how can I help you?"
)
else:
assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in invoke_agent_span["data"]
assert "gen_ai.request.messages" not in invoke_agent_span["data"]
assert "gen_ai.response.text" not in invoke_agent_span["data"]

assert invoke_agent_span["data"]["gen_ai.operation.name"] == "invoke_agent"
assert invoke_agent_span["data"]["gen_ai.system"] == "openai"
assert invoke_agent_span["data"]["gen_ai.agent.name"] == "test_agent"
Expand Down
Loading