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
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
withcontext manager to ensure connections are cleaned up - Access
stream.textafter 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
- Runs — run creation, follow-ups, and polling
- Permissions — control tool access with approval modes
