-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RunResultStreaming.stream_events() raises ValueError when reset_current=True in async environments #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
bug
Something isn't working
Comments
error message: During handling of the above exception, another exception occurred:
+ Exception Group Traceback (most recent call last):
| File "/home/gpower/.venv/lib/python3.12/site-packages/uvicorn/protocols/http/httptools_impl.py", line 409, in run_asgi
| result = await app( # type: ignore[func-returns-value]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "/home/gpower/.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
| return await self.app(scope, receive, send)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "/home/gpower/.venv/lib/python3.12/site-packages/fastapi/applications.py", line 1054, in __call__
| await super().__call__(scope, receive, send)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/applications.py", line 112, in __call__
| await self.middleware_stack(scope, receive, send)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 187, in __call__
| raise exc
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 165, in __call__
| await self.app(scope, receive, _send)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/middleware/cors.py", line 93, in __call__
| await self.simple_response(scope, receive, send, request_headers=headers)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/middleware/cors.py", line 144, in simple_response
| await self.app(scope, receive, send)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
| await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
| raise exc
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
| await app(scope, receive, sender)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/routing.py", line 715, in __call__
| await self.middleware_stack(scope, receive, send)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/routing.py", line 735, in app
| await route.handle(scope, receive, send)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/routing.py", line 288, in handle
| await self.app(scope, receive, send)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/routing.py", line 76, in app
| await wrap_app_handling_exceptions(app, request)(scope, receive, send)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
| raise exc
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
| await app(scope, receive, sender)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/routing.py", line 74, in app
| await response(scope, receive, send)
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/responses.py", line 261, in __call__
| async with anyio.create_task_group() as task_group:
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| File "/home/gpower/.venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 767, in __aexit__
| raise BaseExceptionGroup(
| ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/responses.py", line 264, in wrap
| await func()
| File "/home/gpower/.venv/lib/python3.12/site-packages/starlette/responses.py", line 245, in stream_response
| async for chunk in self.body_iterator:
| File "/home/gpower/gpower/api/routes/doc_agents_server.py", line 42, in event_stream
| async for chunk in result:
| File "/home/gpower/.venv/lib/python3.12/site-packages/agents/result.py", line 181, in stream_events
| self._trace.finish(reset_current=True)
| File "/home/gpower/.venv/lib/python3.12/site-packages/agents/tracing/traces.py", line 173, in finish
| Scope.reset_current_trace(self._prev_context_token)
| File "/home/gpower/.venv/lib/python3.12/site-packages/agents/tracing/scope.py", line 45, in reset_current_trace
| _current_trace.reset(token)
| ValueError: <Token var=<ContextVar name='current_trace' default=None at 0x7ff7b810c770> at 0x7ff76bcac0c0> was created in a different Context

|
Oh sry, I mistakenly reopened this issue 😂. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please read this first
Describe the bug
When using the
RunResultStreaming.stream_events()
method within a streaming API endpoint in frameworks like FastAPI (or any ASGI server), the following exception is raised at the end of the stream:This error originates from
self._trace.finish(reset_current=True)
being executed across async task boundaries, which violates howContextVar
tokens must be reset in the same context they were set.Debug information
v0.0.3
3.12.2
FastAPI + Uvicorn
My codes:
Repro steps
Use the following minimal example with FastAPI:
The text was updated successfully, but these errors were encountered: