Streaming
Stream response tokens in real-time for better user experience.Why Stream?
Without streaming, users wait for the entire response before seeing anything. With streaming, tokens appear as they’re generated — critical for interactive applications.Chat Completion Streaming
Text Completion Streaming
Chunk Format
Each streaming chunk contains a delta (incremental change):SSE Format (Raw API)
When using the API directly withstream: true, responses use Server-Sent Events:
Browser Example
React Hook Example
Finish Reasons
| Reason | Description |
|---|---|
stop | Natural completion or stop sequence hit |
length | Max tokens reached |
content_filter | Content was filtered |
Tips
- Always check for content: Some chunks may have empty content
- Handle the finish reason: Know why generation stopped
- Buffer if needed: For sentence-by-sentence display, buffer until punctuation
- Error handling: Wrap in try/catch for network errors mid-stream
