Skip to content

Streaming

Token-by-token streaming using streamResponse().

ts
import { LanguageModelSession } from "tsfm-sdk";

async function main() {
  const session = new LanguageModelSession();

  process.stdout.write("Streaming: ");
  for await (const chunk of session.streamResponse("Tell me a joke in one sentence.")) {
    process.stdout.write(chunk);
  }
  console.log();

  session.dispose();
}

main().catch(console.error);

What This Shows

  1. Create a session (model availability check can be skipped for brevity)
  2. Use for await...of to iterate over response chunks
  3. Each chunk contains only new tokens — write directly to stdout

Released under the Apache 2.0 License.