Skip to content

Conversation

@constantinius
Copy link
Contributor

Description

When passing in an explicit model instance instead of a string the model was patched multiple times event if it was already patched before. This PR prohibits that.

Issues

Contributes to: https://linear.app/getsentry/issue/TET-1511/openai-agents-double-span-reporting

@constantinius constantinius requested a review from a team as a code owner December 3, 2025 17:06
@linear
Copy link

linear bot commented Dec 3, 2025


# check if we have already patched this model
if getattr(model, "_sentry_wrapped_get_model", False):
return model
Copy link

Choose a reason for hiding this comment

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

Bug: Stale agent reference captured in closure when model reused

When a model instance is shared across multiple agents, the early return on line 37-38 prevents re-patching but causes wrapped_fetch_response and wrapped_get_response to use a stale agent reference from their closures. The closures capture the agent from the first call to wrapped_get_model, so subsequent calls with different agents will incorrectly write/read _sentry_raw_response_model on the wrong agent and create spans associated with the wrong agent. The wrapper functions need access to the current agent being used, not the one captured at patch time.

Additional Locations (2)

Fix in Cursor Fix in Web

Copy link
Contributor

Choose a reason for hiding this comment

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

this looks like a valid concern @constantinius

@codecov
Copy link

codecov bot commented Dec 3, 2025

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
29714 1 29713 2319
View the top 1 failed test(s) by shortest run time
tests.profiler.test_continuous_profiler::test_continuous_profiler_auto_start_and_stop_sampled[experiment-thread]
Stack Traces | 1.07s run time
tests/profiler/test_continuous_profiler.py:506: in test_continuous_profiler_auto_start_and_stop_sampled
    assert get_profiler_id() is None, "profiler should not be running"
E   AssertionError: profiler should not be running
E   assert '92f18ef36e9443faae5a4956573cc131' is None
E    +  where '92f18ef36e9443faae5a4956573cc131' = get_profiler_id()

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants