Streaming

Last updated:

On this page

Set stream: true and the response is delivered token by token via server-sent events (SSE) — standard OpenAI format, so the SDKs' built-in streaming works directly.

python
from openai import OpenAI

client = OpenAI(api_key="sk-st-your-key", base_url="https://api.staik.se/v1")

stream = client.chat.completions.create(
    model="qwen3.6:35b-a3b",
    messages=[{"role": "user", "content": "Write a haiku about Stockholm."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Chunk format

Each SSE event is a data: line with a JSON chunk; the stream ends with data: [DONE]:

text
data: {"choices":[{"delta":{"content":"Snow"},"index":0}], ...}
data: {"choices":[{"delta":{"content":" falls"},"index":0}], ...}
data: [DONE]
  • Incremental text arrives in choices[0].delta.content
  • With the thinking aliases the reasoning streams separately in choices[0].delta.reasoning_content
  • Tool calls stream as delta.tool_calls fragments

The Anthropic endpoint

/v1/messages streams in Anthropic format instead: message_start, content_block_delta, message_stop.

Long responses

Streaming is recommended for anything interactive — the first token arrives as soon as the model starts generating, instead of buffering the whole reply.