Skip to content

Streaming responses

Set stream to true to receive content as the model generates it instead of waiting for the complete answer.

Use --no-buffer so events are printed as they arrive:

Terminal window
curl --no-buffer --silent --show-error \
'https://omrouter.com/v1/chat/completions' \
--header "Authorization: Bearer $OMROUTER_API_KEY" \
--header 'Content-Type: application/json' \
--data "{
\"model\": \"$OMROUTER_MODEL_ID\",
\"stream\": true,
\"messages\": [
{\"role\": \"user\", \"content\": \"Count from one to five.\"}
]
}"

Chat Completions streams contain data: events. A normal stream ends with the protocol’s terminal event, commonly data: [DONE].

import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["OMROUTER_API_KEY"],
base_url="https://omrouter.com/v1",
)
stream = client.chat.completions.create(
model=os.environ["OMROUTER_MODEL_ID"],
stream=True,
messages=[{"role": "user", "content": "Count from one to five."}],
)
for chunk in stream:
text = chunk.choices[0].delta.content
if text:
print(text, end="", flush=True)
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.OMROUTER_API_KEY,
baseURL: 'https://omrouter.com/v1',
});
const stream = await client.chat.completions.create({
model: process.env.OMROUTER_MODEL_ID,
stream: true,
messages: [{ role: 'user', content: 'Count from one to five.' }],
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
}

An HTTP 200 only means the connection has started. The answer is complete after the SDK finishes normally or the stream sends its end marker.

  • Show an interrupted state when the connection ends early.
  • Do not retry automatically after displaying text or running a tool; that can duplicate content or actions.
  • Apply both connection and total-duration timeouts.
  • Stop reading when the client request is cancelled.
  • Do not log API keys, complete prompts, or model answers.

Chat Completions, Responses, Anthropic, and Gemini use different streaming formats. Use the matching SDK reader for each format.