LiveTranslate Python SDK
Call Qwen-LiveTranslate with the DashScope Python SDK for real-time speech translation.
Set these in the
Set these with
Set these in the
Import:
The server sends events to the client through callbacks. Inherit this class and implement its methods to handle them.
Import:
Record and translate audio from a microphone in real time:
Prerequisites
- Install the SDK (version 1.25.6 or later)
- Get an API key
- Install PyAudio for audio capture and playback:
pip install pyaudio
Request parameters
Set these in the OmniRealtimeConversation constructor:
Click to view sample code
Click to view sample code
| Parameter | Type | Required | Description |
|---|---|---|---|
model | str | Yes | Model name. Set to qwen3-livetranslate-flash-realtime. |
callback | OmniRealtimeCallback | Yes | Callback object that handles server events. |
url | str | No | Service endpoint: wss://dashscope-intl.aliyuncs.com/api-ws/v1/realtime. Defaults to the DashScope endpoint. |
api_key | str | No | API key for authentication. If this parameter is not provided, the SDK uses the DASHSCOPE_API_KEY environment variable. |
OmniRealtimeConversation.update_session:
Click to view sample code
Click to view sample code
| Parameter | Type | Required | Description |
|---|---|---|---|
output_modalities | List[MultiModality] | No | Output types. Default: [MultiModality.TEXT, MultiModality.AUDIO]. Valid values: [MultiModality.TEXT] (text only) or [MultiModality.TEXT, MultiModality.AUDIO] (text and audio). |
voice | str | No | Voice for audio output. Default: Cherry. See Supported voices. |
input_audio_transcription_model | str | No | Set to qwen3-asr-flash-realtime to get speech recognition results for the source language. |
translation_params | TranslationParams | No | Translation settings. |
TranslationParams constructor:
Click to view sample code
Click to view sample code
| Parameter | Type | Required | Description |
|---|---|---|---|
language | str | No | Target language code. Default: en. See Supported languages. |
corpus | TranslationParams.Corpus | No | Hotword settings to improve accuracy for specific terms. |
corpus.phrases | dict | No | Hotword map (key: source term, value: target translation). Example: {'Inteligencia Artificial': 'Artificial Intelligence'} |
Key interfaces
OmniRealtimeConversation class
Import: from dashscope.audio.qwen_omni import OmniRealtimeConversation
| Method signature | Server event (via callback) | Description |
|---|---|---|
def connect(self) -> None: | Server event: Session created; Server event: Session config updated | Connects to the server. |
def update_session(self, output_modalities: List[MultiModality], voice: str = None, translation_params: TranslationParams = None, **kwargs) -> None: | Server event: Session config updated | Updates session settings. Call right after connecting. If not called, defaults apply. See the OmniRealtimeConversation.update_session parameters. |
def end_session(self, timeout: int = 20) -> None: | session.finished: The server finishes translation and ends the session | Ends the session. The server finishes any remaining translation before closing. |
def append_audio(self, audio_b64: str) -> None: | None | Sends Base64-encoded audio to the input buffer. The server auto-detects speech boundaries and triggers translation. |
def close(self) -> None: | None | Stops the task and closes the connection. |
def get_session_id(self) -> str: | None | Returns the current session ID. |
def get_last_response_id(self) -> str: | None | Returns the last response ID. |
Callback interface (OmniRealtimeCallback)
The server sends events to the client through callbacks. Inherit this class and implement its methods to handle them.
Import: from dashscope.audio.qwen_omni import OmniRealtimeCallback
| Method signature | Parameters | Description |
|---|---|---|
def on_open(self) -> None: | None | Called when the WebSocket connection opens. |
def on_event(self, message: dict) -> None: | message: Server event | Called when a server event arrives. |
def on_close(self, close_status_code, close_msg) -> None: | close_status_code: Status code. close_msg: Log message. | Called when the WebSocket connection closes. |
Complete example
Record and translate audio from a microphone in real time:
Sample code for real-time translation from a microphone
Sample code for real-time translation from a microphone