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
The current stream_async implementation calls _run_loop and uses threads with a queue to yield streamed data events from the Strands Agents event loop:
The problem is that this threading implementation doesn't work with Python features like thread local contextvars. Certain Python libraries (e.g. https://github.com/UKGovernmentBEIS/inspect_ai) use thread local context vars and require them to function correctly. The only reasonable solution for correctly supporting concurrency within a single thread is async.
This issue is to propose, discuss, and refine a true async implementation of the Agent.stream_async function and Strands Agent event loop.
Proposed Solution
No response
Use Case
Thread local contextvars
A more maintainable implementation of async agent requests
Allows for future enhancements that enable streamed tool results
Alternatives Solutions
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered:
Another use case is providing caller control over the lifecycle of an agent request. Right now all events are enqueued eagerly - meaning that if even when callers stop iterating, the agent continues until it hits a natural stopping point. In theory, an async iterator could/would be pull-based, allowing the agent to suspend if the caller stops iterating which should enable more advanced use cases or control.
Problem Statement
The
Agent
class provides thestream_async
function that returns anAsyncIterator
:sdk-python/src/strands/agent/agent.py
Line 356 in 77f5fa7
The current
stream_async
implementation calls_run_loop
and uses threads with a queue to yield streamed data events from the Strands Agents event loop:sdk-python/src/strands/agent/agent.py
Line 423 in 77f5fa7
sdk-python/src/strands/agent/agent.py
Lines 409 to 421 in 77f5fa7
The problem is that this threading implementation doesn't work with Python features like thread local contextvars. Certain Python libraries (e.g. https://github.com/UKGovernmentBEIS/inspect_ai) use thread local context vars and require them to function correctly. The only reasonable solution for correctly supporting concurrency within a single thread is async.
This issue is to propose, discuss, and refine a true async implementation of the
Agent.stream_async
function and Strands Agent event loop.Proposed Solution
No response
Use Case
Alternatives Solutions
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: