Enabling Streaming
To enable streaming, setstream: true in your request:
Stream Event Types
The streaming API uses Server-Sent Events (SSE) format. Each event has atype field indicating the event type:
Event Types:
response.created: Initial response creation eventresponse.output_item.added: New output item addedresponse.output_text.delta: Text delta (incremental text)response.output_item.done: Output item completedresponse.completed: Response generation completed
Processing Stream Events
Here’s a more complete example that handles all event types:Heartbeat Messages
The streaming API sends heartbeat messages (:ANANNAS PROCESSING) to keep the connection alive. These should be ignored when processing events.
Error Handling in Streams
Errors in streaming responses are sent as events:Best Practices
- Handle heartbeats: Ignore
:ANANNAS PROCESSINGmessages - Check for [DONE]: Stop processing when you receive
[DONE] - Error handling: Always check for error events in the stream
- Buffer management: For long streams, consider buffering and processing in chunks
- Connection management: Handle connection drops gracefully and implement retry logic
Was this page helpful?