Skip to navigation

Set the maximum sentence chunk length

View as Markdown

max_chunk_length sets the maximum number of characters in each sentence chunk when the server splits buffered text for synthesis.

WebSocket only. max_chunk_length is a field of the WebSocket config message. REST and HTTP streaming do not use it.

Parametermax_chunk_length
Typeinteger
Range50–500
Default150
APIWebSocket (/text-to-speech/ws), in the config message
Modelsbulbul:v3, bulbul:v4-flash

Example

import asyncio
import base64
import os
from sarvamai import AsyncSarvamAI, AudioOutput, EventResponse
TEXT = (
"भारत की संस्कृति विश्व की सबसे प्राचीन संस्कृतियों में से एक है। "
"यह विविधता और परंपराओं का अद्भुत संगम है। "
"यहाँ अनेक भाषाएँ, त्योहार और संगीत की परंपराएँ मिलती हैं।"
)
async def main():
client = AsyncSarvamAI(api_subscription_key=os.environ["SARVAM_API_KEY"])
async with client.text_to_speech_streaming.connect(
model="bulbul:v3", send_completion_event="true"
) as ws:
await ws.configure(
target_language_code="hi-IN",
speaker="shubh",
max_chunk_length=200,
)
await ws.convert(TEXT)
await ws.flush()
with open("output.mp3", "wb") as f:
async for message in ws:
if isinstance(message, AudioOutput):
f.write(base64.b64decode(message.data.audio))
elif isinstance(message, EventResponse) and message.data.event_type == "final":
break
asyncio.run(main())

In the SDK, configure() takes target_language_code. On the raw wire, the config message field is language_code:

{"type": "config", "data": {"language_code": "hi-IN", "speaker": "shubh", "max_chunk_length": 200}}

What changes in the output

max_chunk_length is the upper bound on how many characters the server groups into one synthesis unit when it splits long text. The spoken text is unchanged. In a short live test, audio was returned for both 50 and 500, so choose the value from your own listening and timing tests.

Common errors

WhereMessageFix
In-band error message, then the connection closesconfig.data.max_chunk_length: Input should be greater than or equal to 50Use a value from 50 to 500.
In-band error message, then the connection closesconfig.data.max_chunk_length: Input should be less than or equal to 500Same.

Related: min_buffer_size controls when buffered text starts processing.