Real-time generation
Learn how to generate speech in real time with the Soniox Text-to-Speech WebSocket API, including configuration, streaming, responses, and errors.
Overview
Soniox Text-to-Speech AI generates native-speaker-quality speech in over 60 languages with ultra-low latency, hallucination-free output, and accurate pronunciation of alphanumerics like phone numbers, email addresses, and IDs. This is ideal for use cases like voice agents, conversational AI, live narration, and interactive assistants.
Real-time generation is provided through our WebSocket API, which streams audio back to you as text arrives. A single connection can host multiple concurrent streams for serving many users or voices at once.
How real-time generation works
Each stream is opened with a one-time config that picks the voice and audio output format. After that, Text-to-Speech is a two-way stream: you send text in small chunks as it becomes available, and Soniox streams audio back without waiting for the full input.
- Text in (chunks) → You send text incrementally over the WebSocket. Each message carries a
textfragment and atext_endflag marking the last chunk. - Audio out (chunks) → Soniox returns base64-encoded
audiopayloads as they are produced. You can decode and play or buffer each chunk on arrival. - Interleaved → Audio for the first words starts flowing back while you are still writing later chunks. You never have to wait for a full sentence before audio begins.
Pairing with an LLM
This is what makes real-time TTS the right fit for LLM-driven workloads. Pipe each LLM token (or token batch) straight into a Soniox text chunk. Because audio generation begins with the first words, the user hears speech while the LLM is still generating the rest of the response. No buffering the full reply before speaking, no awkward "thinking" pause between the model finishing and the voice starting.
Example streaming timeline
Here is what a single stream looks like on the wire. Outbound text chunks and inbound audio chunks are interleaved, they do not happen in two separate phases.
Client → Server (first text chunk):
Server → Client (audio already streaming back for "Hello"):
Client → Server (next text chunk sent while audio for earlier text keeps arriving):
Server → Client (more audio chunks):
Client → Server (final text chunk, text_end: true):
Server → Client (last audio chunk, audio_end: true):
Server → Client (stream terminated):
Bottom line: You don't wait for a full sentence before you hear anything. Audio for the first words starts streaming back while the rest of the text is still being written. Treat the stream as complete only after terminated: true.
Connection keepalive
Between streams, for example while waiting on an upstream LLM or between agent turns, send keepalive messages to prevent the WebSocket connection from timing out. Keepalive does not keep an individual stream open: a stream that receives no text for more than a few seconds without text_end: true is terminated. For details, see Connection keepalive.
API reference
For the full message schema, configuration parameters, cancellation, and error codes, see the WebSocket API reference.
Code example
Prerequisite: Complete the steps in Get started.
See on GitHub: soniox_sdk_realtime.py.