Client SDK · Chat

Text helpers.

Filter a chat stream down to assistant text, with deduped deltas.

These helpers filter the event stream to just text — useful when you only need the response text and don't care about tool calls, usage, or status events.

chat.streamText(params, options?)

Returns an async generator that yields raw string deltas. Throws SafetyReplaceError when the server replaces the output (safety filter). Throws Error on stream errors.

TypeScript
try {
  for await (const chunk of client.chat.streamText({ input: 'Write a haiku.' })) {
    process.stdout.write(chunk);
  }
} catch (err) {
  if (err instanceof SafetyReplaceError) {
    console.error(`\nSafety replace: ${err.categories.join(', ')}`);
  } else {
    console.error(err);
  }
}
Note

Why throw on safety replace? The original text has already been yielded to the consumer by the time the replace event arrives. Throwing forces the consumer to handle the replacement explicitly — silently dropping it would lose the replacement message.

chat.createText(params, options?)

Non-streaming convenience that returns just the final output text. Safety replace is handled naturally — the replacement text is returned. Throws if the stream produced any error events.

TypeScript
const text = await client.chat.createText({ input: 'What is 2 + 2?' });
console.log(text); // "4"

SafetyReplaceError

TypeScript
class SafetyReplaceError extends Error {
  readonly categories: string[];
  // message = replacement content (or empty string)
}