Repository navigation
fix(openai-agents): Report the resolved request model and real streamed response model - #7810
Closed
sentry-junior[bot] wants to merge 2 commits into
Closed
sentry-junior[bot] wants to merge 2 commits into
sentry-junior[bot] wants to merge 2 commits into
Conversation
…ed response model Chat spans took gen_ai.request.model from agent.model, ignoring RunConfig.model, which the runner gives precedence. Use the model instance the runner resolved instead. Streamed Chat Completions (and LiteLLM) responses are synthesized by the Agents SDK with model set to the requested name, e.g. an Azure deployment name, so it was reported as gen_ai.response.model. Relay prices spans by the response model, so these spans got no cost. Record the model reported by the provider chunks instead. Track the captured response model per model copy instead of on the shared Agent. Co-Authored-By: Matej Minar <matej.minar@sentry.io>
alexander-alderman-webb
requested changes
Oct 1, 2026
…Chat Completions paths
Member
|
we can close this, I just asked Junior to do some digging while in meetings, didn't expect it to open a PR |
Contributor
Codecov Results 📊✅ 129617 passed | ❌ 3 failed | ⏭️ 7232 skipped | Total: 136852 | Pass Rate: 94.71% | Execution Time: 442m 16s ❌ Failed Tests
|
| File | Patch % | Lines |
|---|---|---|
| sentry_sdk/integrations/openai_agents/utils.py | 85.71% | |
| sentry_sdk/integrations/openai_agents/patches/models.py | 100.00% |
Coverage diff
@@ Coverage Diff @@
## master #PR +/-##
==========================================
+ Coverage 90.34% 90.36% +0.02%
==========================================
Files 199 199 —
Lines 26524 26548 +24
Branches 9864 9862 -2
==========================================
+ Hits 23962 23989 +27
- Misses 2562 2559 -3
- Partials 1495 1494 -1Generated by Codecov Action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
A customer reported that model costs are missing for OpenAI Agents spans. Relay prices each span using
gen_ai.response.model, falling back togen_ai.request.model, plus the token counts. This PR fixes two cases where the chat span reports the wrong model:1. Streamed Chat Completions report the requested name as the response model. When
OpenAIChatCompletionsModel(orLitellmModel) streams, the Agents SDK builds the finalResponsewithmodel=self.model. That is the requested name, such as an Azure deployment name likeprod-gpt4o. We copied it intogen_ai.response.model, so Relay found no price and set no cost. The model that actually responded (e.g.gpt-4o-2024-08-06) only shows up on the provider chunks. We now wrap the chunk stream that_fetch_response(stream=True)returns, recordchunk.model, and prefer that value. The wrapper is a pass-through proxy: it delegates every attribute to the original stream.2.
RunConfig.modelwas ignored forgen_ai.request.modeland the span name.get_model()givesRunConfig.modelpriority overAgent.model, but the chat span readagent.model. A run withAgent(model="gpt-4")andRunConfig(model="gpt-4.1-mini")was reported aschat gpt-4. The chat span now uses the name of the model instance that the runner resolved in_get_model. That name also has provider prefixes likelitellm/already removed.Also: the response model captured from
_fetch_responseused to be stored on the sharedAgentobject (agent._sentry_response_model). It now lives in a closure on the model copy made for each turn, so concurrent runs that share oneAgentdon't read each other's state.Before → after, with openai-agents 0.22.3 and a mocked Azure client:
Not fixed here (an upstream default, not an SDK bug): for clients that aren't the official OpenAI endpoint (Azure, OpenAI-compatible proxies),
openai-agentsdoesn't sendstream_options.include_usagewhen streaming Chat Completions. The provider then returns no usage, so there are no tokens and no cost. Users needModelSettings(include_usage=True).Tests
test_ai_client_span_request_model_uses_run_config_modeltest_ai_client_span_response_model_from_streamed_chat_completions. Skipped on openai-agents < 0.7.0, because its Chat Completions streaming already fails against the pinned openai types on master.Both new tests fail on master and pass with this change. I ran the full
tests/integrations/openai_agentssuite locally on Python 3.12 against openai-agents 0.22.3, 0.14.8, 0.7.0 and 0.0.19, and all pass (on 0.0.19 the new streaming test is skipped).ruff check/ruff formatare clean, andmypyreports no errors insentry_sdk/integrations/openai_agents.via matej.
--
View Junior Session [Sentry]