Skip to main content

Non-real-time speech synthesis Qwen-Audio-TTS Python SDK reference

Use the Qwen-Audio-TTS non-real-time speech synthesis Python SDK to generate audio from text in non-streaming or streaming mode.

The features described in this document are available only in the China (Beijing) region.
Alibaba Cloud Model Studio has released a workspace-specific domain for the China (Beijing) region. The new dedicated domain delivers superior performance and higher stability for inference requests. We recommend migrating from dashscope.aliyuncs.com to {WorkspaceId}.cn-beijing.maas.aliyuncs.com.

Prerequisites

Core resource (client.audio.tts.http)

Resource path: client.audio.tts.http Description: HTTP-based speech synthesis. The synthesize method performs a non-streaming call (audio is returned as a URL), and the stream method performs a streaming call (audio data is returned chunk by chunk over SSE).

synthesize() / stream() - speech synthesis

Method signature:
def synthesize(self,
               text: str,
               *,
               model: str,
               voice: str,
               format: str = None,
               sample_rate: int = None,
               language_type: str = None,
               parameters: Dict = None,
               extra_input: Dict = None,
               timeout: float = None,
               ...) -> HttpSynthesis

def stream(self,
           text: str,
           *,
           model: str,
           voice: str,
           format: str = None,
           sample_rate: int = None,
           language_type: str = None,
           parameters: Dict = None,
           extra_input: Dict = None,
           timeout: float = None,
           ...) -> HttpSynthesisStream
Parameters (model, text, voice, format, sample_rate, and language_type are named parameters; pass other input parameters via the extra_input dictionary):
ParameterTypeRequiredDescription
modelstrYesThe speech synthesis model.
textstrYesThe text to synthesize.Supports SSML and LaTeX input. Replace the text content with the corresponding format.
voicestrYesThe voice for synthesis.Valid values:
formatstrNoThe audio format.Default value: mp3.Valid values:
  • mp3
  • pcm
  • wav
  • opus
sample_rateintNoThe audio sample rate in Hz.Valid values: 8000, 16000, 22050 (default), 24000, 44100, 48000.
volumeintNoThe volume level.Default value: 50.Valid values: [0, 100].
ratefloatNoThe speech rate.Default value: 1.0.Valid values: [0.5, 2.0].
pitchfloatNoThe pitch.Default value: 1.0.Valid values: [0.5, 2.0].
bit_rateintNoThe audio bitrate in kbps.Default value: 32.Valid values: [6, 510].
This parameter is supported only when format is set to opus.
enable_ssmlboolNoSpecifies whether to enable SSML. Set this to True when text uses SSML format. Default: False. For supported SSML tags and usage, see SSML. For the SSML usage restrictions (supported models, voices, and APIs), see Limitations.
word_timestamp_enabledboolNoSpecifies whether to enable word-level timestamps.Default: False.
  • True: Enabled.
  • False: Disabled.
Available only in streaming output mode. Cloned voices are supported. For supported system voices, see Qwen-Audio-TTS voice list.
seedintNoThe random seed for synthesis. Different seeds produce different outputs. With identical model version, text, voice, and other parameters, the same seed reproduces the same output.Default value: 0.Valid values: [0, 65535].
language_hintslistNo
  • This parameter is an array, but the current version processes only the first element. Pass a single value.
  • This parameter specifies the target language for speech synthesis and is unrelated to the language of the sample audio used in voice cloning. To set the source language for a voice cloning task, see the Voice Cloning API reference.
Specifies the target language for speech synthesis to improve output quality.Use this parameter when numbers, abbreviations, or symbols are not pronounced as expected, or when synthesis quality for secondary languages is poor. Examples:
  • A number isn't read as expected: "hello, this is 110" is read as "hello, this is one one zero" instead of "hello, this is yao-yao-ling"
  • A symbol isn't read correctly: "@" is read as "ai-te" instead of "at"
  • Minor language synthesis sounds unnatural
  • zh: Chinese
  • en: English
  • fr: French
  • de: German
  • ja: Japanese
  • ko: Korean
  • ru: Russian
  • pt: Portuguese
  • th: Thai
  • id: Indonesian
  • vi: Vietnamese
  • es: Spanish
  • it: Italian
  • ms: Malaysian
  • fil: Filipino
  • ar: Arabic
  • other: Other languages (supported by qwen-audio-3.1-tts-flash) Bulgarian, Dutch, Hungarian, Romanian, Danish, Croatian, Greek, Czech, Norwegian, Swedish, Hindi, Slovak, Finnish, Urdu, Serbian, Mexican Spanish, Lithuanian, Latvian, Turkish, Persian, Hebrew, Mongolian, Slovenian, Azerbaijani, Polish, Catalan, Khmer, Brazilian Portuguese, Lao, Georgian, Afrikaans, Nepali, Belarusian, Malayalam, Luxembourgish, Albanian, Tamil, Sinhala, Telugu, Estonian, Cebuano, Bosnian, Burmese, Armenian, Latin, Icelandic, Maltese, Swahili, Kazakh, Seychellois Creole, Ukrainian.
instructionstrNoAn instruction that controls the synthesis behavior, such as dialect, emotion, or role.For usage details, see Non-real-time speech synthesis.
enable_aigc_tagboolNoSpecifies whether to embed an AIGC watermark in the generated audio. When set to True, a watermark is embedded in audio files of supported formats (wav/mp3/opus).Default: False.
aigc_propagatorstrNoSets the ContentPropagator field in the AIGC watermark to identify the content distributor. Takes effect only when enable_aigc_tag is True.Default: Alibaba Cloud UID.
aigc_propagate_idstrNoSets the PropagateID field in the AIGC watermark to uniquely identify a specific distribution action. Takes effect only when enable_aigc_tag is True.Default: The request ID of the current speech synthesis request.
hot_fixdictNoText hot-fix configuration. Customizes the pronunciation of specified words or substitutes text before speech synthesis.Parameters:
  • pronunciation: Custom pronunciation. Provides pinyin for given words to correct cases where the default pronunciation is inaccurate.
  • replace: Text substitution. Replaces specified words with target text before speech synthesis; the substituted text is what gets synthesized.
Example:
"hot_fix": {
  "pronunciation": [
    {"weather": "tian1 qi4"}
  ],
  "replace": [
    {"today": "gold day"}
  ]
}
enable_markdown_filterboolNoWhether to enable Markdown filtering. When enabled, the service converts Markdown into text suitable for reading aloud before synthesis.Default: False. Set to True to enable.Use extra_input={"enable_markdown_filter": True} to enable it.
Return values:
  • Non-streaming mode (synthesize): Returns a result object (HttpSynthesis) with the following properties:
    • audio_url: The audio download URL (expires after a limited time).
    • audio_id: The audio ID.
    • expires_at: The URL expiration time.
    • usage: Usage information.
  • Streaming mode (stream): Returns an iterator (HttpSynthesisStream). Each element (HttpSynthesisChunk) contains the following properties:
    • audio: The audio binary data (bytes) for the current chunk, already decoded.
    • sentence: Sentence-level synthesis information (if available).
    • is_final: Indicates whether this is the final chunk (finish_reason == "stop").
    • usage: Usage information (carried on the final chunk).
    In streaming mode, the final chunk (is_final is True) additionally carries audio_url (the download URL of the complete audio), audio_id, and expires_at. When concatenating audio from the iterator, write only chunks with non-empty chunk.audio to avoid playing the same content twice.

Sample code

The following examples show non-streaming and streaming calls to Qwen-Audio-TTS speech synthesis. Set the DASHSCOPE_API_KEY environment variable before running the code.
Use a voice that matches the model and supports the target language. When switching models, update the voice accordingly. See Qwen-Audio-TTS voice list.
  • Non-streaming call
  • Streaming call
A non-streaming call uses the synthesize method, waits for synthesis to complete, and returns an audio URL. Download the audio file from this URL.
# -*- coding: utf-8 -*-
import os
import dashscope

# The following configuration is for the China (Beijing) region. Replace "{WorkspaceId}" with your actual workspace ID. Regions differ in configuration.
client = dashscope.Client(
    # If the API key is not configured as an environment variable, replace the next line with your API key: api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    workspace="{WorkspaceId}",
    region="cn-beijing",
)

# Non-streaming call, returns an audio URL
result = client.audio.tts.http.synthesize(
    "Today is a great day to build products that people love!",
    model="qwen-audio-3.0-tts-flash",  # When switching models, you must also switch to the corresponding voice for that version
    voice="longanhuan_v3.6",
    format="wav",
    sample_rate=24000,
)

# Get the audio URL
print(f"Audio URL: {result.audio_url}")
print(f"Audio ID: {result.audio_id}")
if result.audio_id:
    request_id = result.audio_id.removeprefix("audio_")
    print(f"Request ID: {request_id}")
print(f"Expiration time: {result.expires_at}")