Skip to main content
ElevenLabs Documentation Docs
current

Search documentation

Type to search this documentation.

On this pageOverview

Stitching multiple requests

This guide shows you how to maintain voice prosody across multiple text chunks/generations.

When converting a large body of text into audio, you may encounter abrupt changes in prosody from one chunk to another. This can be particularly noticeable when converting text that spans multiple paragraphs or sections. In order to maintain voice prosody over multiple chunks, you can use the Request Stitching feature.

This feature allows you to provide context on what has already been generated and what will be generated in the future, helping to maintain a consistent voice and prosody throughout the entire text.

Here’s an example without Request Stitching:

https://eleven-public-cdn.elevenlabs.io/audio/docs/without_request_stitching.mp3

And the same example with Request Stitching:

https://eleven-public-cdn.elevenlabs.io/audio/docs/with_request_stitching.mp3

Request Stitching is easiest when using the ElevenLabs SDKs.

  1. Stitch multiple requests together

    Create a new file named example.py or example.mts, depending on your language of choice and add the following code:

    Python
    import os
    from io import BytesIO
    from elevenlabs.client import ElevenLabs
    from elevenlabs.play import play
    from dotenv import load_dotenv
    
    load_dotenv()
    
    ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
    
    elevenlabs = ElevenLabs(
        api_key=ELEVENLABS_API_KEY,
    )
    
    paragraphs = [
        "The advent of technology has transformed countless sectors, with education ",
        "standing out as one of the most significantly impacted fields.",
        "In recent years, educational technology, or EdTech, has revolutionized the way ",
        "teachers deliver instruction and students absorb information.",
        "From interactive whiteboards to individual tablets loaded with educational software, ",
        "technology has opened up new avenues for learning that were previously unimaginable.",
        "One of the primary benefits of technology in education is the accessibility it provides.",
    ]
    
    request_ids = []
    audio_buffers = []
    
    for paragraph in paragraphs:
        # Usually we get back a stream from the convert function, but with_raw_response is
        # used to get the headers from the response
        with elevenlabs.text_to_speech.with_raw_response.convert(
            text=paragraph,
            voice_id="T7QGPtToiqH4S8VlIkMJ",
            model_id="eleven_v4",
            previous_request_ids=request_ids
        ) as response:
            request_ids.append(response._response.headers.get("request-id"))
    
            # response._response.headers also contains useful information like 'character-cost',
            # which shows the cost of the generation in characters.
    
            audio_data = b''.join(chunk for chunk in response.data)
            audio_buffers.append(BytesIO(audio_data))
    
    combined_stream = BytesIO(b''.join(buffer.getvalue() for buffer in audio_buffers))
    
    play(combined_stream)
  2. Execute the code

    Python
    python example.py

    You should hear the combined stitched audio play.

How does Request Stitching work with streaming?

In order to use the request IDs of a previous request for conditioning it needs to have processed completely. In case of streaming this means the audio has to be read completely from the response body.

How much difference does Request Stitching make?

The difference depends on the model, voice and voice settings used.

How old can the request IDs be?

The request IDs should be no older than two hours.

Is Request Stitching available on every plan?

Yes, unless you are an enterprise user with increased privacy requirements.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu