Streaming audio
Send complete text to POST /v1/audio/speech with stream: true to receive audio progressively. With stream: false, the server generates the complete audio before returning it.
Play a streamed response
Set your API key as shown in the TTS quickstart and install FFmpeg. This example plays 24 kHz, signed 16-bit little-endian PCM, mono audio as it arrives:
Streaming response
The response contains HTTP headers followed by a continuous binary audio body. For an input of Hello., a PCM response looks like this:
x-request-id identifies the request. x-usage-input-characters reports the input character count; Usage and billing explains how it is measured.
Streamed WAV has an unknown final length in its header. Use PCM if your player requires a finalized WAV length.
Read the bytes in order as they arrive. Read boundaries do not correspond to audio frames or separate files. The stream completes when the HTTP response body ends normally; there is no separate completion event.
Receive audio in Python
Install requests:
This example reads incoming bytes and saves them to speech.pcm. It checks the status before writing audio and reports completion only after the response has been fully read.
The JSON stream option enables server-side streaming. Python’s stream=True reads the response incrementally. chunk_size=4096 is a client read setting, not a server chunk-size guarantee. The timeout values are client settings, not API limits.
Play audio from Python as it arrives
This complete example also requires FFmpeg. It passes each read to ffplay, lets buffered audio finish playing on normal completion, and stops the player if receiving audio fails.
Playback tips
- Keep the receive loop responsive. Avoid slow unrelated work between reads. If playback runs separately, pass audio through a bounded queue so buffering cannot grow indefinitely.
- Tune the playback buffer when needed. If audio stutters, increase the amount buffered before playback; if startup is too slow, reduce it. At this format, 100 ms is 4,800 bytes—an example conversion, not a required buffer size.
- Preserve sample boundaries in custom decoders. Each PCM sample uses two bytes. If a read ends with one unmatched byte, keep it for the next read.
ffplayhandles this in the examples.
Interrupted streams
Errors detected before response headers are sent return a non-2xx status with a JSON body. Check the status before treating the body as audio.
After headers are sent, the status cannot change. A connection error or read timeout while receiving audio means the request has not completed successfully, even if the status was 200 and some speech has played. No JSON error is appended to the audio.
Do not mark partial audio as a completed result. The file-writing example may leave a partial speech.pcm after a read failure; the playback example stops its player. A retry sends the full text again and can repeat speech already heard. There is no resume offset. Errors and troubleshooting covers retryable statuses and request IDs.