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
12 changes: 12 additions & 0 deletions sentry_sdk/integrations/openai_agents/spans/ai_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
HostedMCPTool,
ImageGenerationTool,
LocalShellTool,
Model,
WebSearchTool,
)
except ImportError:
Expand Down Expand Up @@ -218,6 +219,17 @@ def ai_client_context(

context.span.set_attribute(SPANDATA.GEN_AI_PROVIDER_NAME, "openai")

request_model_name: "Optional[str]" = None
if isinstance(agent.model, Model) and hasattr(agent.model, "model"):
request_model_name = agent.model.model
elif isinstance(agent.model, str):
request_model_name = agent.model
elif hasattr(agent, "_sentry_request_model"):
request_model_name = agent._sentry_request_model

if request_model_name:
context.span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, request_model_name)

_set_agent_data(context.span, agent)

if client_options["data_collection"]["gen_ai"]["inputs"]:
Expand Down
14 changes: 1 addition & 13 deletions sentry_sdk/integrations/openai_agents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
)

if TYPE_CHECKING:
from typing import Any, Optional
from typing import Any

from agents import TResponseInputItem, Usage

from sentry_sdk._types import TextPart

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

Expand All @@ -53,17 +52,6 @@ def _set_agent_data(span: "Span", agent: "agents.Agent") -> None:
SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens
)

model_name: "Optional[str]" = None
if isinstance(agent.model, Model) and hasattr(agent.model, "model"):
model_name = agent.model.model
elif isinstance(agent.model, str):
model_name = agent.model
elif hasattr(agent, "_sentry_request_model"):
model_name = agent._sentry_request_model

if model_name:
span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)

if agent.model_settings.presence_penalty:
span.set_attribute(
SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY,
Expand Down
8 changes: 0 additions & 8 deletions tests/integrations/openai_agents/test_openai_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,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.model"] == "gpt-4"
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

Expand Down Expand Up @@ -763,7 +762,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.model"] == "gpt-4"
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

Expand Down Expand Up @@ -885,7 +883,6 @@ def test_agent_invocation_span_sync_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.model"] == "gpt-4"
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

Expand Down Expand Up @@ -1149,7 +1146,6 @@ def test_agent_invocation_span_sync(
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.model"] == "gpt-4"
assert invoke_agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert invoke_agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

Expand Down Expand Up @@ -1299,7 +1295,6 @@ async def test_tool_execution_span(
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.model"] == "gpt-4"
assert agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

Expand Down Expand Up @@ -1365,7 +1360,6 @@ async def test_tool_execution_span(
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.model"] == "gpt-4"
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"
Expand Down Expand Up @@ -1557,7 +1551,6 @@ async def test_run_streamed_tool_execution_span(
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.model"] == "gpt-4"
assert agent_span["attributes"]["gen_ai.request.temperature"] == 0.7
assert agent_span["attributes"]["gen_ai.request.top_p"] == 1.0

Expand All @@ -1566,7 +1559,6 @@ async def test_run_streamed_tool_execution_span(
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.model"] == "gpt-4"
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"
Expand Down
Loading