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
29 changes: 27 additions & 2 deletions sentry_sdk/integrations/openai_agents/spans/ai_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

from ..consts import SPAN_ORIGIN
from ..utils import (
_set_agent_data,
_set_input_data,
_set_output_data,
_set_usage_data,
Expand Down Expand Up @@ -218,6 +217,7 @@ def ai_client_context(
)

context.span.set_attribute(SPANDATA.GEN_AI_PROVIDER_NAME, "openai")
context.span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)

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

_set_agent_data(context.span, agent)
if agent.model_settings.max_tokens:
context.span.set_attribute(
SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens
)

if agent.model_settings.presence_penalty:
context.span.set_attribute(
SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY,
agent.model_settings.presence_penalty,
)

if agent.model_settings.temperature:
context.span.set_attribute(
SPANDATA.GEN_AI_REQUEST_TEMPERATURE, agent.model_settings.temperature
)

if agent.model_settings.top_p:
context.span.set_attribute(
SPANDATA.GEN_AI_REQUEST_TOP_P, agent.model_settings.top_p
)

if agent.model_settings.frequency_penalty:
context.span.set_attribute(
SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY,
agent.model_settings.frequency_penalty,
)

if client_options["data_collection"]["gen_ai"]["inputs"]:
context.span.set_attribute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from sentry_sdk.traces import Span, SpanStatus

from ..consts import SPAN_ORIGIN
from ..utils import _set_agent_data

if TYPE_CHECKING:
from typing import Any
Expand Down Expand Up @@ -36,7 +35,7 @@ def update_execute_tool_span(
) -> None:
client = sentry_sdk.get_client()

_set_agent_data(span, agent)
span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)

if isinstance(result, str) and result.startswith(
"An error occurred while running the tool"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from sentry_sdk.utils import safe_serialize

from ..consts import SPAN_ORIGIN
from ..utils import _set_agent_data

if TYPE_CHECKING:
from typing import Any, Optional
Expand Down Expand Up @@ -70,7 +69,7 @@ def invoke_agent_span(
unpack=False,
)

_set_agent_data(span, agent)
span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)

return span

Expand Down
36 changes: 0 additions & 36 deletions sentry_sdk/integrations/openai_agents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
set_data_normalized,
)
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.traces import Span
from sentry_sdk.utils import (
event_from_exception,
Expand All @@ -28,11 +27,6 @@

from sentry_sdk._types import TextPart

try:
import agents
except ImportError:
raise DidNotEnable("OpenAI Agents not installed")


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


def _set_agent_data(span: "Span", agent: "agents.Agent") -> None:

span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)

if agent.model_settings.max_tokens:
span.set_attribute(
SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens
)

if agent.model_settings.presence_penalty:
span.set_attribute(
SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY,
agent.model_settings.presence_penalty,
)

if agent.model_settings.temperature:
span.set_attribute(
SPANDATA.GEN_AI_REQUEST_TEMPERATURE, agent.model_settings.temperature
)

if agent.model_settings.top_p:
span.set_attribute(SPANDATA.GEN_AI_REQUEST_TOP_P, agent.model_settings.top_p)

if agent.model_settings.frequency_penalty:
span.set_attribute(
SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY,
agent.model_settings.frequency_penalty,
)


def _set_usage_data(span: "Span", usage: "Usage") -> None:
span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, usage.input_tokens)
span.set_attribute(
Expand Down
26 changes: 0 additions & 26 deletions tests/integrations/openai_agents/test_openai_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,6 @@ async def test_agent_invocation_span_no_sensitive_data(

assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

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

assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

assert ai_client_span["name"] == "chat gpt-4"
assert ai_client_span["attributes"]["gen_ai.operation.name"] == "chat"
Expand Down Expand Up @@ -882,9 +876,6 @@ def test_agent_invocation_span_sync_no_sensitive_data(
assert invoke_agent_span["name"] == "invoke_agent test_agent"
assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

assert ai_client_span["name"] == "chat gpt-4"
assert ai_client_span["attributes"]["gen_ai.operation.name"] == "chat"
Expand Down Expand Up @@ -1145,9 +1136,6 @@ def test_agent_invocation_span_sync(
assert invoke_agent_span["name"] == "invoke_agent test_agent"
assert invoke_agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"
assert invoke_agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
assert invoke_agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

assert ai_client_span["name"] == "chat gpt-4"
assert ai_client_span["attributes"]["gen_ai.operation.name"] == "chat"
Expand Down Expand Up @@ -1294,10 +1282,6 @@ async def test_tool_execution_span(
assert agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
assert agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"

assert agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
assert agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

assert ai_client_span1["name"] == "chat gpt-4"
assert ai_client_span1["attributes"]["gen_ai.operation.name"] == "chat"
assert ai_client_span1["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai"
Expand Down Expand Up @@ -1359,9 +1343,6 @@ async def test_tool_execution_span(
assert tool_span["attributes"]["gen_ai.agent.name"] == "test_agent"
assert tool_span["attributes"]["gen_ai.operation.name"] == "execute_tool"

assert tool_span["attributes"]["gen_ai.request.max_tokens"] == 100
assert tool_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert tool_span["attributes"]["gen_ai.request.top_p"] == 1.0
assert tool_span["attributes"]["gen_ai.tool.description"] == "A simple tool"
assert tool_span["attributes"]["gen_ai.tool.input"] == '{"message": "hello"}'
assert tool_span["attributes"]["gen_ai.tool.name"] == "simple_test_tool"
Expand Down Expand Up @@ -1550,17 +1531,10 @@ async def test_run_streamed_tool_execution_span(
assert agent_span["attributes"]["gen_ai.agent.name"] == "test_agent"
assert agent_span["attributes"]["gen_ai.operation.name"] == "invoke_agent"

assert agent_span["attributes"]["gen_ai.request.max_tokens"] == 100
assert agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

assert tool_span["name"] == "execute_tool simple_test_tool"
assert tool_span["attributes"]["gen_ai.agent.name"] == "test_agent"
assert tool_span["attributes"]["gen_ai.operation.name"] == "execute_tool"

assert tool_span["attributes"]["gen_ai.request.max_tokens"] == 100
assert tool_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert tool_span["attributes"]["gen_ai.request.top_p"] == 1.0
assert tool_span["attributes"]["gen_ai.tool.description"] == "A simple tool"
assert tool_span["attributes"]["gen_ai.tool.input"] == '{"message": "hello"}'
assert tool_span["attributes"]["gen_ai.tool.name"] == "simple_test_tool"
Expand Down
Loading