Skip to main content
Streaming lets you display AI responses word-by-word as they are generated, so users see output immediately instead of staring at a spinner for several seconds. Portrix normalises streaming across all 400+ connected models to OpenAI’s Server-Sent Events (SSE) format, which means your streaming code works identically regardless of which underlying provider serves the request.

Enabling streaming

Set stream: true in the request body. That’s the only change required:

Python streaming example

The openai SDK returns an iterator when stream=True. Iterate over the chunks and print each delta as it arrives:
The flush=True argument ensures each token is written to stdout immediately rather than buffered — important for terminal output.

JavaScript / TypeScript streaming example

The openai npm SDK supports async iteration over streaming responses:
In a browser or Node.js server context, write each delta to your UI component or response stream instead of process.stdout.

cURL streaming example

Use the --no-buffer flag to disable curl’s output buffering and see tokens as they arrive in your terminal:

Handling stream events

Each event in the SSE stream is a line starting with data: followed by a JSON object. The final event is the literal string data: [DONE]. A typical stream looks like this:
Key fields in each chunk:

Error handling

Errors can surface at two different points in a streaming request: Before the stream starts — if the request is malformed, authentication fails, or the model is unavailable, Portrix returns a standard non-200 HTTP response with a JSON error body. Handle this by checking the HTTP status code before iterating. Mid-stream — if the provider encounters an error after generation has begun, an error event is injected into the stream:
A robust streaming handler should check for an error key in each chunk:

Streaming with fallbacks

Fallbacks work transparently with streaming. If the primary model fails before the stream starts, Portrix switches to the next model in your fallback chain and begins streaming from there — your code sees a seamless stream with no error:
See the Fallbacks & Load Balancing guide for full configuration options.
Streaming is supported for all chat completion models on Portrix. Embeddings endpoints (/v1/embeddings) do not support streaming, and a small number of legacy completion models may return the full response in a single chunk even when stream: true is set.