HTTP Streaming API
POST /text-to-speech/stream returns audio as a raw binary stream that starts arriving while the text is still being synthesized. You can save it, pipe it to a player or forward it to a client without waiting for the full file. It needs no WebSocket handshake, config message or connection management.
Typical uses are server-side audio generation, proxying audio to a web or mobile client, batch jobs, and serverless or edge runtimes where WebSockets are not available.
HTTP Stream or WebSocket
Choose HTTP Stream when you already have the full text. Choose WebSocket when text arrives incrementally or you send many utterances on one connection. For a three-way comparison including REST, see Which API to use.
Quickstart
Set your API key as the SARVAM_API_KEY environment variable, then run one of these. Each writes the streamed audio to output.mp3.
Always pass speaker explicitly. The Python SDK’s default speaker is not valid for current models, and the API default depends on model.
To play audio as it arrives, pipe the response into a player. This needs ffmpeg installed.
Request parameters
Streaming supports sample rates up to 24,000 Hz only. 32000, 44100 and 48000 are available on the REST API and return a 400 here. opus fails with a 400 at the default 22,050 Hz, so set speech_sample_rate to 8000, 16000 or 24000 when you use it. Unknown request fields are ignored without an error.
Response
The body is raw audio in the codec you requested, sent with chunked transfer. It is not JSON and not base64, so write the bytes straight to a file or a player. Unlike the REST API, nothing needs decoding.
Use a pronunciation dictionary
Pass dict_id to apply your custom pronunciations while streaming. Replace p_5cb7faa6 with the ID of a dictionary you created. An unknown dict_id returns a 500 (“error preprocessing input text”) on this endpoint, where the REST API returns a 404, so confirm the ID exists first.
See the Pronunciation Dictionary guide to create a dictionary.
Errors
Validation errors are returned as JSON, not audio. Check the status code before you treat the body as audio.
The full code table and SDK exception reference are on Errors & Troubleshooting.
See the API Reference for the full endpoint spec.
Need help? Reach out on Discord.