Streaming responses with Velqa — SSE, OpenAI-compatible
Velqa can return chat responses as a stream: tokens arrive as they're generated, via Server-Sent Events (SSE). This is ideal for a chat-style UI where you want to display text progressively. Just add "stream": true to your /v1/chat/completions request.
Enable streaming (curl)
curl https://api.velqa.dev/v1/chat/completions \
-H "Authorization: Bearer sk-..." \
-H "Content-Type: application/json" \
-d '{
"model": "kimi-k2.6",
"messages": [{"role": "user", "content": "Write a haiku about Casablanca."}],
"stream": true
}'SSE chunk format
The response is a stream of data: lines. Each chunk is a JSON object whose incremental text is in choices[0].delta.content. The stream ends with a data: [DONE] line.
data: {"choices":[{"delta":{"content":"Grey"}}]}
data: {"choices":[{"delta":{"content":" waves"}}]}
data: {"choices":[{"delta":{"content":" at"}}]}
data: [DONE]Streaming in Python (openai SDK)
With the OpenAI SDK, pass stream=True and iterate over the chunks:
from openai import OpenAI
client = OpenAI(base_url="https://api.velqa.dev/v1", api_key="sk-...")
stream = client.chat.completions.create(
model="kimi-k2.6",
messages=[{"role": "user", "content": "Write a haiku about Casablanca."}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
print()Good to know
- Some chunks have an empty
delta(for example the very first, which carries the role) — always check thatdelta.contentis notNonebefore printing it. - Streaming works with tool calling:
tool_callsalso arrive in fragments insidedelta. - Usage/billing is identical to a non-streamed request.
See also: chat overview.
