This topic describes the parameters and interfaces of the Python SDK for the Qwen-Audio-3.1-ASR-Flash-Message real-time speech recognition model.
Prerequisites
- The service is activated and Obtain an API key. To guard against security risks caused by code leaks, Configure API key as an environment variable, rather than hard-coding it in your code.
- Install the latest DashScope SDK.
Quick start
The Recognition class provides interfaces for both non-streaming and bidirectional streaming calls. Choose the call method that fits your needs:
- Non-streaming call: Recognizes a local file and returns the complete result in a single response. Suitable for processing pre-recorded audio.
- Bidirectional streaming call: Recognizes an audio stream directly and outputs results in real time. The audio stream can come from an external device, such as a microphone, or be read from a local file. Suitable for scenarios that require immediate feedback.
- Non-streaming call
- Bidirectional streaming call
call to run recognition or translation and get the final Recognition result (RecognitionResult).Request parameters
Set request parameters through the constructor (init) of the Recognition class.
| Parameter | Type | Required | Description |
|---|---|---|---|
model | str | Yes | The model name. Set to qwen-audio-3.1-asr-flash-message. |
sample_rate | int | Yes | The sample rate, in Hz. Only16000 Hz is supported. |
format | str | Yes | The audio format. Valid values:
|
disfluency_removal_enabled | bool | No | Whether to filter filler words and polish the output. Defaults to false. Set to true to enable this feature. Pass as a keyword argument with the same name. |
intermediate_result_enabled | bool | No | Whether to return intermediate streaming results. Defaults to false. Set to true to return intermediate streaming results. Pass as a keyword argument with the same name. |
keep_dialect | bool | No | Defaults to false, which transcribes dialects into standard Chinese. Set to true to preserve dialect expressions. Pass as a keyword argument with the same name. See Client events for details.This parameter applies only to final results (sentence_end=true) and is not supported for intermediate results (sentence_end=false). |
vad_model | str | No | Set to near_meeting_16k (near-field) or far_field_meeting_16k (far-field, default). Pass as a keyword argument with the same name. See Client events for details. |
vocabulary_id | str | No | The ID of a precompiled hot word list. Generate this ID in advance by calling the create hot word list API. Pass the ID during recognition to use the hot words in the list.Suitable for scenarios where the vocabulary is known and relatively stable, and where you need to reuse the same word list across requests.For usage details, see Precompiled hotwords. |
vocabulary | dict | No | Instant hot words. Passed as key-value pairs, where the key is the hot word text (string) and the value is the hot word weight (integer). No hot word list needs to be created in advance. The weight ranges from [1, 5] or is set to 50: a value in [1, 5] makes the model more likely to output the word as the value increases; a value of 50 designates a super hot word, which greatly improves recall, but the number of super hot words cannot exceed 50.Suitable for temporary, session-level hot word optimization.When instant and precompiled hotwords are configured together, the system merges both sets. If the merged set contains more than 2000 hotwords, the system randomly selects 2000 to use. For usage details, see Instant hotwords.Example: |
language_hints | list[str] | No | The language of the audio to recognize. There is no default value; if not set, the model detects the language automatically.You can set up to 4 values. If you set more, only the first 4 take effect.
Click to view the supported language codes
sentence_end=true) and is not supported for intermediate results (sentence_end=false). |
max_sentence_silence | int | No | The VAD silence threshold for segmentation, in ms. When the silence after a segment of speech exceeds this threshold, the system determines that the sentence has ended. Default value: 800.Valid values: [200, 6000]. |
heartbeat | bool | No | Whether to enable heartbeat packets. Default: False.
|
speech_noise_threshold | float | No | The threshold for distinguishing speech from noise, used to adjust the sensitivity of Voice Activity Detection (VAD). Valid values: [-1.0, 1.0].Value descriptions:
|
callback | RecognitionCallback | No | Callback interface (RecognitionCallback). |
call or start method of the Recognition instance.
| Parameter | Type | Required | Description |
|---|---|---|---|
raw_input | dict | No | The input object used to pass in the conversation context. Context enhancement improves recognition accuracy for domain-specific terms. For usage, see Quick start. The dict must include acontext key whose value is a list of messages (list[dict]). Each message contains the following fields:
This field requires SDK version 1.25.23 or later. |
Key interfaces
Recognition class
Import Recognition with from dashscope.audio.asr import *.
| Member method | Method signature | Description |
|---|---|---|
call | A non-streaming call based on a local file. This method blocks the current thread until all audio is read, and requires read permission on the file. The recognition result is returned as aRecognitionResult object. | |
start | Starts speech recognition. A callback-based streaming real-time recognition. This method does not block the current thread. Use it together withsend_audio_frame and stop. | |
send_audio_frame | Pushes audio. Keep each pushed audio frame neither too large nor too small: about 100 ms per frame, between 1 KB and 16 KB. Recognition results are obtained through the on_event method of the Callback interface (RecognitionCallback). | |
stop | Stops speech recognition. Blocks until the server finishes recognizing all received audio, then ends the task. | |
get_last_request_id | Gets the request_id. Available after the constructor is called (the object is created). | |
get_first_package_delay | Gets the first-packet delay: the latency from sending the first audio packet to receiving the first recognition result. Use it after the task completes. | |
get_last_package_delay | Gets the last-packet delay: the time from sending the stop command to receiving the last recognition result. Use it after the task completes. | |
get_response | Gets the last message. Use it to retrieve a task-failed error. |
Update conversation context
Call update_context to update conversation context while a recognition task is running. The context is used to assist recognition of subsequent audio. This method requires DashScope Python SDK 1.27.5 or later.
- When to call: After starting streaming recognition with
startand before callingstop. - Parameter:
payload_inputis a dictionary containing thepayload.inputobject of acontinue-taskevent, including thecontextfield. Do not add an extrapayloadorinputwrapper. - Supported models and parameter constraints: See continue-task.
recognition instance.
Callback interface (RecognitionCallback)
During a Bidirectional streaming call, the server returns key process information and data to the client through callbacks. Implement the callback methods to handle the information and data returned by the server.
| Method | Parameter | Return value | Description |
|---|---|---|---|
| None | None | Called immediately after the connection to the server is established. | |
result: Recognition result (RecognitionResult) | None | Called when the server has a response. | |
| None | None | Called after all recognition results are returned. | |
result: Recognition result (RecognitionResult) | None | Called when an error occurs. | |
| None | None | Called after the server closes the connection. |
Response
Recognition result (RecognitionResult)
RecognitionResult represents the result of a single real-time recognition in a Bidirectional streaming call, or the result of a Non-streaming call.
| Member method | Method signature | Description |
|---|---|---|
get_sentence | Gets the current recognized sentence and its timestamp information. A callback returns a single sentence, so this method returns Dict[str, Any]. For details, see Sentence (Sentence). | |
get_request_id | Gets the request_id of the request. | |
is_sentence_end | Determines whether the given sentence has ended. This method checks whether the end_time field in sentence is None: a non-None end_time indicates that the sentence has ended. Call it as RecognitionResult.is_sentence_end(sentence), where sentence is the single-sentence dict returned by get_sentence(), not a boolean field on a Sentence instance. |
Sentence information (Sentence)
The members of the Sentence class are as follows:
| Parameter | Type | Description |
|---|---|---|
begin_time | int | Sentence start time, in ms. |
end_time | int | Sentence end time, in ms. |
text | str | Recognized text. |
words | A list of Word-level timestamp information (Word) | Word-level timestamp information. |
Word-level timestamp information (Word)
The members of the Word class are as follows:
| Parameter | Type | Description |
|---|---|---|
begin_time | int | Word start time, in ms. |
end_time | int | Word end time, in ms. |
text | str | The word. |
punctuation | str | The punctuation. |
Error codes
If you encounter errors, see Error codes for troubleshooting.
If the issue persists, join the developer community listed in the speech SDK sample repository to report your issue and provide the Request ID for further investigation.
FAQ
Features
Q: How do I keep the connection alive during long periods of silence?
Set the heartbeat request parameter to true, and keep sending silent audio to the server.
Silent audio is content in an audio file or stream that contains no sound signal. You can generate silent audio in several ways, such as using audio editing software like Audacity or Adobe Audition, or command-line tools like FFmpeg.
Q: How do I convert audio to a supported format?
Use FFmpeg. For more usage, see the FFmpeg official website.
Q: How do I recognize a local file (recording)?
There are two ways to recognize a local file:
-
Pass the local file path directly: This way returns the complete result only after recognition finishes, so it isn't suitable for scenarios that need immediate feedback.
See Non-streaming call, and pass the file path to the
callmethod of the Recognition class to recognize the recording directly. -
Convert the local file to a binary stream for recognition: This way recognizes the file while streaming results, so it suits scenarios that need immediate feedback.
See Bidirectional streaming call, and send the binary stream to the server for recognition through the
send_audio_framemethod of the Recognition class.
Troubleshooting
Q: Why can't the speech be recognized (no recognition result)?
-
Check that the audio format (
format) and sample rate (sampleRate/sample_rate) in the request parameters are correct and meet the parameter constraints. Common errors include:- The audio file has a .wav extension but is actually in MP3 format, while the
formatrequest parameter is set to wav (incorrect parameter setting). - The audio sample rate is 3600 Hz, but the
sampleRate/sample_raterequest parameter is set to 48000 (incorrect parameter setting).
- The audio file has a .wav extension but is actually in MP3 format, while the
- If none of the checks above reveal a problem, add custom hotwords to improve recognition of specific terms.