Real-time TTS Python SDK
Prerequisites
Requires DashScope Python SDK 2.0.1 or later.
Getting started
Request parameters
Set these parameters in the connect method of client.audio.realtime:
Parameter | Type | Required | Description |
|---|---|---|---|
model | str | Yes | Model name (see Supported models). |
ws_url | str | No | Session endpoint. Pass |
update_session method of the session (named parameters are passed directly, and other parameters are passed through to the session object in the session.update event):
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:
|
response_format | str | No | The format of the audio output from the model. Supported formats:
|
sample_rate | int | No | The sample rate of the audio output from the model, in Hz. Supported sample rates:
|
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]. |
volume | int | No | The volume of the audio. Valid range: [0, 100]. |
pitch_rate | float | No | The pitch of the synthesized audio. Default value: 1.0. Valid range: [0.5, 2.0]. |
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]. |
Key interfaces
Session (RealtimeSession)
A real-time speech synthesis session is created by client.audio.realtime.connect(model=...). The connection is automatically established when entering the with context (or you can call the session's connect() method manually).
| Method signature | Server response events (delivered via events() iteration) | Description |
|---|---|---|
session.createdSession createdsession.updated Session configuration updated | Connect to the server. Automatically called when entering the with context. | |
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 | Send a raw event. Text input is performed with the input_text_buffer family of events: append a text chunk to the cloud input buffer ({"type": "input_text_buffer.append", "text": ...}). The buffer is temporary storage for text that you write to and submit later.
| |
input_text_buffer.clearedClear text received by the server | Send the {"type": "input_text_buffer.clear"} event to 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 | Send the {"type": "input_text_buffer.commit"} event to submit all text previously appended to the cloud buffer and synthesize it immediately. Returns an error if the buffer is empty.
| |
session.finishedResponse completed | Send the session.finish event to terminate the task. Keep consuming events() afterwards until the session.finished event. | |
| None | Close the connection. Automatically called when exiting the with context. | |
| 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. |
Event iteration (events())
The server returns response events and data as events. Iterate the session's events() in the receiving thread to handle server events; iteration ends after the session.finished event. error events are raised as DashScopeError exceptions, which you can catch with try/except.
| Method | Parameters | Return value | Description |
|---|---|---|---|
event: server response event (dict). | Iterator | Includes API call responses and model-generated text and audio. Iteration ends after the session.finished event. For more information, see Server events. | |
event: server response event (dict). | Event or None | Read one server event. Returns None after a normal close. |