Sarvam Translate API Tutorial
Sarvam Translate API Tutorial
This tutorial demonstrates how to use the Sarvam Translate API to translate texts/paragraphs from one language to another. The API supports additional features such as transliteration (a type of conversion of a text from one script to another that involves swapping letters), output_script and gender.
Table of Contents
- Installation
- Authentication
- Basic Usage
- Translation Modes
- Advanced Features
- Error Handling
- Additional Resources
- Final Notes
1. Installation
Before you begin, ensure you have the necessary Python libraries installed. Run the following commands to install the required packages:
2. Authentication
To use the Sarvam API, you need an API subscription key. Follow these steps to set up your API key:
- Obtain your API key: If you don’t have an API key, sign up on the Sarvam AI Dashboard to get one.
- Replace the placeholder key: In the code below, replace “YOUR_SARVAM_AI_API_KEY” with your actual API key.
3. Basic Usage
3.1. Read the Document
We have two sample documents under the data
folder:
sample1.txt
contains an essay on The Impact of Artificial Intelligence on Society in English.sample2.txt
contains an essay on The Impact of Artificial Intelligence on Society in Hindi.
3.2. Split the text into chunks
Since the API has a restriction of 1000 characters per request, we need to split the text accordingly.
3.3. Setting up the API Endpoint
There are three main types of translations supported:
- English to Indic - Translating from English to Indian languages
- Indic to English - Converting Indian languages to English
- Indic to Indic - Translating between Indian languages
Indic to English Translation
Indic to Indic Translation
English to Indic Translation
4. Translation Modes
Translation Modes & Differences
- Formal – Highly professional, uses pure Hindi (e.g., “कुल राशि”, “देय है”). Suitable for official documents, legal papers, and corporate communication.
- Classic-Colloquial – Balanced mix of Hindi & English, slightly informal (e.g., “कुल जोड़”, “देना होगा”). Ideal for business emails, customer support, and semi-formal communication.
- Modern-Colloquial – Hinglish, casual, and direct (e.g., “Invoice total”, “due है”, “contact करो”). Best for chatbots, social media, and casual conversations.
Rule of Thumb:
- Use Formal for official content
- Use Classic-Colloquial for general communication
- Use Modern-Colloquial for everyday conversations
4.1. Classic Colloquial
4.2. Formal
4.3. Modern Colloquial
5. Advanced Features
5.1. Speaker Gender
The translation model supports Male and Female speaker options, which impact the tone and style of the output.
Female
Male
5.2. Numerals Format Feature
The numerals_format
parameter controls how numbers appear in the translation. It has two options:
-
International (Default) - Uses standard 0-9 numerals. Example: “मेरा phone number है: 9840950950.” Best for universally understood content, technical documents, and modern usage.
-
Native - Uses language-specific numerals. Example: “मेरा phone number है: ९८४०९५०९५०.” Ideal for traditional texts, cultural adaptation, and regional content.
When to Use What?
- Use International for wider readability and digital content
- Use Native for localized, heritage-focused, and print media content
Native
International
5.3. Output Script Feature
The output_script
parameter controls how the translated text is transliterated, i.e., how it appears in different scripts while keeping pronunciation intact.
Transliteration Options:
-
Default (null) – No transliteration applied. Example: “आपका Rs. 3000 का EMI pending है।” Best for modern, mixed-language content.
-
Roman – Converts the output into Romanized Hindi. Example: “aapka Rs. 3000 ka EMI pending hai.” Ideal for users who can speak but not read native scripts.
-
Fully-Native – Uses formal native script transliteration. Example: “आपका रु. 3000 का ई.एम.ऐ. पेंडिंग है।” Best for official documents and structured writing.
-
Spoken-Form-in-Native – Uses native script but mimics spoken style. Example: “आपका थ्री थाउजेंड रूपीस का ईएमअइ पेंडिंग है।” Ideal for voice assistants, conversational AI, and informal speech.
When to Use What?
- Default – For natural, mixed-language modern writing
- Roman – For users unfamiliar with native scripts
- Fully-Native – For formal, structured translations
- Spoken-Form-in-Native – For casual speech and voice applications
6. Error Handling
You may encounter these errors while using the API:
-
403 Forbidden (
invalid_api_key_error
)- Cause: Invalid API key.
- Solution: Use a valid API key from the Sarvam AI Dashboard.
-
429 Too Many Requests (
insufficient_quota_error
)- Cause: Exceeded API quota.
- Solution: Check your usage, upgrade if needed, or implement exponential backoff when retrying.
-
500 Internal Server Error (
internal_server_error
)- Cause: Issue on our servers.
- Solution: Try again later. If persistent, contact support.
-
400 Bad Request (
invalid_request_error
)- Cause: Incorrect request formatting.
- Solution: Verify your request structure and parameters.
7. Additional Resources
For more details, refer to our official documentation and we are always there to support and help you on our Discord Server:
- Documentation: docs.sarvam.ai
- Community: Join the Discord Community
8. Final Notes
- Keep your API key secure.
- Use clear audio for best results.
- Explore advanced features like diarization and translation.
Keep Building! 🚀