Streaming

The ElevenLabs API provides the ability to stream responses back to a client in order to allow partial results for certain requests. To achieve this, we follow the Server-sent events standard. Our official Node and Python libraries include helpers to make parsing these events simpler.

Streaming is supported for the Text to Speech API, Voice Changer API & Audio Isolation API. This section focuses on how streaming works for requests made to the Text to Speech API.

In Python, a streaming request looks like:

1from elevenlabs import stream
2from elevenlabs.client import ElevenLabs
3
4client = ElevenLabs()
5
6audio_stream = client.text_to_speech.convert_as_stream(
7 text="This is a test",
8 voice_id="JBFqnCBsd6RMkjVDRZzb",
9 model_id="eleven_multilingual_v2"
10)
11
12# option 1: play the streamed audio locally
13stream(audio_stream)
14
15# option 2: process the audio bytes manually
16for chunk in audio_stream:
17 if isinstance(chunk, bytes):
18 print(chunk)

In Node / Typescript, a streaming request looks like:

1import { ElevenLabsClient, stream } from "elevenlabs";
2import { Readable } from "stream";
3
4const client = new ElevenLabsClient();
5
6async function main() {
7 const audioStream = await client.textToSpeech.convertAsStream(
8 "JBFqnCBsd6RMkjVDRZzb",
9 {
10 text: "This is a test",
11 model_id: "eleven_multilingual_v2",
12 }
13 );
14
15 // option 1: play the streamed audio locally
16 await stream(Readable.from(audioStream));
17
18 // option 2: process the audio manually
19 for await (const chunk of audioStream) {
20 console.log(chunk);
21 }
22}
23
24main();
Built with