Study 70 files ยท 12 subfolders
Copy to Workspace 16_STREAMING Shared from "Study" on Inkdown
Streaming - Comprehensive Deep Dive
Overview
Streaming in the OpenAI Agents SDK enables real-time delivery of agent execution events. Think of Streaming as "live updates" or "progressive rendering" - instead of waiting for the entire agent run to complete before seeing results, you receive events as they happen. This is essential for responsive user interfaces, real-time feedback, and long-running operations.
Core Concepts
What is Streaming?
Streaming is the process of delivering events incrementally as they occur during agent execution:
Real-time updates - Receive events as they happen
Progressive output - Show results as they're generated
Event-driven - React to specific events
Non-blocking - Don't wait for completion
Why Streaming Matters
Responsiveness - Provide immediate feedback to users
basic-ques
User Experience - Show progress during long operations
Interactivity - Allow users to see what's happening
Efficiency - Start processing partial results early
Transparency - Show agent reasoning and tool usage
Real-time Applications - Enable live chat interfaces
Streaming Methods
run_streamed() The main streaming method:
Same as run() - agent, input, context, etc.
Returns an async iterator of stream events
Stream Events
RunItemStreamEvent - A new run item was created
AgentUpdatedStreamEvent - The current agent changed
RawResponsesStreamEvent - A raw model stream event
RunItemStreamEvent
Event Structure
Handling RunItem Events
Message Output Stream message content as it arrives:
Tool Call Streaming
Tool Output Streaming
AgentUpdatedStreamEvent
Event Structure
Handling Agent Updates
Handoff Detection Detect handoffs via agent updates:
RawResponsesStreamEvent
Event Structure
Handling Raw Events
Delta Streaming
Streaming Patterns
1. Text Streaming Stream text output character by character:
2. Progress Indicator Show progress during execution:
3. Structured Output Streaming Stream structured output as it's built:
4. Multi-Agent Streaming
5. Tool Execution Visualization Visualize tool execution:
Streaming and WebSockets
WebSocket Streaming Stream to WebSocket clients:
Server-Sent Events Stream via Server-Sent Events:
Streaming and UI
React Component Stream to React component:
Terminal UI Stream to terminal with rich output:
Streaming Performance
Event Batching Batch events for efficiency:
Event Filtering Filter events to reduce overhead:
Backpressure Handling
Streaming and State
Streaming with State Stream while maintaining state:
Streaming to State Accumulate streaming results:
Streaming Errors
Handling Streaming Errors Handle errors during streaming:
Error Events Errors may be sent as events:
Streaming and Cancellation
Cancellation Cancel streaming operations:
Graceful Cancellation
Streaming Best Practices
1. Use Async Iterators Use async iterators properly:
2. Handle All Event Types
3. Flush Output Flush output for real-time display:
4. Handle Backpressure
5. Clean Up Resources Clean up streaming resources:
Common Streaming Patterns
1. Chat Interface Streaming chat interface:
2. Progress Bar Show progress during tool execution:
3. Multi-Stream Aggregation Aggregate multiple streams:
4. Conditional Streaming Stream based on conditions:
5. Streaming with Callbacks Use callbacks for event handling:
Streaming and Testing
Testing Streaming
Mock Streaming Mock streaming for tests:
Summary Streaming enables real-time agent execution. Key takeaways:
run_streamed() is the main streaming method
RunItemStreamEvent - new run item created
AgentUpdatedStreamEvent - agent changed
RawResponsesStreamEvent - raw model event
Text streaming - stream output character by character
Progress indicators - show execution progress
Structured output - stream as it's built
Multi-agent - track multiple agents
Tool visualization - show tool execution
WebSocket - stream to WebSocket clients
SSE - stream via Server-Sent Events
React - stream to React components
Terminal UI - stream to rich terminal
Batching - batch events for efficiency
Filtering - filter events to reduce overhead
Backpressure - handle slow consumers
State - maintain state during streaming
Errors - handle streaming errors
Cancellation - cancel streaming operations
Best practices - async iterators, flush, cleanup
Streaming is essential for building responsive, real-time agent interfaces.