Qwen-LiveTranslate Python SDK - API reference
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.
ImportantAlibaba Cloud Model Studio has released workspace-specific domains for the China (Beijing) and Singapore regions. The new dedicated domains deliver superior performance and higher stability for inference requests. We recommend migrating to the new domains:
- China (Beijing): from
wss://dashscope.aliyuncs.comtowss://{WorkspaceId}.cn-beijing.maas.aliyuncs.com - Singapore: from
wss://dashscope-intl.aliyuncs.comtowss://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com
{WorkspaceId} is your workspace ID, which can be found on the Workspace Details page in the Alibaba Cloud Model Studio console. The existing domain remains fully functional.
Request parameters
-
Set the following parameters in the constructor of the
OmniRealtimeConversationclass.Click to view sample code
from dashscope.audio.qwen_omni import ( OmniRealtimeConversation, OmniRealtimeCallback, MultiModality, ) from dashscope.audio.qwen_omni.omni_realtime import TranslationParams class MyCallback(OmniRealtimeCallback): """Callback handler for real-time translation""" def __init__(self, conversation=None): self.conversation = conversation self.handlers = { 'session.created': self._handle_session_created, 'response.audio_transcript.done': self._handle_translation_done, 'response.audio.delta': self._handle_audio_delta, 'response.done': lambda r: print('======Response Done======'), 'input_audio_buffer.speech_started': lambda r: print('======Speech Start======'), 'input_audio_buffer.speech_stopped': lambda r: print('======Speech Stop======'), } def on_open(self): print('Connection opened') def on_close(self, code, msg): print(f'Connection closed, code: {code}, msg: {msg}') def on_event(self, response): try: handler = self.handlers.get(response['type']) if handler: handler(response) except Exception as e: print(f'[Error] {e}') def _handle_session_created(self, response): print(f"Session created: {response['session']['id']}") def _handle_translation_done(self, response): print(f"Translation result: {response['transcript']}") def _handle_audio_delta(self, response): # Process incremental audio data. audio_b64 = response.get('delta', '') # Decode the audio data for playback or to save it. conversation = OmniRealtimeConversation( model='qwen3.5-livetranslate-flash-realtime', # China (Beijing) region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region. url='wss://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api-ws/v1/realtime', callback=MyCallback(conversation=None) # Temporarily pass None. It will be injected later. ) # Inject self into the callback. conversation.callback.conversation = conversationParameter Type Required Description modelstrYes
The name of the model to use. Set this to
qwen3.5-livetranslate-flash-realtime(recommended).qwen3-livetranslate-flash-realtimeis a legacy model.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
Replace
{WorkspaceId}with your actual workspace ID. - China (Beijing):
-
Set the following parameters using the
update_sessionmethod of theOmniRealtimeConversationclass.Click to view sample code
# Set translation parameters translation_params = TranslationParams( language='en', # Target language corpus=TranslationParams.Corpus( phrases={ 'Inteligencia Artificial': 'Artificial Intelligence', 'Aprendizaje Automático': 'Machine Learning' } ) ) # Update session configuration conversation.update_session( output_modalities=[MultiModality.TEXT, MultiModality.AUDIO], voice='Tina', translation_params=translation_params, )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:
TinaQwen3-LiveTranslate-Flash-Realtime:
Cherry
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
translation_params = TranslationParams( language='en', # Target language code corpus=TranslationParams.Corpus( phrases={ 'Inteligencia Artificial': 'Artificial Intelligence', # Source phrase: Target translation 'Aprendizaje Automático': 'Machine Learning' } ) )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 |
|---|---|---|
|
| Connect to the server. |
| Session updated
| Update session configuration. Call immediately after connecting. If you do not call this method, default configurations apply. |
|
| End the session. The server completes final translation processing. |
| None | Send Base64-encoded audio chunks to the server. Speech detection and translation are automatic. |
|
| In Manual mode, submit the audio previously appended to the server buffer via the |
|
| 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
import os
import sys
import base64
import signal
import pyaudio
from dashscope.audio.qwen_omni import (
OmniRealtimeConversation,
OmniRealtimeCallback,
MultiModality,
)
from dashscope.audio.qwen_omni.omni_realtime import TranslationParams
class Callback(OmniRealtimeCallback):
"""Callback handler class for real-time translation"""
def __init__(self, speaker):
self.speaker = speaker
def on_open(self):
print("[Connection established]")
def on_close(self, code, msg):
print(f"[Connection closed] code: {code}, msg: {msg}")
def on_event(self, response):
event_type = response.get("type", "")
if event_type == "input_audio_buffer.speech_started":
print("====== Speech input detected ======")
elif event_type == "input_audio_buffer.speech_stopped":
print("====== Speech input ended ======")
elif event_type == "conversation.item.input_audio_transcription.completed":
print(f"[Original text] {response.get('transcript', '')}")
elif event_type == "response.audio_transcript.done":
print(f"[Translation result] {response.get('transcript', '')}")
elif event_type == "response.audio.delta":
audio_b64 = response.get("delta", "")
if audio_b64:
self.speaker.write(base64.b64decode(audio_b64))
elif event_type == "error":
print(f"[Error] {response.get('error', {}).get('message', '')}")
def main():
# Check for the API key.
if not os.environ.get("DASHSCOPE_API_KEY"):
print("Set the DASHSCOPE_API_KEY environment variable.")
sys.exit(1)
# Initialize PyAudio.
pya = pyaudio.PyAudio()
# Initialize the speaker for playing back the translated audio.
speaker = pya.open(
format=pyaudio.paInt16,
channels=1,
rate=24000,
output=True,
frames_per_buffer=2400
)
# Initialize the microphone for capturing speech input.
mic = pya.open(
format=pyaudio.paInt16,
channels=1,
rate=16000,
input=True,
frames_per_buffer=1600
)
# Create a callback instance.
callback = Callback(speaker=speaker)
# Create a real-time session.
conversation = OmniRealtimeConversation(
model="qwen3.5-livetranslate-flash-realtime",
# China (Beijing) region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
url="wss://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api-ws/v1/realtime",
callback=callback
)
# Connect to the server.
conversation.connect()
# Configure translation parameters.
translation_params = TranslationParams(
language="en", # Target language for translation: English
corpus=TranslationParams.Corpus(
phrases={
"Source Term 1": "Target Translation 1",
"Source Term 2": "Target Translation 2"
}
)
)
# Update the session configuration.
conversation.update_session(
output_modalities=[MultiModality.TEXT, MultiModality.AUDIO],
input_audio_transcription_model="qwen3-asr-flash-realtime",
voice="Tina",
translation_params=translation_params,
)
# Register the exit signal handler.
def on_exit(sig, frame):
print("\n[Exiting...]")
mic.stop_stream()
mic.close()
speaker.stop_stream()
speaker.close()
pya.terminate()
conversation.end_session()
conversation.close()
sys.exit(0)
signal.signal(signal.SIGINT, on_exit)
print("[Starting real-time translation] Speak into the microphone. Press Ctrl+C to exit.")
# Continuously capture and send microphone audio.
while True:
audio_data = mic.read(1600, exception_on_overflow=False)
conversation.append_audio(base64.b64encode(audio_data).decode("ascii"))
if __name__ == "__main__":
main()