Skip to main content

Overview

Streaming allows you to receive chat completion responses incrementally as they are generated, rather than waiting for the complete response.

Endpoint

Set stream: true in the request body to enable streaming.

Request

Streaming Parameters

boolean
required
Set to true to enable streaming
object
Additional streaming options
All other parameters are identical to the Chat Completions endpoint.

Response Format

Streamed responses are sent as Server-Sent Events (SSE):

Chunk Object

string
Unique identifier for the completion
string
Object type, always chat.completion.chunk
integer
Unix timestamp of creation
string
The model used
array
Array of streaming choices
integer
Choice index
object
Incremental message content
string
Role (only in first chunk)
string
Incremental content
array
Incremental tool calls
string
Reason for completion (only in last chunk)

Examples

Basic Streaming Request

Python Streaming Example

JavaScript Streaming Example

OpenAI SDK Streaming

Streaming with Usage Information

Streaming with Function Calling

Error Handling

Handle errors during streaming:

Best Practices

  1. Buffer Management: Process chunks as they arrive to provide real-time feedback
  2. Error Recovery: Implement proper error handling for connection issues
  3. Token Counting: Use stream_options.include_usage to track token usage
  4. Connection Timeout: Set appropriate timeouts for long-running streams
  5. UI Updates: Update your UI incrementally for better user experience