Nirmos
Gateway

Streaming chat

Consume typed SSE events once and assemble the final chat completion.

gateway.chat.stream(params, options?) returns a ChatStream. It parses gateway SSE and assembles text, tool arguments and usage. Use an enabled streaming-capable model or route and a configured client from the quickstart.

const stream = await nirmos.gateway.chat.stream({
  route: "production-chat",
  messages: [{ role: "user", content: "Write a short release note." }],
});
try {
  for await (const event of stream) {
    if (event.type === "content.delta") console.log(event.delta ?? "");
    if (event.type === "toolCall.delta") console.log(event.toolCall);
  }
  const completion = await stream.finalResponse();
  console.log(completion.usage, completion.metadata.requestId);
} catch (error) {
  // Covers failures during body consumption, not just initial HTTP setup.
  console.error(error);
  throw error;
}

Events

EventPayload
content.deltaOptional choiceIndex and text delta
toolCall.deltaPartial toolCall; argument fragments are assembled into final choices
completion.completedOptional normalized completion

All events carry id, model and metadata. Event names are extensible; ignore unfamiliar values. Usage is available when the server emits it, otherwise counters remain zero. Do not execute partial tool arguments.

One consumer

Finish active iteration before calling finalResponse(). Concurrent iteration or final-response draining raises StreamError. If live deltas are unnecessary, consume using one convenience method:

const stream = await nirmos.gateway.chat.stream({
  route: "production-chat",
  messages: [{ role: "user", content: "Say hello." }],
});
console.log(await stream.text());

text() drains the stream and returns outputText; finalResponse() can drain an unconsumed stream. After full consumption, the assembled result can be read again. Breaking from a loop cancels the reader and prevents a final completion. There is no separate public abort() method; pass a signal.

Cancellation and timeouts

const controller = new AbortController();
const stream = await nirmos.gateway.chat.stream({
  model: "configured/chat-model",
  messages: [{ role: "user", content: "Write a summary." }],
}, { signal: controller.signal, timeoutMs: 15_000 });
// Call controller.abort() when your UI action or downstream request is cancelled.
const completion = await stream.finalResponse();
console.log(completion.outputText);

The timeout remains active after headers, through body consumption. Caller cancellation raises RequestAbortedError, timeouts raise TimeoutError, and malformed/server-declared stream failures raise StreamError. The SDK does not retry or reconnect after stream consumption begins.

Gateway reference · ChatStream and event types · Errors

Compatible providers

Groq, OpenRouter and Cloudflare stream through the shared gateway. reasoning.delta carries optional reasoning text; signed reasoning details remain on the final message for continuation. Upstream error events fail the stream, including errors inside HTTP 200 responses. Fallback never continues after a chunk was emitted. Groq schema output and Cloudflare JSON mode cannot stream. See provider limits.

On this page