How Streaming Works
Set"stream": true in your request body. The API responds with a stream of newline-delimited events, each prefixed with data: . Each event contains a partial response chunk in JSON. When generation is complete, the stream sends a final data: [DONE] sentinel and closes.
A raw stream looks like this:
delta.content field contains the next piece of text. Concatenate them in order to reconstruct the full response.
Code Examples
Streaming Response Format
Each chunk follows this structure:[DONE] sets finish_reason to "stop", "length", or another terminal value — the same reasons as non-streaming responses. Use this to detect truncation.
End of Stream
The stream terminates with:Error Handling
Errors that occur before streaming begins are returned as standard HTTP error responses (4xx or 5xx) with a JSON body. Errors that occur mid-stream are delivered as a final SSE event with anerror field instead of choices.
Python
Streaming is supported for the
/v1/chat/completions and /v1/completions endpoints. The /v1/embeddings endpoint does not support streaming — embedding requests always return a complete response in a single JSON payload.