Skip to main content
Server-Sent Events (SSE): Streaming uses SSE format with text/event-stream content type.

How Streaming Works

1

Enable Streaming

Set stream: true in your request to receive incremental responses.
2

Receive Chunks

Get response tokens as they’re generated, not waiting for completion.
3

Handle Events

Process data: events containing JSON chunks until [DONE] signal.

Implementation Examples

With Async Support

Stream Event Format

Delta Events

Each streaming chunk follows this format:

Stream Lifecycle

  1. Initial chunk - Contains role but no content:
  1. Content chunks - Incremental text:
  1. Final chunk - Includes finish_reason:
  1. Stream end signal:

Advanced Streaming Features

Function Calling in Streams

Stream function calls as they’re generated:

Progress Tracking

Error Handling in Streams

Streaming connections can fail mid-stream. Always implement proper error handling.

Performance Optimization

Buffering Strategy

Use Cases

Live Chat Interface

Real-time Translation

Best Practices

  1. Handle connection interruptions - Implement retry logic with exponential backoff
  2. Buffer for UI updates - Don’t update DOM for every chunk to avoid performance issues
  3. Show loading indicators - Display typing indicators or progress bars
  4. Implement timeouts - Set reasonable timeouts for streaming connections
  5. Clean up resources - Always close streams properly to avoid memory leaks

Next Steps