stream: true on a chat completion to receive the response incrementally as server-sent events (SSE). Instead of waiting for the full completion, you get a series of small ChatCompletionChunk objects as the model generates tokens, so you can render output as it arrives.
Streaming works with any model on the gateway, and with the OpenAI SDK you already use. Point the SDK at https://api.infyrence.com/v1 and pass stream=True.
Streaming uses the same
POST /v1/chat/completions endpoint as a normal request. The only difference is the stream field and the response format.How it works
When you setstream: true, the gateway responds with Content-Type: text/event-stream and writes each chunk as a single SSE line:
data: line contains one JSON ChatCompletionChunk. After the final chunk, the gateway writes a terminator line and closes the connection:
data: [DONE] marker is not JSON. Use it to know the stream is finished. It is always sent, including after an error, so a client that blocks on the terminator closes cleanly.
The gateway also sets Cache-Control: no-cache and Connection: keep-alive on the streamed response.
Request
Send a normal chat completion request withstream set to true.
boolean
default:"false"
If
true, the gateway streams the response as server-sent events. If omitted or false, the gateway returns a single ChatCompletionResponse JSON object.model, messages, temperature, max_tokens, and the rest) work exactly as they do for a non-streaming request. See the chat completions reference for the full list.
Chunk shape
Each event’sdata: payload is a ChatCompletionChunk object.
string
An id for the completion. The same id is repeated across every chunk in the stream.
string
Always
chat.completion.chunk.integer
Unix timestamp (seconds) of when the completion was created.
string
The model that produced the chunk.
array
A list of chunk choices. Each has an
index, a delta, and a finish_reason.object
Token usage for the completion (
prompt_tokens, completion_tokens, total_tokens). Present on the usage-bearing chunk near the end of the stream.choices[0].delta.content across all chunks in order.
Example chunks
The first chunk opens the message with a role:finish_reason:
Examples
The OpenAI SDKs parse the SSE stream and thedata: [DONE] terminator for you, so you iterate over chunk objects directly.
Parsing the stream yourself
If you consume the raw HTTP response instead of an SDK, follow the SSE format:1
Read the response line by line
The body is a
text/event-stream. Each event is a line that starts with data: .2
Check for the terminator
If the line is
data: [DONE], the stream is complete. Stop reading. Do not try to parse [DONE] as JSON.3
Parse each data line as JSON
For every other
data: line, parse the remainder as a ChatCompletionChunk and read choices[0].delta.4
Accumulate the content
Append each
delta.content to build the full message, and stop once you see a non-null finish_reason followed by data: [DONE].Errors during a stream
The HTTP status is200 as soon as the stream opens, so an error that happens mid-stream cannot change the status code. Instead, the gateway emits a final error event and then the [DONE] terminator:
error field before reading choices. Every error body includes a request id, also returned in the X-Request-Id header, which you can quote when contacting support.
Related
Chat completions
The full request and response schema for
/v1/chat/completions.Models
List every model id you can stream from.
Errors
Error types, codes, and status codes.
Quickstart
Point your OpenAI SDK at the gateway in one step.