Enabling streaming
Setstream: true in the request body. That’s the only change required:
Python streaming example
Theopenai SDK returns an iterator when stream=True. Iterate over the chunks and print each delta as it arrives:
flush=True argument ensures each token is written to stdout immediately rather than buffered — important for terminal output.
JavaScript / TypeScript streaming example
Theopenai npm SDK supports async iteration over streaming responses:
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 withdata: followed by a JSON object. The final event is the literal string data: [DONE].
A typical stream looks like this:
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: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: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.