← klaro.services
SentinelConsentraKlaroShieldBundlesPricing
Streaming

Wrapping a streaming completion

klaro.wrap() awaits the promise your function returns -- for a stream, that means retries cover the initial request that opens the stream, not failures mid-stream.

← All recipes
import { Klaro, retries, secrets } from "@klaroshield/sdk";
import OpenAI from "openai";

const openai = new OpenAI();
const klaro = new Klaro().use(retries({ max: 2 })).use(secrets({ mode: "mask" }));

const startStream = klaro.wrap(
  openai.chat.completions.create.bind(openai.chat.completions)
);

const stream = await startStream({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "Count to 10." }],
  stream: true,
});

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

Checked against the shipped @klaroshield/sdk API. Full docs at klaro.services/klaroshield/docs.