Text Translation API

Translation Types

English to Indic

Translate from English to various Indian languages with support for different translation modes.

Indic to English

Convert Indian languages to English with high accuracy and natural output.

Indic to Indic

Translate between different Indian languages while preserving context and meaning.

Translation Modes

Formal

Highly professional, uses pure language forms. Ideal for official documents and legal papers.

Classic-Colloquial

Balanced mix of languages, slightly informal. Perfect for business emails and general communication.

Modern-Colloquial

Casual and direct style with mixed language. Best for chatbots and social media content.

Code Examples

1from sarvamai import SarvamAI
2
3client = SarvamAI(
4 api_subscription_key="YOUR_SARVAM_API_KEY"
5)
6
7response = client.text.translate(
8 input="Hello, how are you?",
9 source_language_code="en-IN",
10 target_language_code="hi-IN",
11 speaker_gender="Male"
12)
13
14print(response)

API Features

Translation Options
  • Multiple Indian languages support
  • Three translation modes
  • Gender-specific translations
  • Code-mixed text support
Output Formats
  • Multiple script options
  • Native/International numerals
  • Customizable formatting
  • Transliteration support
Advanced Features
  • Automatic language detection
  • Context preservation
  • Entity handling

API Response Format

FieldTypeDescription
request_idstringUnique identifier for the request
translated_textstringTranslated text in the target language
source_language_codestringDetected or provided source language (BCP-47 format)

Supported languages:

  • mayura:v1: bn-IN, en-IN, gu-IN, hi-IN, kn-IN, ml-IN, mr-IN, od-IN, pa-IN, ta-IN, te-IN
  • sarvam-translate:v1: All mayura languages + as-IN, brx-IN, doi-IN, kok-IN, ks-IN, mai-IN, mni-IN, ne-IN, sa-IN, sat-IN, sd-IN, ur-IN
1{
2 "request_id": "20241115_12345678-1234-5678-1234-567812345678",
3 "translated_text": "नमस्ते, आप कैसे हैं?",
4 "source_language_code": "en-IN"
5}

With Roman Script Output:

1{
2 "request_id": "20241115_12345678-1234-5678-1234-567812345678",
3 "translated_text": "namaste, aap kaise hain?",
4 "source_language_code": "en-IN"
5}

With Native Numerals:

1{
2 "request_id": "20241115_12345678-1234-5678-1234-567812345678",
3 "translated_text": "मेरा फोन नंबर है ९८४०९५०९५०",
4 "source_language_code": "en-IN"
5}

Auto-Detected Source Language:

1{
2 "request_id": "20241115_12345678-1234-5678-1234-567812345678",
3 "translated_text": "Hello, how are you?",
4 "source_language_code": "hi-IN"
5}

Error Responses

All errors return a JSON object with an error field containing details about what went wrong.

Error Response Structure

1{
2 "error": {
3 "message": "Human-readable error description",
4 "code": "error_code_for_programmatic_handling",
5 "request_id": "unique_request_identifier"
6 }
7}

Error Codes Reference

HTTP StatusError CodeWhen This HappensWhat To Do
400invalid_request_errorMissing required parameters or malformed requestCheck input, source_language_code, target_language_code
403invalid_api_key_errorAPI key is invalid, missing, or expiredVerify your API key in the dashboard
422unprocessable_entity_errorText too long or unsupported language pairKeep text under 1000 chars (mayura:v1) or 2000 chars (sarvam-translate:v1)
429insufficient_quota_errorAPI quota or rate limit exceededWait for reset or upgrade your plan
500internal_server_errorUnexpected server errorRetry the request; contact support if persistent

Example Error Response

1{
2 "error": {
3 "message": "Input text exceeds maximum length of 1000 characters for mayura:v1",
4 "code": "unprocessable_entity_error",
5 "request_id": "20241115_abc12345"
6 }
7}
1from sarvamai import SarvamAI
2from sarvamai.core.api_error import ApiError
3
4client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")
5
6try:
7 response = client.text.translate(
8 input="Hello, how are you?",
9 source_language_code="en-IN",
10 target_language_code="hi-IN"
11 )
12 print(response.translated_text)
13except ApiError as e:
14 if e.status_code == 400:
15 print(f"Bad request: {e.body}")
16 elif e.status_code == 403:
17 print("Invalid API key. Check your credentials.")
18 elif e.status_code == 422:
19 print(f"Invalid parameters: {e.body}")
20 elif e.status_code == 429:
21 print("Rate limit exceeded. Wait and retry.")
22 else:
23 print(f"Error {e.status_code}: {e.body}")

Check out our detailed API Reference to explore Translation and all available options.

Need help with translation? Contact us on discord for guidance.