Use the DashScope SDK for Python to call Qwen-LiveTranslate for real-time speech translation.
Prerequisites
- Install the SDK. Make sure that your DashScope SDK version is 1.25.6 or later.
- Obtain an API key.
Request parameters
-
Set the following parameters in the constructor of the
OmniRealtimeConversationclass.Click to view sample code
Parameter Type Required Description modelstrYes The name of the model to use. Set this to qwen3.5-livetranslate-flash-realtime(recommended).callbackOmniRealtimeCallbackYes A callback instance to handle server-side events. urlstrYes The service endpoint for real-time translation: - China (Beijing):
wss://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api-ws/v1/realtime - Singapore:
wss://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/realtime
{WorkspaceId}with your actual workspace ID. - China (Beijing):
-
Set the following parameters using the
update_sessionmethod of theOmniRealtimeConversationclass.Click to view sample code
Parameter
Type
Required
Description
output_modalitiesList[MultiModality]No
The output format for translation results.
Default:
[MultiModality.TEXT, MultiModality.AUDIO].Valid values:
[MultiModality.TEXT]: Outputs text only[MultiModality.TEXT, MultiModality.AUDIO]: Outputs text and audio
voicestrNo
The voice for audio output.
Default:
Qwen3.5-LiveTranslate-Flash-Realtime:
Tina
Valid values: Supported voices.
input_audio_transcription_modelstrNo
Set this parameter to
qwen3-asr-flash-realtimeto receive speech recognition results for the source language.translation_paramsTranslationParamsNo
Translation settings (target language and hotwords).
enable_turn_detectionboolNo
Whether to enable VAD (Voice Activity Detection).
Default value:
True, which enables VAD mode where the server automatically detects speech start/end and triggers translation.Set to
Falseto switch to Manual mode, where the client manually submits audio via thecommitmethod. For detailed parameter descriptions, see theturn_detectiondescription in the Client events document. -
Set the following parameters in the constructor of the
TranslationParamsclass.Click to view sample code
Parameter
Type
Required
Description
languagestrNo
The target language for translation.
Default:
en.Valid values: Supported languages.
corpusTranslationParams.CorpusNo
A hotword mapping to improve translation accuracy for specific terms.
corpus.phrasesdictNo
The hotword mapping table where keys are source terms and values are target translations.
Example:
{'Inteligencia Artificial': 'Artificial Intelligence'}
Key interfaces
OmniRealtimeConversation class
Import: from dashscope.audio.qwen_omni import OmniRealtimeConversation
| Method signature | Server-side response event (sent via callback) | Description |
|---|---|---|
Server-side eventSession createdServer-side event Session configuration updated | Connect to the server. | |
Session updatedSession updated | Update session configuration. Call immediately after connecting. If you do not call this method, default configurations apply. | |
session.finishedThe server completes the speech translation and ends the session | End the session. The server completes final translation processing. | |
| None | Send Base64-encoded audio chunks to the server. Speech detection and translation are automatic. | |
Server-side eventInput audio buffer committed | In Manual mode, submit the audio previously appended to the server buffer via the append_audio method. The server automatically starts generating translation responses upon receipt. In VAD mode, this method does not need to be called as the server commits automatically. | |
Server-side eventInput audio buffer cleared | Clear uncommitted audio data in the current server buffer. | |
| None | Stop the task and close the connection. | |
| None | Get the session ID. | |
| None | Get the last response ID. |
Callback interface (OmniRealtimeCallback)
Receive server events and data through callbacks.
Inherit this class and implement its methods to handle events from the server.
Import: from dashscope.audio.qwen_omni import OmniRealtimeCallback
| Method signature | Parameters | Description |
|---|---|---|
| None | Called when the WebSocket connection opens. | |
| message: Server-side event | Called when the server sends an event. | |
| close_status_code: The status code.close_msg: The log message when the WebSocket connection is closed. | Called when the WebSocket connection closes. |
Complete example
This example records microphone audio and translates it in real time.
Sample code for real-time translation from a microphone
Sample code for real-time translation from a microphone