APIQIKAPIQIK
Docs
Guides

Streaming Responses

Read incremental output with stream=true and handle completion, cancellation, and disconnections.

With stream: true, the server returns incremental data during generation, so users can see content before the complete answer is ready.

Python example

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["APIQIK_API_KEY"],
    base_url="https://api.apiqik.com/v1",
)

stream = client.chat.completions.create(
    model="qwen-turbo",
    messages=[
        {"role": "user", "content": "Explain the value of intelligent routing in three points."}
    ],
    stream=True,
)

for chunk in stream:
    if not chunk.choices:
        continue
    content = chunk.choices[0].delta.content
    if content:
        print(content, end="", flush=True)

print()

Node.js example

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.APIQIK_API_KEY,
  baseURL: "https://api.apiqik.com/v1",
});

const stream = await client.chat.completions.create({
  model: "qwen-turbo",
  messages: [
    { role: "user", content: "Explain the value of intelligent routing in three points." },
  ],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}

Raw HTTP

curl -N https://api.apiqik.com/v1/chat/completions \
  -H "Authorization: Bearer $APIQIK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen-turbo",
    "messages": [{"role": "user", "content": "Write a product introduction"}],
    "stream": true
  }'

Implementation notes

  • Streamed content is in choices[0].delta.content, not message.content.
  • Not every chunk contains text: tool calls, role information, and usage statistics may arrive separately.
  • Batch frequent frontend updates to avoid an expensive render for every character.
  • When a user stops generation, cancel both the browser request and the backend connection to APIQIK.
  • Do not assume seamless continuation after a disconnect; prevent duplicate content when retrying.
  • For raw HTTP, parse SSE event boundaries rather than treating network packets as complete events.