Streaming Guide
Design resilient clients for SSE streaming across model protocols.
Streaming
For OpenAI-compatible and Claude-compatible requests, set stream: true in the JSON body. Gemini streaming can use :streamGenerateContent, alt=sse, or model/client-specific streaming helpers. Streaming responses are delivered as SSE chunks; handle network disconnects and mid-stream errors explicitly.
curl https://onerouter.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-5.2",
"messages": [{"role": "user", "content": "Write a short launch note."}],
"stream": true
}'Consume a stream
With an OpenAI-compatible SDK, iterate the streamed chunks and read choices[0].delta.content as it arrives. Print or forward each fragment to build the response incrementally.
from openai import OpenAI
client = OpenAI(base_url="https://onerouter.com/v1", api_key="YOUR_API_KEY")
stream = client.chat.completions.create(
model="openai/gpt-5.2",
messages=[{"role": "user", "content": "Write a short launch note."}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content or ""
print(delta, end="", flush=True)Client behavior
Treat streaming as a long-lived network operation. Parse SSE frames incrementally, ignore keep-alive comments, surface mid-stream errors to users, and let users cancel the request. If a stream disconnects before a final completion event, check request logs and retry only if your operation is safe to repeat.