Sarvam AI Python SDK

raw JSON →
0.1.28 verified Fri May 01 auth: no python

Official Python SDK for Sarvam AI APIs, providing access to text-to-speech, speech-to-text, translation, and text generation models. Current version 0.1.28. Actively maintained with monthly releases.

pip install sarvamai
error sarvamai.exceptions.AuthenticationError: Invalid API key
cause Missing or incorrect API key.
fix
Set SARVAM_API_KEY environment variable to a valid key from Sarvam AI dashboard.
error TypeError: __init__() got an unexpected keyword argument 'api_key'
cause Installed version <0.1.0 which uses positional argument for key.
fix
Upgrade to >=0.1.0: pip install --upgrade sarvamai
error AttributeError: 'SarvamAI' object has no attribute 'tts'
cause Code uses deprecated `.tts` method.
fix
Use .text_to_speech() instead.
gotcha api_key parameter is required; no default key or fallback. Must be set via environment variable or passed explicitly.
fix Export SARVAM_API_KEY environment variable or pass api_key to constructor.
gotcha All text inputs should be provided in the target language script; no automatic transliteration.
fix Ensure text is already in the target language's script (e.g., Devanagari for Hindi).
breaking In v0.1.20, the `client.tts` method was renamed to `client.text_to_speech`. Old code using `.tts` will break.
fix Replace `client.tts(...)` with `client.text_to_speech(...)`.

Initialize client with API key and call TTS endpoint.

from sarvamai import SarvamAI

client = SarvamAI(api_key=os.environ.get('SARVAM_API_KEY', ''))
response = client.text_to_speech(text='Hello world', target_language_code='hi-IN')
print(response.audio_content)