Skip to content
Open
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
4 changes: 4 additions & 0 deletions sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,7 @@ class SDKInfo(TypedDict):
)

HttpStatusCodeRange = Union[int, Container[int]]

class TextPart(TypedDict):
type: Literal["text"]
content: str
6 changes: 6 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,12 @@ class SPANDATA:
Example: 2048
"""

GEN_AI_SYSTEM_INSTRUCTIONS = "gen_ai.system_instructions"
"""
The system instructions passed to the model.
Example: [{"type": "text", "text": "You are a helpful assistant."},{"type": "text", "text": "Be concise and clear."}]
"""

GEN_AI_REQUEST_MESSAGES = "gen_ai.request.messages"
"""
The messages passed to the model. The "content" can be a string or an array of objects.
Expand Down
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,
}
]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System instruction format uses wrong key name

Medium Severity

The _transform_system_instruction function and TextPart TypedDict use "content" as the dictionary key, but the OpenTelemetry semantic conventions and the rest of the codebase use "text". The docstring example for GEN_AI_SYSTEM_INSTRUCTIONS shows {"type": "text", "text": "..."}, and existing code in utils.py, anthropic.py, and langchain.py all use the "text" key. This inconsistency means the telemetry data won't conform to the OTel structure as intended by the PR.

Additional Locations (1)

Fix in Cursor Fix in Web

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

um check the link in the PR description.



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