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. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Aug 25, 2025
  2. Afstkla commented on Aug 25, 2025

    @Afstkla
    Author

    (probably duplicate of #4698, sorry, just found that one)

  3. sentrivana commented on Aug 25, 2025

    @sentrivana
    Contributor

    Hey @Afstkla, thanks for reporting.

    This does look similar to what I wanted to patch with #4698, but it's not the same since in your case, it's the Span complaining about the missing attribute, and in the PR it was the profiler.

    All in all though it looks like there's a more fundamental issue with tracing (and profiling, by extension) in the OpenAIAgentsIntegration. But at the very least we should, of course, not error out like this. I'll see if I can find the issue; otherwise I'll at least patch our code so that we don't throw an exception.

    An example to reproduce (even if not consistently) would be super helpful -- in my tests I haven't been able to trigger this so far.

  4. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Aug 25, 2025
  5. self-assigned this
    on Aug 25, 2025
  6. 35 remaining items

  7. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Sep 4, 2025
  8. 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

  9. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Sep 4, 2025
  10. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Sep 8, 2025
  11. moved this to Waiting for: Community in GitHub Issues with 👀 3on Sep 8, 2025
  12. 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!

  13. moved this from Waiting for: Community to Waiting for: Product Owner in GitHub Issues with 👀 3on Sep 10, 2025
  14. 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