Prerequisites
Requires DashScope Python SDK 1.25.11 or later.
Getting started
Request parameters
Set these parameters in the QwenTtsRealtime constructor:
Parameter | Type | Required | Description |
|---|---|---|---|
model | str | Yes | Model name (see Supported models). |
url | str | Yes | China (Beijing): Singapore: |
Parameter | Type | Required | Description |
|---|---|---|---|
voice | str | Yes | The voice for speech synthesis. For more information, see Supported voices. System voices and custom voices are supported:
|
language_type | str | No | Specifies the language of the synthesized audio. The default value is
|
mode | str | No | The interaction mode. Valid values:
|
format | str | No | The format of the audio output from the model. Supported formats:
Qwen-TTS-Realtime (see Supported models) supports only |
sample_rate | int | No | The sample rate of the audio output from the model, in Hz. Supported sample rates:
Qwen-TTS-Realtime (see Supported models) supports only 24000. |
speech_rate | float | No | The speech rate of the audio. A value of 1.0 is the normal speed. A value less than 1.0 is slower, and a value greater than 1.0 is faster. Default value: 1.0. Valid range: [0.5, 2.0]. Qwen-TTS-Realtime (see Supported models) does not support this parameter. |
volume | int | No | The volume of the audio. Default value: 50. Valid range: [0, 100]. Qwen-TTS-Realtime (see Supported models) does not support this parameter. |
pitch_rate | float | No | The pitch of the synthesized audio. Default value: 1.0. Valid range: [0.5, 2.0]. Qwen-TTS-Realtime (see Supported models) does not support this parameter. |
bit_rate | int | No | Specifies the bitrate of the audio in kbps. A higher bitrate results in better audio quality and a larger file size. This parameter is available only when the audio format ( Default value: 128. Valid range: [6, 510]. Qwen-TTS-Realtime (see Supported models) does not support this parameter. |
instructions | str | No | Sets the instructions. For more information, see Real-time speech synthesis - Qwen. Default value: None. The parameter is not active if not set. Length limit: The length cannot exceed 1600 tokens. Supported languages: Chinese and English only. Scope: This feature is available only for the Qwen3-TTS-Instruct-Flash-Realtime model series. |
optimize_instructions | bool | No | Specifies whether to optimize the Default value: False Behavior: When set to True, the system enhances and rewrites the Scenarios: Recommended for scenarios that require high-quality, fine-grained voice expression. Dependency: This parameter depends on the Scope: This feature is available only for the Qwen3-TTS-Instruct-Flash-Realtime model series. |
Key interfaces
QwenTtsRealtime class
Import: from dashscope.audio.qwen_tts_realtime import QwenTtsRealtime
| Method signature | Server response events (delivered via callback) | Description |
|---|---|---|
session.createdSession createdsession.updated Session configuration updated | Connect to the server. | |
session.updatedSession configuration updated | Update default session configurations. For parameter details, see the Request parameters.After connecting, the server returns default input and output configurations. Call this method immediately after connecting to update these defaults.The server validates parameters when it receives a session.update event. If validation fails, it returns an error; otherwise, it updates the session configuration. | |
| None | Append a text chunk to the cloud input buffer (temporary storage for text before submission).
| |
input_text_buffer.clearedClear text received by the server | Delete all text in the cloud buffer. | |
input_text_buffer.committedSubmit text and trigger speech synthesisresponse.output_item.added New output content addedresponse.content_part.added New content added to assistant messageresponse.audio.delta Incremental audio generated by the modelresponse.audio.done Audio generation completedresponse.content_part.done Streaming of audio content for assistant message completedresponse.output_item.done Streaming of entire output item for assistant message completedresponse.done Response completed | Submit all text in the cloud buffer and synthesize immediately. Returns an error if the buffer is empty.
| |
session.finishedResponse completed | Terminate the task. | |
| None | Close the connection. | |
| None | Get the session ID for the current task. | |
| None | Get the response ID of the most recent response. | |
| None | Get the delay before the first audio packet arrives. |
Callback interface (QwenTtsRealtimeCallback)
The server sends responses and data through callbacks. Implement callback methods to handle them.
Import: from dashscope.audio.qwen_tts_realtime import QwenTtsRealtimeCallback
| Method | Parameters | Return value | Description |
|---|---|---|---|
| None | None | Called when the server connection is established. | |
| message: Server response event. | None | Includes API call responses, model-generated text, and audio. See Server events. | |
| close_status_code: WebSocket close status code.close_msg: WebSocket close message. | None | Called after the server closes the connection. |