Skip to navigation

Set the WebSocket text buffer size

View as Markdown

min_buffer_size sets how many characters the server collects from your text messages before it starts synthesis. Text below the threshold waits for more text or a flush message.

WebSocket only. min_buffer_size is a field of the WebSocket config message. REST and HTTP streaming do not use it (they receive the whole text in one request).

Parametermin_buffer_size
Typeinteger
Range30–200
Default50
APIWebSocket (/text-to-speech/ws), in the config message
Modelsbulbul:v3, bulbul:v4-flash

How it works

SituationWhat happens
Buffered text reaches min_buffer_size charactersSynthesis starts and audio is streamed back.
Buffered text is shorter than the thresholdText is held. Nothing is synthesized.
You send {"type": "flush"}The buffer is processed immediately, whatever its length.

In a live test, with min_buffer_size set to 100, sending "Hi there." without a flush returned no audio, and a 62-character sentence with min_buffer_size set to 30 and no flush returned audio. Lower values start audio sooner when you stream text token by token. Higher values give the model more context per synthesis call. Always send flush at the end of an utterance.

Example

import asyncio
import base64
import os
from sarvamai import AsyncSarvamAI, AudioOutput, EventResponse
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="en-IN",
speaker="shubh",
min_buffer_size=80,
)
# Streamed in pieces, as an LLM would produce it
await ws.convert("Streaming text can arrive in small pieces, ")
await ws.convert("and the server starts speaking once enough has gathered.")
await ws.flush() # process whatever is still below min_buffer_size
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": "en-IN", "speaker": "shubh", "min_buffer_size": 80}}

send_completion_event="true" is a connection option (query parameter). It makes the server send {"type": "event", "data": {"event_type": "final"}} after the last audio message, which is the signal to stop reading.

Common errors

WhereMessageFix
In-band error message, then the connection closesconfig.data.min_buffer_size: Input should be greater than or equal to 30Use a value from 30 to 200.
In-band error message, then the connection closesconfig.data.min_buffer_size: Input should be less than or equal to 200Same.
No audio, no errorText is below the threshold and no flush was sentSend {"type": "flush"}.

Related: max_chunk_length controls how buffered text is split into sentences.