Skip to content

Commit e8161c5

Browse files
ref(openai-agents): Deduplicate request model lookup
Extract shared agent/request model resolution into one helper and reuse it from the AI client span and agent-data call sites. Co-authored-by: Tiago Vilas Boas <tcarvalhovb@gmail.com>
1 parent a618e3a commit e8161c5

3 files changed

Lines changed: 54 additions & 13 deletions

File tree

‎sentry_sdk/integrations/openai_agents/spans/ai_client.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
from ..consts import SPAN_ORIGIN
4848
from ..utils import (
49+
_get_request_model_name,
4950
_set_agent_data,
5051
_set_input_data,
5152
_set_output_data,
@@ -201,11 +202,7 @@ def ai_client_span(
201202
agent: "Agent", get_response_kwargs: "dict[str, Any]"
202203
) -> "Union[sentry_sdk.tracing.Span, StreamedSpan]":
203204
# TODO-anton: implement other types of operations. Now "chat" is hardcoded.
204-
model_name = None
205-
if agent.model:
206-
model_name = agent.model.model if hasattr(agent.model, "model") else agent.model
207-
elif hasattr(agent, "_sentry_request_model"):
208-
model_name = agent._sentry_request_model
205+
model_name = _get_request_model_name(agent)
209206

210207
client_options = sentry_sdk.get_client().options
211208

‎sentry_sdk/integrations/openai_agents/utils.py‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ def _capture_exception(exc: "Any") -> None:
4848
sentry_sdk.capture_event(event, hint=hint)
4949

5050

51+
def _get_request_model_name(agent: "agents.Agent") -> "Optional[str]":
52+
"""Resolve the request model name used on openai-agents spans.
53+
54+
Precedence matches the previous duplicated lookups:
55+
1. ``agent.model.model`` when ``agent.model`` is a ``Model`` instance
56+
2. ``agent.model`` when it is a string
57+
3. ``agent._sentry_request_model`` when the agent uses a default model
58+
"""
59+
if isinstance(agent.model, Model) and hasattr(agent.model, "model"):
60+
return agent.model.model
61+
if isinstance(agent.model, str):
62+
return agent.model
63+
if hasattr(agent, "_sentry_request_model"):
64+
return agent._sentry_request_model
65+
return None
66+
67+
5168
def _set_agent_data(
5269
span: "Union[sentry_sdk.tracing.Span, StreamedSpan]", agent: "agents.Agent"
5370
) -> None:
@@ -64,14 +81,7 @@ def _set_agent_data(
6481
if agent.model_settings.max_tokens:
6582
set_on_span(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens)
6683

67-
model_name: "Optional[str]" = None
68-
if isinstance(agent.model, Model) and hasattr(agent.model, "model"):
69-
model_name = agent.model.model
70-
elif isinstance(agent.model, str):
71-
model_name = agent.model
72-
elif hasattr(agent, "_sentry_request_model"):
73-
model_name = agent._sentry_request_model
74-
84+
model_name = _get_request_model_name(agent)
7585
if model_name:
7686
set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
7787

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from agents import Agent
2+
from agents.models.openai_responses import OpenAIResponsesModel
3+
from openai import AsyncOpenAI
4+
5+
from sentry_sdk.integrations.openai_agents.utils import _get_request_model_name
6+
7+
8+
def test_request_model_name_prefers_model_instance_over_sentry_fallback():
9+
client = AsyncOpenAI(api_key="test-key")
10+
model = OpenAIResponsesModel(model="gpt-4", openai_client=client)
11+
agent = Agent(name="test_agent", model=model)
12+
agent._sentry_request_model = "fallback-model"
13+
14+
assert _get_request_model_name(agent) == "gpt-4"
15+
16+
17+
def test_request_model_name_prefers_string_model_over_sentry_fallback():
18+
agent = Agent(name="test_agent", model="gpt-4o-mini")
19+
agent._sentry_request_model = "fallback-model"
20+
21+
assert _get_request_model_name(agent) == "gpt-4o-mini"
22+
23+
24+
def test_request_model_name_falls_back_to_sentry_request_model():
25+
agent = Agent(name="test_agent", model=None)
26+
agent._sentry_request_model = "default-from-run-config"
27+
28+
assert _get_request_model_name(agent) == "default-from-run-config"
29+
30+
31+
def test_request_model_name_returns_none_without_model_or_fallback():
32+
agent = Agent(name="test_agent", model=None)
33+
34+
assert _get_request_model_name(agent) is None

0 commit comments

Comments
 (0)