WebSocket Streaming API
The WebSocket API keeps one connection open for many text-to-speech conversions. You send a config message, then text, and the server returns audio chunks as they are generated. It suits voice agents that stream text from an LLM and play audio back immediately.
If you do not need a persistent connection, the HTTP Streaming API is simpler. See Which API to use to compare.
For the full message schema, see the Text-to-Speech API Reference.
Quickstart
Set your API key as the SARVAM_API_KEY environment variable. These examples synthesize one sentence into output.mp3 and stop when the server sends the final completion event.
Always pass speaker in the config. The Python SDK’s default speaker (anushka) is rejected by both current models. The completion event is off unless you set send_completion_event. Without it, the server sends audio only and keeps the socket open, so your code must decide when to stop.
Message flow
- Open the connection with the
modeland, optionally,send_completion_eventquery parameters. The model is set only in the URL: amodelfield inside theconfigmessage is ignored. - Send a
configmessage first. It must includelanguage_codeandspeaker. - Send one or more
textmessages. Audio chunks start arriving as the buffered text is synthesized. - Send
flushto synthesize any text still in the buffer. - Read
audiomessages until theeventmessage withevent_typefinal(if enabled). - Reuse the connection for the next utterance, or close it.
Messages you send
config
text
flush
ping
Sets voice and output options. Send it first. You can send a new config later in the session. Text still in the buffer is flushed with the old settings before the new ones apply.
opus fails at the default 22,050 Hz. Set speech_sample_rate to 8000, 16000 or 24000 when you use it.
Sends text to synthesize. Each message can hold 1 to 2,500 characters. Send as many as you like on the same connection.
Text is buffered until it reaches min_buffer_size characters, then synthesized. Send flush to force the remainder through.
Synthesizes whatever is in the buffer now, regardless of min_buffer_size. Send it at the end of each utterance.
Keeps the connection alive during pauses. The server closes a connection after 60 seconds without any message, and it does not reply to a ping.
Messages you receive
Decode each audio value from base64 and append the bytes to your file or player. The bytes are in the codec you configured, and content_type tells you which one (audio/mpeg for mp3, audio/wav for wav, audio/pcm for linear16, and so on).
Errors
Rejected messages come back in-band as JSON. The connection stays open unless the server closes it.
An invalid key or exceeded rate limit rejects the connection before it opens (HTTP 403 or 429 on the upgrade request). See Errors & Troubleshooting and Credits & Rate Limits.
Handle disconnects
Reconnect with exponential backoff and a small random jitter, resend the config message, then resume sending text. Do not blindly retry on 4xxx closes.
Voice agent barge-in
The TTS WebSocket has no server-side cancel message. The only client messages are config, text, flush and ping. Handle an interruption on the client:
- Stop playback and clear your local audio buffer when your STT detects
speech_start. - Close the TTS socket so the server stops generating. In-flight chunks are discarded on your side.
- Open a fresh connection for the agent’s next reply.
Send replies as several shorter text messages so an interruption discards less generated audio. See the STT WebSocket barge-in recipe and the LiveKit and Pipecat voice-agent guides.