You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
'Span' object has no attribute '_context_manager_state' #4718
{
"error": "'Span' object has no attribute '_context_manager_state'",
"tool_name": "<agentname>_agent"
}
Expected Result
This error not to happen
That most (if not all) parts of the Sentry SDK are wrapped in try catch blocks, so that if for some reason like now a Span cannot be closed, it doesn't interfere with the normal flow
Actual Result
The agent crashes, and instead of the result that was generated by the nested agent being available to the main agent, the main agent only sees the error message and cannot continue.
Current working theory: scope bleed between the different agent runners. We're missing proper scope isolation. Working on a fix here, should be ready by tomorrow latest.
The fix is basically akin to changing run_agent to
async def run_agent() -> None:
+ with sentry_sdk.isolation_scope():
runner = await Runner.run(
starting_agent=main_agent,
input="How are you?",
)
print(runner.final_output)
@sentrivana thanks for the update, we've currently had to disable tracing in prod unfortunately, and ideally don't want to depend on a non-release version of the sentry sdk. Can you tell me how it aligns with your release schedule?
).
Ran a very quick test locally and that indeed seemed to resolve the issue. Ofc I don't know too much about the sdk, so not sure if it actually resolves the underlying bug or if it just makes a lot of other stuff throw up, but sharing in case it helps :)
not sure if it actually resolves the underlying bug or if it just makes a lot of other stuff throw up, but sharing in case it helps :)
Thanks, but in this case looks like it's the latter. If you look at the transactions produced with the fix, they're all mixed up, so there's still scope bleed going on.
@sentrivana I thought it was resolved, but it seems to not have been fully resolved unfortunately...
(unfortunately I can't re-open the issue, so if you could (or create a new one, whichever fits your process better) that'd be great)
From our traces, I can't see anything obvious unfortunately, adding a screenshot in case it helps from one of the traces that has errored. This was a nested agent running (agent.as_tool(), which calls Runner.run, nested inside a Runner.run_streamed)
So unfortunately we're again reverting the integration from our prod, and I'll again vote to at least try the calls so sth like this doesn't negatively interfere with our agents that are running...
Hey @Afstkla, yesterday we fixed yet another bug that was presenting itself with the same error in b1a8b63. We'll cut a release with this today/tomorrow -- maybe with the try..except on top for good measure. I'll ping you when it's out.
Hey @Afstkla, yesterday we fixed yet another bug that was presenting itself with the same error in b1a8b63. We'll cut a release with this today/tomorrow -- maybe with the try..except on top for good measure. I'll ping you when it's out.
Thx for the update. I cannot have this same issue break production again, so I’m realistically not going to re-enable it on prod until there’s error handling around it (and I hope it’s also something you see as well, that enabling tracing in any way should never be responsible for breaking our main flow in prod…).
So please keep me posted, and sharing the above just so you know where we’re coming from :)
Hey @Afstkla, of course, it goes without saying that the SDK should never break a user's app. That being said, the OpenAI agents integration is still in beta so there might be (and apparently are) things that need ironing out. We're on them with high prio -- also thanks to you raising them and following up. 🙏🏻
Re: release, v2.36.0 of the SDK is out. It includes the fix #4736 I mentioned above as well as some error silencing around span/profiler restoration (#4698, #4719). Do you happen to have a non-prod environment where you could give this a spin?
Hey @Afstkla, of course, it goes without saying that the SDK should never break a user's app. That being said, the OpenAI agents integration is still in beta so there might be (and apparently are) things that need ironing out. We're on them with high prio -- also thanks to you raising them and following up. 🙏🏻
Re: release, v2.36.0 of the SDK is out. It includes the fix #4736 I mentioned above as well as some error silencing around span/profiler restoration (#4698, #4719). Do you happen to have a non-prod environment where you could give this a spin?
Thx for the response! We'll test it out (tho not in prod) but as it seems mostly a multithreading issue, it might be quite difficult to reproduce in an env where we have not the traffic of prod.. Will keep you posted when we test & encounter anything
Activity
Afstkla commentedon Aug 25, 2025
@sentrivana
Played around a bit, and this seems to be quite reliable (100% so far) in repro-ing:
From my tests:
asyncio.gather) Tasksgpt-5andgpt-4o-mini, so seems to be unrelated to reasoning vs non-reasoning model(also updated the original Issue so people don't have to scroll :) )
sentrivana commentedon Aug 25, 2025
Awesome, thanks for the repro @Afstkla! We're on it.
sentrivana commentedon Aug 25, 2025
Current working theory: scope bleed between the different agent runners. We're missing proper scope isolation. Working on a fix here, should be ready by tomorrow latest.
The fix is basically akin to changing
run_agenttoasync def run_agent() -> None: + with sentry_sdk.isolation_scope(): runner = await Runner.run( starting_agent=main_agent, input="How are you?", ) print(runner.final_output)Afstkla commentedon Aug 25, 2025
@sentrivana thanks for the update, we've currently had to disable tracing in prod unfortunately, and ideally don't want to depend on a non-release version of the sentry sdk. Can you tell me how it aligns with your release schedule?
sentrivana commentedon Aug 25, 2025
Unless anything happens there should be a release with this tomorrow.
Afstkla commentedon Aug 25, 2025
@sentrivana not sure if it helps btw, but I had Claude Code go on an adventure, and it proposed changing
to
In runner.py (
sentry-python/sentry_sdk/integrations/openai_agents/patches/runner.py
Line 38 in 28d0ddd
sentry-python/sentry_sdk/integrations/openai_agents/spans/invoke_agent.py
Line 34 in 28d0ddd
Ran a very quick test locally and that indeed seemed to resolve the issue. Ofc I don't know too much about the sdk, so not sure if it actually resolves the underlying bug or if it just makes a lot of other stuff throw up, but sharing in case it helps :)
fix(openai-agents): Isolate agent run (#4720)
sentrivana commentedon Aug 26, 2025
Thanks, but in this case looks like it's the latter. If you look at the transactions produced with the fix, they're all mixed up, so there's still scope bleed going on.
I've merged the PR, will cut a release shortly.
sentrivana commentedon Aug 26, 2025
@Afstkla Out now in 2.35.1. Let us know if the fix works for you :)
Afstkla commentedon Sep 2, 2025
@sentrivana I thought it was resolved, but it seems to not have been fully resolved unfortunately...
(unfortunately I can't re-open the issue, so if you could (or create a new one, whichever fits your process better) that'd be great)
From our traces, I can't see anything obvious unfortunately, adding a screenshot in case it helps from one of the traces that has errored. This was a nested agent running (
agent.as_tool(), which callsRunner.run, nested inside aRunner.run_streamed)So unfortunately we're again reverting the integration from our prod, and I'll again vote to at least
trythe calls so sth like this doesn't negatively interfere with our agents that are running...Tagging @seynadio for vis as well.
sentrivana commentedon Sep 3, 2025
Hey @Afstkla, yesterday we fixed yet another bug that was presenting itself with the same error in b1a8b63. We'll cut a release with this today/tomorrow -- maybe with the
try..excepton top for good measure. I'll ping you when it's out.Wrap span restoration in `__exit__` in `capture_internal_exceptions` (#…
Afstkla commentedon Sep 4, 2025
Thx for the update. I cannot have this same issue break production again, so I’m realistically not going to re-enable it on prod until there’s error handling around it (and I hope it’s also something you see as well, that enabling tracing in any way should never be responsible for breaking our main flow in prod…).
So please keep me posted, and sharing the above just so you know where we’re coming from :)
sentrivana commentedon Sep 4, 2025
Hey @Afstkla, of course, it goes without saying that the SDK should never break a user's app. That being said, the OpenAI agents integration is still in beta so there might be (and apparently are) things that need ironing out. We're on them with high prio -- also thanks to you raising them and following up. 🙏🏻
Re: release, v2.36.0 of the SDK is out. It includes the fix #4736 I mentioned above as well as some error silencing around span/profiler restoration (#4698, #4719). Do you happen to have a non-prod environment where you could give this a spin?
Afstkla commentedon Sep 4, 2025
Thx for the response! We'll test it out (tho not in prod) but as it seems mostly a multithreading issue, it might be quite difficult to reproduce in an env where we have not the traffic of prod.. Will keep you posted when we test & encounter anything
Afstkla commentedon Sep 10, 2025
@sentrivana haven't seen any occurrences of this happening anymore, so deployed to prod and so far so good. Thanks for the support!
antonpirker commentedon Sep 11, 2025
This issue was fixed by the patch mentioned above. I will close this issue.