Skip to main content

Overview

Streaming allows you to receive model responses incrementally as they’re generated, rather than waiting for the complete response. This is implemented using Server-Sent Events (SSE) and is useful for real-time user interfaces.

Enabling Streaming

Set stream: true in your chat completions request:

Stream Response Format

Initial Chunk

The first chunk contains the role:

Content Chunks

Subsequent chunks contain content deltas:

Final Chunk

The last chunk has an empty delta and a finish_reason:

Stream Termination

Response Fields

string
Always "chat.completion.chunk" for streaming responses
object
Incremental content update:
  • role: Present in first chunk only
  • content: Text content delta (not cumulative)
string | null
Null during generation, then one of:
  • "stop": Natural completion
  • "length": Reached max_length

Python Client Examples

Basic Streaming

With OpenAI SDK

Collecting Full Response

JavaScript Example

Limitations

Function Calling Not Supported

Streaming does not support function calling:
Error response:

Best Practices

Buffer Management

Handle partial lines in streams:

Error Handling

Stop Word Handling

The streaming implementation includes a delay buffer to properly handle stop words. The last few tokens may be held back temporarily to check for stop sequences before being yielded.