SPB Git

Repository Public

live-translate.md 16.0 KB 389 lines markdown

1The Gemini Live API supports low-latency, real-time speech to speech translation between 70+ languages using the [`gemini-3.5-live-translate-preview`](https://ai.google.dev/gemini-api/docs/models/gemini-3.5-live-translate-preview) model. By configuring the Live API with translation settings, you can stream audio in one language and receive translated audio output in another language, enabling seamless real-time voice-to-voice translation.2[Try Live Translate in Google AI Studio](https://aistudio.google.com/live?model=gemini-3.5-live-translate-preview) [Clone the example app from GitHub](https://github.com/google-gemini/gemini-live-api-examples) [Use coding agent skills](https://ai.google.dev/gemini-api/docs/coding-agents#gemini-live-api-dev)34## Live Agent vs. Live Translation56While both use the Live API, the mental model for Live Translation is different from conversational real-time agent interactions.78| Live Agent | Live Translation |9|---|---|10| **The model acts as an assistant.** It listens, reasons, and takes actions on your behalf. | **The model acts as an interpreter.** It behaves as a real-time translator pipeline. |11| **Uses turn-based interactions.** Relies on pauses, intent detection, and handles interruptions. | **Uses continuous stream processing.** Translates as the speaker talks without waiting for turns. |12| **Supports tools and agents.** Native support for function calling, Google Search, and instructions. | **Supports translation only.** Pure low-latency translation; no support for tools or instructions. |13| **Fully multimodal.** Supports text, audio, video, and image inputs. | **Audio restricted.** Input is limited to audio to ensure strict real-time latency thresholds. |14| **Granular configuration.** Uses generation, speech, tools, and system instructions. | **Simplified configuration.** Set `target_language_code` and toggles like `echo_target_language`. |1516## Get started1718The following examples demonstrate how to initialize a client and connect to the Live API with a translation configuration.1920### Python2122    import asyncio23    from google import genai24    from google.genai import types2526    client = genai.Client()2728    model = "gemini-3.5-live-translate-preview"29    config = types.LiveConnectConfig(30        response_modalities=["AUDIO"],31        input_audio_transcription=types.AudioTranscriptionConfig(),32        output_audio_transcription=types.AudioTranscriptionConfig(),33        translation_config=types.TranslationConfig(34            target_language_code="pl",35            echo_target_language=True36        )37    )3839    async def main():40        async with client.aio.live.connect(model=model, config=config) as session:41            print("Session started with translation")42            # Start receiving the translated audio stream43            async for response in session.receive():44                if response.server_content:45                    if response.server_content.input_transcription:46                        print(f"Input transcript: {response.server_content.input_transcription.text}")47                    if response.server_content.output_transcription:48                        print(f"Output transcript: {response.server_content.output_transcription.text}")49                    if response.server_content.model_turn:50                        for part in response.server_content.model_turn.parts:51                            if part.inline_data:52                                audio_data = part.inline_data.data53                                # Play or process the translated audio chunk54                                print(f"Received audio chunk ({len(audio_data)} bytes)")5556    if __name__ == "__main__":57        asyncio.run(main())5859### JavaScript6061    import { GoogleGenAI, Modality } from '@google/genai';6263    const ai = new GoogleGenAI({});64    const model = 'gemini-3.5-live-translate-preview';65    const config = {66        responseModalities: [Modality.AUDIO],67        inputAudioTranscription: {},68        outputAudioTranscription: {},69        translationConfig: {70            targetLanguageCode: 'pl',71            echoTargetLanguage: true72        }73    };7475    async function main() {76      const session = await ai.live.connect({77        model: model,78        config: config,79        callbacks: {80          onopen: () => console.debug('Opened'),81          onmessage: (message) => {82            const content = message.serverContent;83            if (content?.inputTranscription) {84              console.log('Input transcript:', content.inputTranscription.text);85            }86            if (content?.outputTranscription) {87              console.log('Output transcript:', content.outputTranscription.text);88            }89            if (content?.modelTurn?.parts) {90              for (const part of content.modelTurn.parts) {91                if (part.inlineData) {92                  const audioData = part.inlineData.data;93                  // Play or process the translated audio chunk (base64 encoded)94                  console.debug(`Received audio chunk (${audioData.length} bytes)`);95                }96              }97            }98          },99          onerror: (e) => console.debug('Error:', e.message),100          onclose: (e) => console.debug('Close:', e.reason),101        },102      });103104      console.debug("Session started with translation");105    }106107    main();108109### WebSockets110111    const API_KEY = "YOUR_API_KEY";112    const MODEL_NAME = "gemini-3.5-live-translate-preview";113    const WS_URL = `wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent?key=${API_KEY}`;114115    const websocket = new WebSocket(WS_URL);116117    websocket.onopen = () => {118      console.log('WebSocket Connected');119120      const setupMessage = {121        setup: {122          model: `models/${MODEL_NAME}`,123          generationConfig: {124            responseModalities: ['AUDIO'],125            inputAudioTranscription: {},126            outputAudioTranscription: {},127            translationConfig: {128              targetLanguageCode: 'pl',129              echoTargetLanguage: true130            }131          }132        }133      };134      websocket.send(JSON.stringify(setupMessage));135    };136137    websocket.onmessage = (event) => {138      const response = JSON.parse(event.data);139      if (response.serverContent) {140        const content = response.serverContent;141        if (content.inputTranscription) {142          console.log('Input transcript:', content.inputTranscription.text, `(${content.inputTranscription.languageCode})`);143        }144        if (content.outputTranscription) {145          console.log('Output transcript:', content.outputTranscription.text, `(${content.outputTranscription.languageCode})`);146        }147        if (content.modelTurn?.parts) {148          for (const part of content.modelTurn.parts) {149            if (part.inlineData) {150              const audioData = part.inlineData.data;151              // Play or process the translated audio chunk (base64 encoded)152              console.debug(`Received audio chunk (${audioData.length} bytes)`);153            }154          }155        }156      }157    };158159## Sending audio160161To stream voice inputs for translation, you send raw, little-endian, 16-bit PCM audio.162163- **Input audio format**: Raw 16-bit PCM at 16kHz (mono, little-endian).164- **Output audio format**: Raw 16-bit PCM at 24kHz (mono, little-endian).165- **Chunk Size and Latency**: Send audio in chunks of 100ms.166167> [!NOTE]168> **Note:** Only audio input is supported for translation. Text input is not supported.169170The following examples show how to send audio chunks to the session.171172### Python173174    # Assuming 'chunk' is your raw PCM audio bytes175    await session.send_realtime_input(176        audio=types.Blob(177            data=chunk,178            mime_type="audio/pcm;rate=16000"179        )180    )181182### JavaScript183184    // Assuming 'chunk' is a Buffer of raw PCM audio185    session.sendRealtimeInput({186      audio: {187        data: chunk.toString('base64'),188        mimeType: 'audio/pcm;rate=16000'189      }190    });191192### WebSockets193194    // Assuming 'chunk' is a Buffer of raw PCM audio195    function sendAudioChunk(chunk) {196      if (websocket.readyState === WebSocket.OPEN) {197        const audioMessage = {198          realtimeInput: {199            audio: {200              data: chunk.toString('base64'),201              mimeType: 'audio/pcm;rate=16000'202            }203          }204        };205        websocket.send(JSON.stringify(audioMessage));206      }207    }208209## Configuration210211To enable translation, you must specify the `translationConfig` within the `generationConfig` during the session setup.212213### Setup message configuration214215The `generationConfig` supports the following fields to enable transcripts:216217- **`inputAudioTranscription`**: An object that, when present, enables the model to send text transcripts of the input audio.218- **`outputAudioTranscription`**: An object that, when present, enables the model to send text transcripts of the output (translated) audio.219220The `translationConfig` supports the following fields:221222- **`targetLanguageCode`** : The [BCP-47 language code](https://ai.google.dev/gemini-api/docs/live-api/live-translate#supported-languages) of the language you want the model to translate into (e.g., `"pl"` for Polish, `"es"` for Spanish). Defaults to `"en"`.223- **`echoTargetLanguage`** : A boolean indicating how input audio that is already in the target language should be handled. If set to `true`, the model will echo (parrot) input audio that is already in the target language. If set to `false`, the model will stay silent when the input speech is already in the target language. Defaults to `false`.224225Here is an example of the setup message structure:226227    "setup": {228        "model": "models/gemini-3.5-live-translate-preview",229        "generationConfig": {230          "responseModalities": [231            "AUDIO"232          ],233          "inputAudioTranscription": {},234          "outputAudioTranscription": {},235          "translationConfig": {236            "targetLanguageCode": "pl",237            "echoTargetLanguage": true238          }239        }240    }241242## Use ephemeral tokens in client-side applications243244For client-to-server applications, you can use [ephemeral tokens](https://ai.google.dev/gemini-api/docs/live-api/ephemeral-tokens) (currently in `v1beta`) to avoid exposing your API key.245246When using ephemeral tokens with Live Translation:2472481. You must use the `v1beta` endpoint.2492. **Locking configuration:** By default, you should specify the `translationConfig` in the token creation constraints on your server. This ensures the translation configuration is locked and cannot be tampered with by the client.2503. **Unlocking configuration:** If you want to be able to set the `translationConfig` on the client-side (for example, to let a user choose their own target language), you must omit it from the token creation request and set `"lock_additional_fields": []` instead. This will unlock `translationConfig` to be set on the client-side.251252### Create a constrained ephemeral token253254The following examples demonstrate how to create an ephemeral token with translation constraints.255256### Python257258    import datetime259    from google import genai260261    now = datetime.datetime.now(tz=datetime.timezone.utc)262263    client = genai.Client()264265    token = client.auth_tokens.create(266        config = {267            'uses': 1,268            'expire_time': now + datetime.timedelta(minutes=30),269            'live_connect_constraints': {270                'model': 'gemini-3.5-live-translate-preview',271                'config': {272                    'translation_config': {273                        'target_language_code': 'pl',274                        'echo_target_language': True275                    }276                }277            },278        }279    )280281### JavaScript282283    import { GoogleGenAI } from "@google/genai";284285    const client = new GoogleGenAI({});286    const expireTime = new Date(Date.now() + 30 * 60 * 1000).toISOString();287288    const token = await client.authTokens.create({289        config: {290            uses: 1,291            expireTime: expireTime,292            liveConnectConstraints: {293                model: 'gemini-3.5-live-translate-preview',294                config: {295                    responseModalities: ['AUDIO'],296                    inputAudioTranscription: {},297                    outputAudioTranscription: {},298                    translationConfig: {299                        targetLanguageCode: 'pl',300                        echoTargetLanguage: true301                    }302                }303            },304        },305    });306307### REST308309    curl -X POST "https://generativelanguage.googleapis.com/v1beta/auth_tokens" \310      -H "x-goog-api-key: ${GEMINI_API_KEY}" \311      -H "Content-Type: application/json" \312      -d '{313        "uses": 1,314        "expireTime": "YYYY-MM-DDTHH:MM:SSZ",315        "liveConnectConstraints": {316          "model": "models/gemini-3.5-live-translate-preview",317          "config": {318            "responseModalities": ["AUDIO"],319            "inputAudioTranscription": {},320            "outputAudioTranscription": {},321            "translationConfig": {322              "targetLanguageCode": "pl",323              "echoTargetLanguage": true324            }325          }326        }327      }'328329## Limitations330331- **Input Modalities**: Only audio input is supported for translation. Text input is not supported.332- **Voice Replication**: Voice replication can be inconsistent. Voices might shift after long pauses, assign the wrong gender based on how the speech starts, or get stuck on one voice during rapid multi-speaker conversations.333- **Language Detection** : Language detection struggles with heavy accents, similar languages (e.g., Spanish vs. Portuguese), or rapid language switches. **Note:** This should only impact the input transcript. Language codes and the final translation should still be accurate.334- **Background Audio**: The model is designed to filter out noise and music to produce clean speech, but not all background audio may be ignored.335- **Echo Target Language** : When `echoTargetLanguage: true`, background noise or music may introduce artifacts in the translated audio when input audio is already in the target language.336337## Supported languages338339The following languages are supported for Live Translation.340341| Language | BCP-47 Code | Language | BCP-47 Code |342|---|---|---|---|343| Afrikaans | af | Kazakh | kk |344| Akan | ak | Khmer | km |345| Albanian | sq | Kinyarwanda | rw |346| Amharic | am | Korean | ko |347| Arabic | ar | Lao | lo |348| Armenian | hy | Latvian | lv |349| Azerbaijani | az | Lithuanian | lt |350| Basque | eu | Macedonian | mk |351| Belarusian | be | Malay | ms |352| Bengali | bn | Malayalam | ml |353| Bulgarian | bg | Marathi | mr |354| Burmese (Myanmar) | my | Mongolian | mn |355| Catalan | ca | Nepali | ne |356| Chinese (Simplified) | zh-Hans | Norwegian | no, nb |357| Chinese (Traditional) | zh-Hant | Persian | fa |358| Croatian | hr | Polish | pl |359| Czech | cs | Portuguese (Brazil) | pt-BR |360| Danish | da | Portuguese (Portugal) | pt-PT |361| Dutch | nl | Punjabi | pa |362| English | en | Romanian | ro |363| Estonian | et | Russian | ru |364| Filipino | fil | Serbian | sr |365| Finnish | fi | Sindhi | sd |366| French | fr | Sinhala | si |367| Galician | gl | Slovak | sk |368| Georgian | ka | Slovenian | sl |369| German | de | Spanish | es |370| Greek | el | Sundanese | su |371| Gujarati | gu | Swahili | sw |372| Hausa | ha | Swedish | sv |373| Hebrew | he | Tamil | ta |374| Hindi | hi | Telugu | te |375| Hungarian | hu | Thai | th |376| Icelandic | is | Turkish | tr |377| Indonesian | id | Ukrainian | uk |378| Italian | it | Urdu | ur |379| Japanese | ja | Uzbek | uz |380| Javanese | jv | Vietnamese | vi |381| Kannada | kn | Zulu | zu |382383## What's next384385- Read the full Live API [Capabilities](https://ai.google.dev/gemini-api/docs/live-api/capabilities) guide.386- Read the [Get started with the SDK](https://ai.google.dev/gemini-api/docs/live-api/get-started-sdk) guide.387- Read the [Get started with WebSockets](https://ai.google.dev/gemini-api/docs/live-api/get-started-websocket) guide.388- Read the [Ephemeral tokens](https://ai.google.dev/gemini-api/docs/live-api/ephemeral-tokens) guide for secure authentication in client-to-server applications.389- Clone the [Live API examples](https://github.com/google-gemini/gemini-live-api-examples) from GitHub.
Files main
Menu

Theme