Skip to main content
Translate speech into another language as each turn completes. Real-time translation runs on LLM Gateway, configured through the llm_gateway connection parameter. When a turn ends, the server sends the final transcript to the gateway with your translation prompt and returns the translation on the same WebSocket as an LLMGatewayResponse message. You open one connection, receive the original transcript as usual, and receive the translation a moment later. There is no second API to call.

Quickstart

Pass an llm_gateway object with a translation prompt. The example below translates English into Spanish.
{{turn}} is a template variable. The server replaces it with the final transcript of each turn before the prompt reaches the model, so leave it exactly as written. See Prompt template variables for giving the model previous turns as context. The prompt field is shorthand for a single user message. The SDK config types take the equivalent messages form, and either works on the raw WebSocket.

Example output

The Turn messages arrive unchanged, in the spoken language. Each final turn is followed by an LLMGatewayResponse carrying its translation.

Reading the translation

Translations arrive as LLMGatewayResponse messages. The data field is the LLM Gateway chat completion for that turn, so the translated text is at data.choices[0].message.content.
Match each translation to its source turn by turn_order. The translation is produced in the background, so it does not delay the final Turn message, and translations for consecutive short turns can arrive out of order. If the gateway call fails or times out, no LLMGatewayResponse is sent for that turn. With a small model such as qwen3.5-4b-32k-fast, the translation typically lands within about 100 ms of the final turn. Only final turns are translated. Partial Turn messages stay in the spoken language.

Prompt template variables

The prompt is a template. The server renders it for each turn with these variables. To give the model context, add {{history_3}} to the prompt above the current turn. A turn joins the history once its own translation returns, so when turns come in quick succession the immediately preceding turn may not be included yet. History is the main lever for translation quality. A single sentence like “Yes, that one.” translates badly on its own and well with the question that preceded it. Three turns of history is a good default for conversations. Raise it for long monologues and lower it for a voice agent where each turn stands alone, since every turn of history adds prompt tokens.

Choosing a model and target language

The llm_gateway object accepts the same fields as the LLM Gateway chat completions API. The fields that matter for translation are listed below. To translate into another language, change the two mentions of Spanish in the prompt. To translate from a language other than English, change the source language in the prompt, or combine translation with multilingual transcription and tell the model to translate “from whatever language is spoken”.
Data retentionTranslation requests are sent to LLM Gateway with zero data retention, so the prompt and the translated text are not stored by the gateway.

Combine with other features

Translation runs after transcription, so it composes with the rest of the streaming pipeline.
  • Multilingual transcription to translate a conversation whose source language changes or is unknown in advance.
  • Speaker labels to show who said each translated line. The speaker_label is on the Turn message, so join it to the translation by turn_order.
  • Turn detection settings decide how often a translation is produced. Longer turns give the model more context per request and fewer, more complete translations.

Going further