PineRouterDocs
GuidesModelsAPI referenceApps

Streaming

Get text as it is written, with server-sent events, on chat completions, responses and Claude messages.

Add "stream": true to a text request. The answer comes as server-sent events (Content-Type: text/event-stream) in the same format as the original API: OpenAI's for /v1/chat/completions and /v1/responses, Anthropic's for /v1/messages. Any client that streams from those APIs works unchanged.

Chat completions

Each event is a line data: {...} with a chunk whose choices[0].delta.content holds the next piece of text. The stream ends with data: [DONE].

curl -N https://pinerouter.ai/v1/chat/completions \
  -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" \
  -d '{"model": "gpt-6-luna", "stream": true, "messages": [{"role": "user", "content": "Write a haiku about rivers."}]}'
from openai import OpenAI

client = OpenAI(base_url="https://pinerouter.ai/v1", api_key="sk-...")
stream = client.chat.completions.create(
    model="gpt-6-luna",
    messages=[{"role": "user", "content": "Write a haiku about rivers."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
    if chunk.usage:
        print("\n", chunk.usage)
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://pinerouter.ai/v1", apiKey: "sk-..." });
const stream = await client.chat.completions.create({
  model: "gpt-6-luna",
  messages: [{ role: "user", content: "Write a haiku about rivers." }],
  stream: true,
});
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
  if (chunk.usage) console.log("\n", chunk.usage);
}

Usage in the last chunk

The last chunk before data: [DONE] carries usage (prompt, completion and total tokens) and an empty choices list. You get it by default. To leave it out, send "stream_options": {"include_usage": false}. Check choices is not empty before you read choices[0], as in the examples above.

Responses

POST /v1/responses with "stream": true sends typed events (event: response.output_text.delta, then response.completed with the full response and its usage), as OpenAI's Responses API does.

stream = client.responses.create(model="gpt-5-5", input="Write a haiku about rivers.", stream=True)
for event in stream:
    if event.type == "response.output_text.delta":
        print(event.delta, end="", flush=True)

Claude messages

POST /v1/messages with "stream": true sends Anthropic's events: message_start, content_block_delta with the text, message_delta with the final usage, and message_stop.

import anthropic

client = anthropic.Anthropic(base_url="https://pinerouter.ai", api_key="sk-...")
with client.messages.stream(
    model="gpt-5-2",
    max_tokens=300,
    messages=[{"role": "user", "content": "Write a haiku about rivers."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Errors during a stream

Errors before the first byte come back as a normal HTTP error with a JSON body (see Errors). Once the stream has started, the status is already 200, so an error arrives inside the stream:

  • Chat completions and responses: a last event data: {"error": {"message": "...", "type": "...", "code": "..."}}, then data: [DONE].
  • Claude messages: event: error with {"type": "error", "error": {"type": "...", "message": "..."}}.

Check every event for an error field, and treat a stream that ends without its normal final event ([DONE] after a chunk with a finish_reason, response.completed, or message_stop) as incomplete. A stream that sends nothing for 5 minutes is closed.

Stop a stream

Close the connection: break out of the loop and close the stream (stream.close() in Python, controller.abort() with an AbortController in JavaScript, Ctrl+C for curl). Generation stops, and you pay only for the tokens produced up to that point.

const controller = new AbortController();
const stream = await client.chat.completions.create(
  { model: "gpt-6-luna", messages: [{ role: "user", content: "Count to 1000." }], stream: true },
  { signal: controller.signal },
);
setTimeout(() => controller.abort(), 2000);

Jobs do not stream: start them and poll. See Jobs.