Skip to navigation

Set the output bitrate

View as Markdown

output_audio_bitrate sets the bitrate of compressed streaming audio. Lower values give smaller output, higher values give better quality.

Parameteroutput_audio_bitrate
Typestring
Values32k, 64k, 96k, 128k, 192k
Default128k
APIsHTTP streaming (POST /text-to-speech/stream) and WebSocket (/text-to-speech/ws)
Modelsbulbul:v3, bulbul:v4-flash

The REST API does not define this parameter. REST requests that include it still succeed, but the field is ignored.

Example: HTTP streaming

The body of the response is the audio itself, so no base64 decoding is needed.

import os
import httpx
response = httpx.post(
"https://api.sarvam.ai/text-to-speech/stream",
headers={"api-subscription-key": os.environ["SARVAM_API_KEY"]},
json={
"text": "Welcome to Sarvam AI!",
"language_code": "en-IN",
"model": "bulbul:v3",
"speaker": "shubh",
"output_audio_codec": "mp3",
"output_audio_bitrate": "64k",
},
)
response.raise_for_status()
with open("output.mp3", "wb") as f:
f.write(response.content)

Example: WebSocket

WebSocket-only snippet using the Python SDK. Audio arrives as base64 in AudioOutput messages.

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",
output_audio_codec="mp3",
output_audio_bitrate="64k",
)
await ws.convert("Welcome to Sarvam AI!")
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. send_completion_event="true" is a connection option (a query parameter) that makes the server send a final event when synthesis finishes.

What changes in the output

  • For mp3, a lower bitrate produces smaller chunks. In a test of the same short sentence over WebSocket, 32k produced roughly 2 KB chunks and 192k roughly 10 KB chunks.
  • Use 64k for speech where size matters, and 128k or higher where fidelity matters.

Common errors

WhereMessageFix
HTTP stream, 400output_audio_bitrate: Input should be '32k', '64k', '96k', '128k' or '192k'Use one of the five values.
WebSocket, in-band error messageValidation Error(s): config.data.output_audio_bitrate: Input should be '32k', '64k', '96k', '128k' or '192k'Same. The server then closes the connection.