Introduction
Streaming
Receive AI responses in real-time via Server-Sent Events (SSE).
Streaming
Receive AI responses in real-time via Server-Sent Events (SSE). Perfect for chat applications where you want to show responses as they're being typed.
How It Works
Set "stream": true in your request to enable streaming. The gateway returns an SSE stream with partial responses.
Text Completion Streaming
curl https://viprouter.com/v1/completions \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o-mini",
"prompt": "Write a short haiku about the ocean",
"stream": true
}'Response (SSE):
data: {"choices":[{"text":"Waves whisper soft"}],"model":"gpt-4o-mini"}
data: {"choices":[{"text":" on distant shores, the moon calls"}],"model":"gpt-4o-mini"}
data: {"choices":[{"text":" tides to rise."}],"model":"gpt-4o-mini"}
data: [DONE]Chat Completion Streaming
curl https://viprouter.com/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o-mini",
"messages": [
{"role": "user", "content": "Tell me a story about a robot learning to paint"}
],
"stream": true
}'Response (SSE):
data: {"choices":[{"delta":{"content":"Once"}}],"model":"gpt-4o-mini"}
data: {"choices":[{"delta":{"content":" upon"}}],"model":"gpt-4o-mini"}
data: {"choices":[{"delta":{"content":" a"}}],"model":"gpt-4o-mini"}
data: [DONE]Each chunk contains partial content that you append to build the complete response.
Responses API Streaming
Stream the OpenAI-style Responses API with event-based SSE:
curl https://viprouter.com/v1/responses \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o-mini",
"input": "Tell me one interesting fact about Mars",
"stream": true
}'Response (SSE):
event: response.created
data: {"type":"response.created"}
event: response.output_text.delta
data: {"type":"response.output_text.delta","delta": /* partial text */}
event: response.completed
data: {"type":"response.completed","response":{ /* usage, finish_reason */ }}Python Example
from openai import OpenAI
client = OpenAI(base_url="https://viprouter.com/v1", api_key="your-key")
stream = client.chat.completions.create(
model="openai/gpt-4o-mini",
messages=[{"role": "user", "content": "Count to 10"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")Notes
- Streaming requests follow the default timeout (30 seconds)
- Usage and finish reason are sent only in the last chunk
- Content is sent in all previous chunks
