Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions docs/user-guide/concepts/streaming/async-iterators.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Async Iterators for Streaming

Strands Agents SDK provides support for asynchronous iterators through the `stream_async` method, enabling real-time streaming of agent responses in asynchronous environments like web servers, APIs, and other async applications.
Strands Agents SDK provides support for asynchronous iterators through the [`stream_async`](../../../api-reference/agent.md#strands.agent.agent.Agent.stream_async) method, enabling real-time streaming of agent responses in asynchronous environments like web servers, APIs, and other async applications.

> **Note**: If you want to use callbacks instead of async iterators, take a look at the [callback handlers](./callback-handlers.md) documentation. Async iterators are ideal for asynchronous frameworks like FastAPI, aiohttp, or Django Channels. For these environments, Strands Agents SDK offers the `stream_async` method which returns an asynchronous iterator.

Expand All @@ -27,14 +27,15 @@ async def process_streaming_response():
asyncio.run(process_streaming_response())
```

> Note, Strands also offers an [`invoke_async`](../../../api-reference/agent.md#strands.agent.agent.Agent.invoke_async) method for non-iterative async invocations.

## Event Types

The async iterator yields the same event types as [callback handlers](callback-handlers.md#callback-handler-events), including:

### Text Generation Events

- `data`: Text chunk from the model's output
- `complete`: Boolean indicating if this is the final chunk
- `delta`: Raw delta content from the model

### Tool Events
Expand All @@ -53,6 +54,7 @@ The async iterator yields the same event types as [callback handlers](callback-h
- `event`: Raw event from the model stream
- `force_stop`: True if the event loop was forced to stop
- `force_stop_reason`: Reason for forced stop
- `result`: The final [`AgentResult`](../../../api-reference/agent.md#strands.agent.agent_result.AgentResult)

### Reasoning Events

Expand Down Expand Up @@ -83,17 +85,17 @@ async def stream_response(request: PromptRequest):
tools=[calculator, http_request],
callback_handler=None
)

try:
async for event in agent.stream_async(request.prompt):
if "data" in event:
# Only stream text chunks to the client
yield event["data"]
except Exception as e:
yield f"Error: {str(e)}"

return StreamingResponse(
generate(),
media_type="text/plain"
)
```
```
2 changes: 1 addition & 1 deletion docs/user-guide/concepts/streaming/callback-handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ Callback handlers receive the same event types as [async iterators](./async-iter
### Text Generation Events

- `data`: Text chunk from the model's output
- `complete`: Boolean indicating if this is the final chunk
- `delta`: Raw delta content from the model

### Tool Events
Expand All @@ -61,6 +60,7 @@ Callback handlers receive the same event types as [async iterators](./async-iter
- `event`: Raw event from the model stream
- `force_stop`: True if the event loop was forced to stop
- `force_stop_reason`: Reason for forced stop
- `result`: The final [`AgentResult`](../../../api-reference/agent.md#strands.agent.agent_result.AgentResult)

### Reasoning Events

Expand Down