Skip to main content

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 with stream: true, responses use Server-Sent Events:

Browser Example

React Hook Example

Finish Reasons

Tips

  1. Always check for content: Some chunks may have empty content
  2. Handle the finish reason: Know why generation stopped
  3. Buffer if needed: For sentence-by-sentence display, buffer until punctuation
  4. Error handling: Wrap in try/catch for network errors mid-stream