Skip to content

'Span' object has no attribute '_context_manager_state' #4718

Closed

Description

@Afstkla

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.35.0

Steps to Reproduce

Run

import asyncio

import sentry_sdk
from agents import Agent, Runner
from sentry_sdk.integrations.openai_agents import OpenAIAgentsIntegration

sentry_sdk.init(
    dsn="...",
    send_default_pii=True,
    traces_sample_rate=1.0,
    integrations=[
        OpenAIAgentsIntegration(),
    ],
)


main_agent = Agent(
    name="main_agent",
    model="gpt-5",
)


async def run_agent() -> None:
    runner = await Runner.run(
        starting_agent=main_agent,
        input="How are you?",
    )
    print(runner.final_output)


async def main() -> None:
    await asyncio.gather(*[run_agent() for _ in range(2)])


if __name__ == "__main__":
    asyncio.run(main())

Then, the agent that finishes 2nd will fail with:

{
  "error": "'Span' object has no attribute '_context_manager_state'",
  "tool_name": "<agentname>_agent"
}

Expected Result

  1. This error not to happen
  2. 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.

Activity

  1. Afstkla commented on Aug 25, 2025

    @Afstkla
    Author

    @sentrivana

    Played around a bit, and this seems to be quite reliable (100% so far) in repro-ing:

    import asyncio
    
    import sentry_sdk
    from agents import Agent, Runner
    from sentry_sdk.integrations.openai_agents import OpenAIAgentsIntegration
    
    sentry_sdk.init(
        dsn="...",
        send_default_pii=True,
        traces_sample_rate=1.0,
        integrations=[
            OpenAIAgentsIntegration(),
        ],
    )
    
    
    main_agent = Agent(
        name="main_agent",
        model="gpt-5",
    )
    
    
    async def run_agent() -> None:
        runner = await Runner.run(
            starting_agent=main_agent,
            input="How are you?",
        )
        print(runner.final_output)
    
    
    async def main() -> None:
        await asyncio.gather(*[run_agent() for _ in range(2)])
    
    
    if __name__ == "__main__":
        asyncio.run(main())

    From my tests:

    • Couldn't repro with just 1 call, needed to be 2 simultaneous (asyncio.gather) Tasks
    • Doesn't seem to be related to nested agents after all, was able to reproduce without any nested agent and without any function calling
    • Seems to fail irrespective of the model, tested both with gpt-5 and gpt-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 :) )

  2. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Aug 25, 2025
  3. sentrivana commented on Aug 25, 2025

    @sentrivana
    Collaborator

    Awesome, thanks for the repro @Afstkla! We're on it.

  4. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Aug 25, 2025
  5. sentrivana commented on Aug 25, 2025

    @sentrivana
    Collaborator

    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)
  6. linked a pull request that will close this issuefix(openai-agents): Isolate agent run #4720on Aug 25, 2025
  7. Afstkla commented on Aug 25, 2025

    @Afstkla
    Author

    @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?

  8. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Aug 25, 2025
  9. sentrivana commented on Aug 25, 2025

    @sentrivana
    Collaborator

    Unless anything happens there should be a release with this tomorrow.

  10. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Aug 25, 2025
  11. Afstkla commented on Aug 25, 2025

    @Afstkla
    Author

    @sentrivana not sure if it helps btw, but I had Claude Code go on an adventure, and it proposed changing

    current_span.__exit__(None, None, None)

    to

    current_span.finish()

    In runner.py (

    current_span.__exit__(None, None, None)
    ) and `invoke_agent.py (
    current_span.__exit__(None, None, None)
    ).
    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 :)

  12. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Aug 25, 2025
  13. sentrivana commented on Aug 26, 2025

    @sentrivana
    Collaborator

    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.

    I've merged the PR, will cut a release shortly.

  14. sentrivana commented on Aug 26, 2025

    @sentrivana
    Collaborator

    @Afstkla Out now in 2.35.1. Let us know if the fix works for you :)

  15. Afstkla commented on Sep 2, 2025

    @Afstkla
    Author

    @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)

    Image

    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)

    Image

    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...

    Tagging @seynadio for vis as well.

  16. sentrivana commented on Sep 3, 2025

    @sentrivana
    Collaborator

    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.

  17. Afstkla commented on Sep 4, 2025

    @Afstkla
    Author

    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 :)

  18. sentrivana commented on Sep 4, 2025

    @sentrivana
    Collaborator

    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?

  19. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Sep 4, 2025
  20. Afstkla commented on Sep 4, 2025

    @Afstkla
    Author

    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

  21. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Sep 4, 2025
  22. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Sep 8, 2025
  23. moved this to Waiting for: Community in GitHub Issues with 👀 3on Sep 8, 2025
  24. Afstkla commented on Sep 10, 2025

    @Afstkla
    Author

    @sentrivana haven't seen any occurrences of this happening anymore, so deployed to prod and so far so good. Thanks for the support!

  25. moved this from Waiting for: Community to Waiting for: Product Owner in GitHub Issues with 👀 3on Sep 10, 2025
  26. antonpirker commented on Sep 11, 2025

    @antonpirker
    Contributor

    This issue was fixed by the patch mentioned above. I will close this issue.

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

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions