Streaming

Runs stream Server-Sent Events (SSE) by default. The SDK wraps these in a RunStream iterator so you can process events as they arrive.

Quick Patterns

Just get the final output:

Python

Print text as it arrives:

Python

Don't need streaming? Use poll:

Python

Context Manager

Use with to ensure the HTTP connection is closed cleanly:

Python

stream.text (or stream.output) returns all text deltas joined together.

Event Types

EventDescriptionKey fields
text-deltaIncremental text chunkevent.delta
tool-call-startTeammate starts using a toolevent.tool_name
tool-call-deltaStreaming tool argumentsevent.delta
tool-result-endTool finished with resultevent.result
errorError during executionevent.error
doneStream completeevent.stop_reason (end_turn, max_tokens, stop_sequence)
run_metricsExecution statsevent.execution_time_ms, event.input_tokens_used

Filter by Event Type

Python

Streaming Replies

Follow-up messages also stream by default:

Python

Non-Streaming Alternative

Pass stream=False to get a Run object directly. The run executes in the background — poll for completion:

Best Practices

  • Use stream=False + poll() for simple scripts and background jobs
  • Use streaming for real-time UX (chat interfaces, progress indicators)
  • Use the with context manager to ensure connections are cleaned up
  • Access stream.text after iteration to get the full accumulated output

SSE Frame Format

Response is a stream of SSE frames:

data: {"type": "text-delta", "delta": "Here are "} data: {"type": "text-delta", "delta": "the open tickets..."} data: {"type": "tool-call-start", "toolName": "gmail_search", "toolCallId": "tc_1"} data: {"type": "tool-result-end", "toolCallId": "tc_1", "result": "..."} data: {"type": "done", "stop_reason": "end_turn"}

What's Next