Libraries & SDKs
Libraries & SDKs
Official Python and JavaScript clients for the Sarvam AI API — with async, retries, timeouts, streaming, and typed errors.
The official SDKs wrap every Sarvam AI API — Speech-to-Text, Text-to-Speech, Translation, Chat Completion, and Document Digitization — behind a typed, ergonomic client. They handle auth, serialization, multipart uploads, and error mapping so you don’t have to hand-roll HTTP calls.
Official SDKs vs. generated request snippets. The Python and JavaScript SDKs above are hand-tested, fully supported clients. The per-endpoint snippets you see in other languages (cURL, Go, Swift, etc.) on API Reference pages are auto-generated request examples — useful as a starting point, but only Python and JavaScript are first-class SDKs.
Initialize the client
Pass your API key directly, or let the SDK read it from the SARVAM_API_KEY environment variable.
Async usage
Both SDKs support fully asynchronous calls — use them when you need concurrency (e.g. fanning out many requests, or inside an async web server or voice agent).
In JavaScript, every method is already Promise-based — await it directly. In Python, the synchronous SarvamAI and asynchronous AsyncSarvamAI expose the exact same method names and arguments, so you can switch with a one-line change.
Timeouts and retries
Configure timeouts and automatic retries either globally (on the client) or per request. Per-request options override the client defaults.
Retries use exponential backoff and apply to transient failures (HTTP 429, 5xx, and connection errors). For a full retry/backoff helper and idempotency guidance, see Errors & Troubleshooting.
Handling errors
The SDKs raise typed exceptions mapped to HTTP status codes, so you can catch exactly the failure you care about. Catch the base class to handle everything.
Exception reference
¹ Auth failures return HTTP 403 (invalid_api_key_error), not 401 — see the auth status-code note.
Streaming support
Several APIs stream results instead of returning a single response. Here’s what each SDK supports:
- HTTP streaming returns an iterable/
BinaryResponseof raw audio bytes — see the HTTP Streaming guide. - WebSocket streaming uses the async clients (
AsyncSarvamAI/SarvamAIClient) with an event-driven connection — see the TTS WebSocket and STT WebSocket guides.
Versioning
The SDKs follow semantic versioning. Pin a version in production and review the API changelog before upgrading across a major version. Always develop against the latest release: