Skip to main content
For real-time LLM pipelines, use client.tts.streamingSession() instead of client.tts.stream(). The session endpoint (/ws/tts/stream) keeps a persistent WebSocket connection and accumulates LLM tokens server-side, starting generation at natural sentence boundaries.

Why not flush per sentence?

Calling send(token, flush=true) on every sentence feels intuitive, but it actually increases latency:
  • Each flush triggers a full model prefill (the fixed cost of loading context into the model).
  • The server’s KV cache cannot be reused across separate flushes, so each segment is cold.
  • Word-level flushing adds avoidable latency per sentence compared to letting the server batch — see Latency.
Let the server handle chunking via chunkLengthSchedule and autoMode.

Basic usage

Session Reuse

End a session without closing the WebSocket to avoid reconnection overhead (see Latency):

Barge-in (interrupt the current turn)

When the end user speaks over the agent, call cancelCurrent() to stop generating the current turn immediately and drop any buffered/queued text — without closing the WebSocket. Unlike endSession(), no remaining text is flushed; the turn is abandoned. The socket stays open so you can send() the next turn right away.
cancelCurrent() resolves once the server acknowledges (onInterrupted fires), or after a short quiet timeout if the server goes silent. Stop local playback as soon as you call it — a few in-flight frames may arrive before the acknowledgement. See Barge-in for the full protocol.

Updating settings mid-session

Change generation parameters on a live connection without reconnecting via updateSettings(). It sends an explicit, acknowledged update and resolves with the parameters now in effect:
Only generation parameters are updatable: cfgScale, temperature, speed, maxNewTokens, language, normalize. Identity / audio-format settings (voiceId, modelId, sampleRate, outputFormat, dictionaryIds) are fixed for the connection — change those with updateConfig() after endSession(). The change applies to the next turn; call it between turns. The promise rejects if the server refuses an out-of-range value. MultiContextSession exposes updateSettings() too (session-scoped; applies to contexts started after the update).

Chunking presets

autoMode: true and small chunkLengthSchedule values minimise time-to-first-audio. Use larger values when prosody quality matters more than TTFA.
Avoid calling send(text, true) (flush=true) on every sentence. This bypasses server-side semantic chunking, forces a cold model prefill per segment, and degrades both latency and audio quality.

Session methods

streamingSession(config, callbacks) returns a StreamingSession: voiceId is optional in the TypeScript declaration but must be set before the first synthesis; omission fails with MISSING_VOICE_ID.

Multi-Context Sessions

A multi-context session manages up to 20 independent audio-generation contexts over a single WebSocket. Each context has its own text buffer, voice settings, and generation queue — useful for multi-speaker conversations, pre-buffering one stream while another plays, or interleaving audio for dynamic dialogue.
Create the session with createMultiContextSession(config?): Every context that synthesizes text must have an effective voice. Configure defaultVoiceId or pass voiceId to each createContext() call; a context with neither fails on its first text with MISSING_VOICE_ID. MultiContextSession methods and properties: Audio arrives via the onChunk callback as a MultiContextAudioChunk — an AudioChunk plus a contextId field identifying its context.

Multi-context types


Shared interfaces (StreamConfig, StreamingSessionCallbacks, SessionUsage, AudioChunk) are documented in Types & Errors.