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
Use the same examples for the models below. Replace the model ID using this table.
| Model | Model ID in the example |
|---|---|
| Qwen-Audio-ASR-Streaming | qwen-audio-3.1-asr-flash-streamingFor microphone input, use qwen-audio-3.0-asr-flash-streaming. |
| Qwen-Audio-ASR-Message | qwen-audio-3.1-asr-flash-messageSupports only a sample rate of 16000 Hz. |
| Fun-ASR-Realtime | fun-asr-realtime |
The core resource (client.audio.asr) provides interfaces for both non-streaming and bidirectional streaming calls. Choose the call method that fits your needs:
- Non-streaming call (
recognize_file): Recognizes a local file. The SDK automatically sends the audio in chunks and drives the task to completion. Suitable for processing pre-recorded audio. - Bidirectional streaming call (
recognizesession): 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
Submit a single real-time speech recognition task and get the recognition result synchronously by passing in a local file.
Call the recognize_file method of the core resource (client.audio.asr) to bind the request parameters and pass in the local file path, then iterate the server events to get the recognition results.
import os
import dashscope
# The API Key differs between the Singapore and Beijing regions. Get an API Key: https://www.alibabacloud.com/help/model-studio/get-api-key
# If you have not configured the environment variable, replace the following line with your Model Studio API Key: api_key="sk-xxx"
client = dashscope.Client(
api_key=os.environ.get('DASHSCOPE_API_KEY'),
# The following is the configuration for the Singapore (ap-southeast-1) region. When calling, replace "{WorkspaceId}" with your actual workspace ID. The configuration differs by region.
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
# Recognize a local audio file: recognize_file automatically sends the audio in chunks and drives the task to completion
for event in client.audio.asr.recognize_file(
'{YOUR_AUDIO_FILE}',
model='qwen-audio-3.1-asr-flash-streaming',
format='wav',
sample_rate=16000):
payload = event.get('payload') or {}
sentence = (payload.get('output') or {}).get('sentence') or {}
if sentence.get('text') and not sentence.get('heartbeat'):
print('Recognition result:', sentence['text'])
Bidirectional streaming call
Submit a single real-time speech recognition task and stream the real-time recognition results by iterating events.
-
Start streaming speech recognition.
Call the
recognizemethod of the core resource (client.audio.asr) to create a recognition session and bind the request parameters, then enter thewithcontext (which automatically calls thestartmethod to establish the connection and submit the task) to start streaming speech recognition. -
Stream the audio.
Call the session's
send_audio_framemethod in a loop to send the binary audio stream to the server in segments. The stream is read from a local file or a device, such as a microphone.Sending and receiving must run concurrently: the receiver thread iterates the server events through the session's
events()method, and recognition results are returned to the client in real time.Send about 100 ms of audio per frame, and keep each frame between 1 KB and 16 KB.
-
End the task.
Call the session's
stopmethod to end speech recognition.This method only notifies the server that the input has ended; the receiver thread must keep consuming
events()until thetask-finishedevent to get the last recognition results and statistics. Errors at the WebSocket layer are raised asDashScopeErrorexceptions, which you can catch with try/except where you send or receive.
import os
import signal # for keyboard events handling (press "Ctrl+C" to terminate recording)
import sys
import dashscope
import pyaudio
from concurrent.futures import ThreadPoolExecutor
mic = None
stream = None
# Set recording parameters
sample_rate = 16000 # sampling rate (Hz)
channels = 1 # mono channel
dtype = 'int16' # data type
format_pcm = 'pcm' # the format of the audio data
block_size = 3200 # number of frames per buffer
# Real-time speech recognition event receiver
def receive_events(rec):
for event in rec.events():
payload = event.get('payload') or {}
sentence = (payload.get('output') or {}).get('sentence') or {}
if sentence.get('text') and not sentence.get('heartbeat'):
print('Recognition text: ', sentence['text'])
if sentence.get('sentence_end'):
print(
'Recognition sentence end, request_id:%s, usage:%s'
% (rec.request_id, rec.usage))
def signal_handler(sig, frame):
print('Ctrl+C pressed, stop recognition ...')
# Stop recognition
recognition.stop()
print('Recognition stopped.')
print(
'[Metric] requestId: {}, first package delay ms: {}, last package delay ms: {}'
.format(
recognition.request_id,
recognition.first_package_delay,
recognition.last_package_delay,
))
# Forcefully exit the program
sys.exit(0)
# main function
if __name__ == '__main__':
# The API Key differs between the Singapore and Beijing regions. Get an API Key: https://www.alibabacloud.com/help/model-studio/get-api-key
# If you have not configured the environment variable, replace the following line with your Model Studio API Key: api_key="sk-xxx"
client = dashscope.Client(
api_key=os.environ.get('DASHSCOPE_API_KEY'),
# The following is the configuration for the Singapore (ap-southeast-1) region. When calling, replace "{WorkspaceId}" with your actual workspace ID. The configuration differs by region.
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
# Call recognition service, you can customize the recognition parameters, like model, format,
# sample_rate
with client.audio.asr.recognize(
model='qwen-audio-3.0-asr-flash-streaming',
format=format_pcm,
# 'pcm'、'wav'、'opus'、'speex'、'aac'、'amr', you can check the supported formats in the document
sample_rate=sample_rate,
# support 8000, 16000
) as recognition:
# Open the microphone
mic = pyaudio.PyAudio()
stream = mic.open(format=pyaudio.paInt16,
channels=1,
rate=16000,
input=True)
with ThreadPoolExecutor(max_workers=1) as pool:
receiver = pool.submit(receive_events, recognition)
try:
# Send audio from the microphone until "Ctrl+C" is pressed
while stream:
data = stream.read(3200, exception_on_overflow=False)
recognition.send_audio_frame(data)
recognition.stop()
receiver.result()
finally:
# close must run inside the pool context to unblock the receiver
recognition.close()
stream.stop_stream()
stream.close()
mic.terminate()
stream = None
mic = None
import os
import time
import dashscope
from concurrent.futures import ThreadPoolExecutor
# The API Key differs between the Singapore and Beijing regions. Get an API Key: https://www.alibabacloud.com/help/model-studio/get-api-key
# If you have not configured the environment variable, replace the following line with your Model Studio API Key: api_key="sk-xxx"
client = dashscope.Client(
api_key=os.environ.get('DASHSCOPE_API_KEY'),
# The following is the configuration for the Singapore (ap-southeast-1) region. When calling, replace "{WorkspaceId}" with your actual workspace ID. The configuration differs by region.
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
from datetime import datetime
def get_timestamp():
now = datetime.now()
formatted_timestamp = now.strftime("[%Y-%m-%d %H:%M:%S.%f]")
return formatted_timestamp
# Real-time speech recognition event receiver
def receive_events(rec):
for event in rec.events():
payload = event.get('payload') or {}
sentence = (payload.get('output') or {}).get('sentence') or {}
if sentence.get('text') and not sentence.get('heartbeat'):
print(get_timestamp() + ' Recognition text: ', sentence['text'])
if sentence.get('sentence_end'):
print(get_timestamp() +
'Recognition sentence end, request_id:%s, usage:%s'
% (rec.request_id, rec.usage))
with client.audio.asr.recognize(
model='qwen-audio-3.1-asr-flash-streaming',
format='wav',
sample_rate=16000,
) as recognition:
with ThreadPoolExecutor(max_workers=1) as pool:
receiver = pool.submit(receive_events, recognition)
try:
audio_data: bytes = None
f = open("{YOUR_AUDIO_FILE}", 'rb')
if os.path.getsize("{YOUR_AUDIO_FILE}"):
# Read all the file data into the buffer at once
file_buffer = f.read()
f.close()
print("Start Recognition")
# Send 3200 bytes from the buffer at a time
buffer_size = len(file_buffer)
offset = 0
chunk_size = 3200
while offset < buffer_size:
# Calculate the size of the data chunk to send this time
remaining_bytes = buffer_size - offset
current_chunk_size = min(chunk_size, remaining_bytes)
# Extract the current data chunk from the buffer
audio_data = file_buffer[offset:offset + current_chunk_size]
# Send the audio data frame
recognition.send_audio_frame(audio_data)
# Update the offset
offset += current_chunk_size
# Add a delay to simulate real-time transmission
time.sleep(0.1)
recognition.stop()
receiver.result()
else:
raise Exception(
'The supplied file was empty (zero bytes long)')
finally:
# close must run inside the pool context to unblock the receiver
recognition.close()
print(
'[Metric] requestId: {}, first package delay ms: {}, last package delay ms: {}'
.format(
recognition.request_id,
recognition.first_package_delay,
recognition.last_package_delay,
))
Request parameters
Qwen-Audio-ASR-Streaming
Shared reference
See the current Qwen-Audio-ASR-Streaming Python SDK reference.
Set request parameters through the recognize/recognize_file methods of the core resource (client.audio.asr): model, format, sample_rate, and vocabulary_id are named parameters, and the remaining parameters (such as language_hints) are passed in through the parameters dict.
| Parameter | Type | Required | Description |
|---|---|---|---|
model | str | Yes | The model name. |
sample_rate | int | Yes | The sample rate in Hz. Any sample rate is supported. |
format | str | Yes | The audio format. Valid values:
Importantopus/speex: Must use Ogg encapsulation. wav: Must use PCM encoding. amr: Only the AMR-NB type is supported. |
keep_dialect | bool | No | Supported by qwen-audio-3.1-asr-flash-streaming only. 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. |
vad_model | str | No | Supported by qwen-audio-3.1-asr-flash-streaming only. 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 ( 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: |
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]. |
multi_threshold_mode_enabled | bool | No | Whether to enable multi-threshold mode. When enabled, it prevents VAD segmentation from producing segments that are too long. Default: False. |
punctuation_prediction_enabled | bool | No | Whether to automatically add punctuation to recognition results:
|
heartbeat | bool | No | Whether to enable heartbeat packets. Default: False.
Silent audio refers to content in an audio file or data 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 using a command-line tool like FFmpeg. This field requires SDK version 2.0.1 or later. |
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
|
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:
This is an advanced configuration parameter. Adjusting it can significantly affect recognition results. Recommendations:
|
special_word_filter | str | No | Specifies the sensitive words to process during speech recognition, and supports setting different processing methods for different sensitive words. For details, see Sensitive word filtering. |
The following parameters are passed in through the input keyword argument of the recognize/recognize_file methods.
| Parameter | Type | Required | Description |
|---|---|---|---|
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 a
ImportantContext messages of the ImportantWhen you pass context, the messages in NoteThis field requires SDK version 2.0.1 or later. |
Qwen-Audio-ASR-Message
Shared reference
See the current Qwen-Audio-ASR-Message Python SDK reference.
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
dashscope.aliyuncs.comto{WorkspaceId}.cn-beijing.maas.aliyuncs.com - Singapore: from
dashscope-intl.aliyuncs.comto{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com
Set request parameters through the recognize/recognize_file methods of the core resource (client.audio.asr): model, format, sample_rate, and vocabulary_id are named parameters, and other parameters such as language_hints are passed in through the parameters dictionary.
| 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. Only |
format | str | Yes | The audio format. Valid values:
Importantopus/speex: Must use Ogg encapsulation. wav: Must use PCM encoding. amr: Only the AMR-NB type is supported. |
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 ( |
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 ( 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
Pass it as a keyword argument with the same name. This parameter applies only to final results ( |
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.
Silent audio refers to content in an audio file or data 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 using a command-line tool like FFmpeg. This field was introduced in SDK 1.23.1 and is already included in version 2.0.1 and later. |
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:
This is an advanced configuration parameter. Adjusting it can significantly affect recognition results. Recommendations:
|
Pass the following parameters through the input keyword argument of the recognize/recognize_file methods.
| Parameter | Type | Required | Description |
|---|---|---|---|
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 a
ImportantContext messages of the ImportantWhen you pass context, the messages in NoteThis field was introduced in SDK 1.25.23 and is already included in version 2.0.1 and later. |
Fun-ASR-Realtime
Shared reference
See the current Fun-ASR-Realtime Python SDK reference.
Set request parameters through the recognize/recognize_file methods of the core resource (client.audio.asr): model, format, sample_rate, and vocabulary_id are named parameters, and the remaining parameters (such as max_sentence_silence and language_hints) are passed in through the parameters dictionary.
| Parameter | Type | Required | Description |
|---|---|---|---|
model | str | Yes | The model name. |
sample_rate | int | Yes | The sample rate, in Hz. Valid values: 8 kHz models support only 8000 Hz; other models support any sample rate. |
format | str | Yes | The audio format. Valid values:
Importantopus/speex: Must use Ogg encapsulation. wav: Must use PCM encoding. amr: Only the AMR-NB type is supported. |
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. |
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: 1300. Valid values: [200, 6000]. |
multi_threshold_mode_enabled | bool | No | Whether to enable multi-threshold mode. When enabled, it prevents VAD segmentation from producing segments that are too long. Default: False. |
punctuation_prediction_enabled | bool | No | Whether to automatically add punctuation to recognition results:
|
heartbeat | bool | No | Whether to enable heartbeat packets. Default: False.
Silent audio refers to content in an audio file or data 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 using a command-line tool like FFmpeg. This field requires SDK version 2.0.1 or later. |
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 1 value. If you set more, only the first value takes effect. Click to view the supported language codes
|
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:
This is an advanced configuration parameter. Adjusting it can significantly affect recognition results. Recommendations:
|
special_word_filter | str | No | Specifies the sensitive words to process during speech recognition, and supports setting different processing methods for different sensitive words. For details, see Sensitive word filtering. |
Pass the following parameters through the input keyword argument of the recognize/recognize_file methods.
| Parameter | Type | Required | Description |
|---|---|---|---|
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 Improve recognition accuracy. ImportantThe context parameter is supported only by the The dict must include a
ImportantContext messages of the ImportantWhen you pass context, the messages in NoteThis field requires SDK version 2.0.1 or later. |
API reference
Qwen-Audio-ASR-Streaming / Fun-ASR-Realtime
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. |
Qwen-Audio-ASR-Streaming
Key interfaces
Core resource (client.audio.asr)
Real-time speech recognition is mounted on the client.audio.asr resource: recognize creates a bidirectional streaming recognition session (Recognition), and recognize_file recognizes a local file in one shot.
| Member method | Method signature | Description |
|---|---|---|
recognize | | Creates a bidirectional streaming recognition session. Entering the with context automatically calls start to establish the connection and submit the task.Sending audio and consuming events must run concurrently (use it together with |
recognize_file | | A non-streaming call based on a local file: automatically sends the audio in chunks and drives the task to completion, yielding server events one by one. This method requires read permission on the file. |
start | | Starts speech recognition: establishes the connection, submits the task, and waits for task-started. It is called automatically when entering the with context; audio can be sent only after a successful start. |
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 event iteration ( |
stop | | Stops speech recognition: it only notifies the server that the input has ended. The receiver thread must keep consuming events() until the task-finished event. |
events / results | | Iterates server events: events() yields raw event dicts (recognition text is in payload.output.sentence); results() yields only recognition result frames and returns typed recognition result (RecognitionResult) objects (heartbeat frames filtered out). |
close | | Closes the recognition session. Called automatically when exiting the with context. |
request_id / first_package_delay / last_package_delay / last_response | | Gets the request_id, first-packet delay (latency from sending the first audio packet to receiving the first recognition result), and last-packet delay (time from sending the stop command to receiving the last recognition result). Use them after the task completes; last_response is the raw message most recently read successfully, which you can use to retrieve a task-failed error. They remain readable after the session is closed. |
Event iteration (events()/results())
During a bidirectional streaming call, the server returns key process information and data to the client through events. In the receiver thread, iterate the session's events() (raw event dicts) or results() (typed recognition result frames) to handle the information and data returned by the server.
# Iterate raw events: recognition text is in payload.output.sentence
def receive_events(rec):
for event in rec.events():
payload = event.get('payload') or {}
sentence = (payload.get('output') or {}).get('sentence') or {}
if sentence.get('text') and not sentence.get('heartbeat'):
print('Recognized text:', sentence['text'])
# Or iterate recognition result frames only (returns RecognitionResult, heartbeat frames filtered out)
def receive_results(rec):
for result in rec.results():
sentence = result.get_sentence()
if sentence and 'text' in sentence:
print('Recognized text:', sentence['text'])
| Event/iteration method | Output | Return value | Description |
|---|---|---|---|
|
| None | Read internally by start() and saved in last_response when it returns; not yielded again by events(). |
| event: server event dict | Iterator | Yields events whenever the server has a response. Recognition results are in payload.output.sentence and may be heartbeat or empty-text events, which the application can ignore as needed. Keep a single receiver thread consuming it; do not create multiple concurrent readers. |
| result: recognition result (RecognitionResult) | Iterator | Yields only recognition result frames (heartbeat frames filtered out); the iteration ends after all recognition results are returned. |
| DashScopeError exception | None | Raised in the sending/receiving thread when an error occurs; catch it with try/except. |
|
| None | Yielded by events() after the server completes all recognition; the iteration then ends. You must still read this event after calling stop(). |
Response
Recognition result (RecognitionResult)
RecognitionResult represents a single recognition result frame yielded by the session's results() iteration (usable in both bidirectional streaming calls and non-streaming calls; events() yields raw event dicts, so use results() if you need this typed view).
| Member method | Method signature | Description |
|---|---|---|
get_sentence | | Gets the current recognized sentence and its timestamp information. This method returns Dict[str, Any] (single-sentence information). For details, see Sentence (Sentence). |
request_id | | The request_id of the current event. |
is_sentence_end | | Determines whether the current sentence has ended. This method checks whether the end_time field in the single-sentence information returned by get_sentence() is None: a non-None end_time indicates that the sentence has ended. |
Qwen-Audio-ASR-Message
Key interfaces
Core resource (client.audio.asr)
The real-time speech recognition capability is mounted on the client.audio.asr resource: recognize creates a bidirectional streaming recognition session (Recognition), and recognize_file performs one-shot recognition on a local file.
| Member method | Method signature | Description |
|---|---|---|
recognize | | Creates a bidirectional streaming recognition session. Entering the Sending audio and consuming events must run concurrently (use it together with |
recognize_file | | A non-streaming call based on a local file: it automatically sends the audio in chunks, drives the task to completion, and yields server events one by one. This method requires read permission on the file. |
start | | Starts speech recognition: establishes the connection, submits the task, and waits for |
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 event iteration ( |
stop | | Stops speech recognition: it only notifies the server that the input is finished; the receiver thread must keep consuming |
events / results | | Iterates server events: |
close | | Closes the recognition session. Called automatically when the |
request_id / first_package_delay / last_package_delay / last_response | | Gets the request_id, the first-packet delay (the latency from sending the first audio packet to receiving the first recognition result), and the last-packet delay (the time from sending the |
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 2.0.1 or later.
def update_context(self, context: dict)
- When to call: After entering the
withcontext (which automatically callsstart) to start streaming recognition, and before callingstop. - Parameter:
contextis a dictionary containing thepayload.inputobject of acontinue-taskevent, including thecontextfield. Do not add an extrapayloadorinputwrapper. - Supported models and parameter constraints: See continue-task.
The following example uses an existing, started recognition instance.
payload_input = {
"context": [
{
"role": "user",
"content": [{"type": "input_text", "text": "Hello"}]
},
{
"role": "assistant",
"content": [{"type": "text", "text": "Hello, I am Qwen. How can I help you?"}]
}
]
}
recognition.update_context(context=payload_input)
Event iteration (events()/results())
During a Bidirectional streaming call, the server returns key process information and data to the client through events. In the receiver thread, iterate the session's events() (raw event dicts) or results() (typed recognition result frames) to handle the information and data returned by the server.
# Iterate raw events: the recognized text is in payload.output.sentence
def receive_events(rec):
for event in rec.events():
payload = event.get('payload') or {}
sentence = (payload.get('output') or {}).get('sentence') or {}
if sentence.get('text') and not sentence.get('heartbeat'):
print('Recognized text: ', sentence['text'])
# Or iterate only recognition result frames (returns RecognitionResult, heartbeat frames are filtered out)
def receive_results(rec):
for result in rec.results():
sentence = result.get_sentence()
if sentence and 'text' in sentence:
print('Recognized text: ', sentence['text'])
| Event/iteration method | Output | Return value | Description |
|---|---|---|---|
|
| None | Read internally by |
|
| Iterator | Yields events when the server responds. Recognition results are in |
|
| Iterator | Yields only recognition result frames (heartbeat frames are filtered out), and iteration ends after all recognition results are returned. |
|
| None | Raised in the sending/receiving thread when an error occurs; catch it with try/except. |
|
| None | Yielded by |
Response
Recognition result (RecognitionResult)
RecognitionResult represents a single recognition result frame yielded by the session's results() iteration (usable in both Bidirectional streaming calls and Non-streaming calls; events() yields raw event dicts—use results() if you need this typed view).
| Member method | Method signature | Description |
|---|---|---|
get_sentence | | Gets the current recognized sentence and its timestamp information. This method returns Dict[str, Any] (a single sentence). For details, see Sentence (Sentence). |
request_id | | The request_id of the current event. |
is_sentence_end | | Determines whether the current sentence has ended. This method checks whether the end_time field in the single-sentence dict returned by get_sentence() is None: a non-None end_time indicates that the sentence has ended. |
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. |
Fun-ASR-Realtime
Key interfaces
Core resource (client.audio.asr)
Real-time speech recognition is mounted on the client.audio.asr resource: recognize creates a bidirectional streaming recognition session (Recognition), and recognize_file recognizes a local file in one call.
| Member method | Method signature | Description |
|---|---|---|
recognize | | Creates a bidirectional streaming recognition session. Entering the with context automatically calls start to establish the connection and submit the task.Sending audio and consuming events must run concurrently (use it together with |
recognize_file | | A non-streaming call based on a local file: it automatically sends the audio in chunks and drives the task to completion, yielding server events one by one. This method requires read permission on the file. |
start | | Starts speech recognition: establishes the connection, submits the task, and waits for task-started. Automatically called when entering the with context. Audio can be sent only after a successful start. |
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 by iterating |
stop | | Stops speech recognition: it only notifies the server that the input is finished. The receiver thread must keep consuming events() until the task-finished event. |
events / results | | Iterates server events: events() yields raw event dicts (the recognition text is in payload.output.sentence); results() yields only recognition result frames, returning typed Recognition result (RecognitionResult) objects (heartbeat frames are filtered out). |
close | | Closes the recognition session. Automatically called when exiting the with context. |
request_id / first_package_delay / last_package_delay / last_response | | Gets the request_id, the first-packet delay (latency from sending the first audio packet to receiving the first recognition result), and the last-packet delay (time from sending the stop command to the last recognition result). Use them after the task completes. last_response is the last raw message successfully read, which you can use to retrieve task-failed errors. They remain readable after the session is closed. |
Event iteration (events()/results())
During a bidirectional streaming call, the server returns key process information and data to the client through events. In a receiver thread, iterate the session's events() (raw event dicts) or results() (typed recognition result frames) to handle the information and data returned by the server.
# Iterate raw events: the recognition text is in payload.output.sentence
def receive_events(rec):
for event in rec.events():
payload = event.get('payload') or {}
sentence = (payload.get('output') or {}).get('sentence') or {}
if sentence.get('text') and not sentence.get('heartbeat'):
print('Recognized text: ', sentence['text'])
# Or iterate only recognition result frames (returns RecognitionResult, heartbeat frames filtered out)
def receive_results(rec):
for result in rec.results():
sentence = result.get_sentence()
if sentence and 'text' in sentence:
print('Recognized text: ', sentence['text'])
| Event/iteration method | Output | Return value | Description |
|---|---|---|---|
| task-started event | None | Read internally by start() and saved in last_response when the start returns. It is not yielded again by events(). |
| event: server event dict | Iterator | Yields events when the server responds. The recognition result is in payload.output.sentence, which may be a heartbeat or empty-text event that applications can ignore as needed. Consume it from a single receiver thread and do not create multiple concurrent readers. |
| result: recognition result (RecognitionResult) | Iterator | Yields only recognition result frames (heartbeat frames are filtered out). The iteration ends after all recognition results are returned. |
| DashScopeError exception | None | Raised in the sending/receiving thread when an error occurs. You can catch it with try/except. |
| task-finished event | None | Yielded by events() when the server has completed all recognition, after which the iteration ends. You must still read this event after calling stop(). |
Response
Recognition result (RecognitionResult)
RecognitionResult represents a single recognition result frame yielded by iterating the session's results() (usable for both bidirectional streaming and non-streaming calls; events() yields raw event dicts—use results() if you need this typed view).
| Member method | Method signature | Description |
|---|---|---|
get_sentence | | Gets the current recognized sentence and its timestamp information. This method returns Dict[str, Any] (single-sentence information). For details, see Sentence (Sentence). |
request_id | | The request_id corresponding to the current event. |
is_sentence_end | | Determines whether the current sentence has ended. This method checks whether the end_time field in the single-sentence information returned by get_sentence() is None: a non-None end_time indicates that the sentence has ended. |
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.