Skip to content

Streaming text-to-speech: start speaking before the reply is done

Stream a model's reply into text-to-speech so the first words play while the rest is still being written, and play the audio in chunks as it arrives, with TypeScript examples.

Streaming text-to-speech means two kinds of streaming: sending text to the speech provider while the model is still writing it, and playing audio chunks as they come back. Do both and the listener hears the first sentence before the model has finished the last one. In VoiceAI SDK, you pass the model's AsyncIterable<string> to session.speak and iterate the result.

The examples are written against the planned 1.0 API of VoiceAI SDK, a developer preview, and were not run against live accounts. How soon the first audio arrives depends on the provider, the model and the network, so this post shows how to measure it instead of quoting figures.

Three ways to speak a reply at a glance

Wait, then speakStream audio outStream text in and audio out
Callawait session.speak(text)for await (const chunk of session.speak(text))for await (const chunk of session.speak(textStream))
First sound afterThe reply is written and fully synthesizedThe reply is written and the first chunk is readyThe first words are written and synthesized
Relies on streamingTtsNoYes, for chunks before the endYes, for chunks before the end
Good forSaving a file, short fixed promptsFixed text in a live callModel replies in a live call

Only the last column lets speech overlap with the model's writing. The rest of this post builds up to it.

Stream audio out in chunks

Awaiting a Speech gives you the whole clip. Iterating it gives you { data, ms } chunks as they arrive: audio bytes in the session's format, and how long they play.

import { createSession } from "voiceai-sdk";
import { cartesia } from "voiceai-sdk/cartesia";

await using session = await createSession(cartesia({ voice: process.env.VOICE_ID }), {
  sampleRate: 16000,
  encoding: "pcm16",
});

// Raw 16 kHz pcm16 on stdout. Pipe it to a player, or send each chunk over a WebSocket.
for await (const chunk of session.speak("Your order shipped this morning. It should arrive on Thursday.")) {
  process.stdout.write(chunk.data);
}

Whether chunks arrive before the whole clip is synthesized depends on the provider. Check session.capabilities.streamingTts. When it is false, speak still works, but the audio arrives as one chunk at the end.

Stream text in from the model

speak also takes an AsyncIterable<string>. The session sends each piece to the provider as it arrives, so synthesis starts while the model is still writing. With the AI SDK, that is the textStream of a streamText call:

import { streamText } from "ai";
import { createSession } from "voiceai-sdk";
import { elevenlabs } from "voiceai-sdk/elevenlabs";

await using session = await createSession(elevenlabs({ voice: process.env.VOICE_ID }), {
  sampleRate: 16000,
  encoding: "pcm16",
});

const result = streamText({
  model: "openai/gpt-5",
  prompt: "In two short spoken sentences, explain how to return an unused item.",
});

for await (const chunk of session.speak(result.textStream)) {
  process.stdout.write(chunk.data);
}

Adapters handle text that arrives in pieces in different ways. Per their provider pages, the ElevenLabs adapter buffers streamed text until a sentence boundary before sending it, which keeps intonation natural, and the Cartesia adapter keeps one context per utterance, so pieces are spoken as one sentence. Either way, you pass the stream and the adapter handles it.

Clean the text without buffering it

Models write Markdown, and asterisks and pound signs don't belong in speech. Asking for plain sentences in the prompt is the first fix. To filter what slips through, transform the stream piece by piece. Collecting the full text first would undo the streaming.

import { streamText } from "ai";

// Drop Markdown symbols from each piece as it passes through.
async function* forSpeech(text: AsyncIterable<string>) {
  for await (const piece of text) yield piece.replace(/[*#_]/g, "");
}

const result = streamText({
  model: "openai/gpt-5",
  system: "Answer in plain spoken sentences, with no Markdown, lists or links.",
  prompt: "How do I return an unused item?",
});

for await (const chunk of session.speak(forSpeech(result.textStream))) {
  process.stdout.write(chunk.data);
}

Return the stream from converse

In a conversation, session.converse does the speaking. Return the stream from your handler and the session passes it to speak the same way:

import { streamText } from "ai";

await session.converse((text, ctx) => {
  const result = streamText({
    model: "openai/gpt-5",
    system: "You are a support agent. Start with one short sentence that answers the question.",
    messages: [...ctx.history, { role: "user", content: text }],
    abortSignal: ctx.signal,
  });
  return result.textStream; // not: return await result.text
});

A handler that awaits result.text and returns a string makes the caller wait for the whole reply before hearing any of it. Returning the stream also lets the session cut it off cleanly: if the caller talks over the reply, speech stops and ctx.signal aborts the model. Barge-in covers that path.

Measure time to first audio

Time from the moment you call the model to the first audio chunk, and to the end of the stream:

import { streamText } from "ai";

const start = performance.now();
const result = streamText({ model: "openai/gpt-5", prompt: "In two short sentences, explain how returns work." });

let first = true;
for await (const chunk of session.speak(result.textStream)) {
  if (first) {
    console.error("first audio after", Math.round(performance.now() - start), "ms");
    first = false;
  }
  process.stdout.write(chunk.data);
}
console.error("last audio after", Math.round(performance.now() - start), "ms");

Run it many times with your provider, region and prompt, and compare medians. Ask for a short first sentence in the prompt: speech can only start once the model has written something to say.

Common questions

Does streaming change how the voice sounds? It can. A provider that gets text in pieces has less context for intonation than one that gets a whole paragraph. The adapters handle this per provider, as described above, and the result depends on the provider and the voice.

Which providers stream speech? Check streamingTts on the session, or in the matrix on Compare providers. In a pipeline, the flag comes from the tts provider.

How do I stop a streamed reply? In converse, barge-in stops it for you. Outside it, call session.interrupt(), which stops the current speech right away.

Can I save streamed speech to a file? Await the Speech instead of iterating it to get the complete audio, or use the CLI: npx voiceai-sdk say "Hello" --provider elevenlabs --voice "$VOICE_ID" --out hello.wav.

Next steps

More guides on the VoiceAI SDK blog, or start with the quickstart.