Skip to main content
Non-real-time speech recognition (Paraformer)

Paraformer non-real-time speech recognition Python SDK

Use the Paraformer Python SDK to transcribe audio and video files with the DashScope API.

This document applies only to the Chinese mainland (Beijing) region. To use the model, you must use an API key from the Chinese mainland (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.Replace {WorkspaceId} with your actual Workspace ID. The existing domain remains fully functional.
User guide:Non-real-time speech recognition

Prerequisites

You have activated the service and Obtain an API key. Please Configure API key as an environment variable instead of hardcoding it in your code to prevent security risks caused by code leakage.
When you need to provide temporary access to third-party applications or users, or when you want to strictly control high-risk operations such as accessing or deleting sensitive data, we recommend using temporary authentication tokens.Compared with long-term API Keys, temporary authentication tokens have a short validity period (60 seconds) and higher security, making them suitable for temporary call scenarios and effectively reducing the risk of API Key leakage.Usage: In your code, replace the API Key originally used for authentication with the obtained temporary authentication token.

Getting started

The core class (Transcription) supports two transcription approaches:
  • Async submission + sync wait: Submit a task and block until it completes and returns the result.
  • Async submission + async polling: Submit a task and poll for results at any time.

Async submission + sync wait

image
  1. Call the async_call method of the core class (Transcription) and set the request parameters.
    • The file transcription service processes tasks submitted through the API on a best-effort basis. After submission, a task enters the queued (PENDING) state. The queue time depends on the queue length and file duration and cannot be precisely estimated, but typically completes within a few minutes. Once processing begins, speech recognition completes at several hundred times the real-time speed.
    • After each task completes, the recognition result and URL download link are valid for 24 hours. After expiration, you cannot query the task or download results through the previously provided URL.
  2. Call the wait method of the core class (Transcription) to wait synchronously for the task to complete. Task statuses: PENDING, RUNNING, SUCCEEDED, FAILED. The wait call blocks during PENDING or RUNNING. When the task reaches SUCCEEDED or FAILED, wait returns the result. The wait method returns a TranscriptionResponse.
from http import HTTPStatus
from dashscope.audio.asr import Transcription
import json

# If you have not configured the API Key in an environment variable,
# uncomment the following line and replace "apiKey" with your own API Key.
# import dashscope
# dashscope.api_key = "apiKey"
# China (Beijing): Replace {WorkspaceId} with your actual workspace ID. The configuration varies by region.
dashscope.base_http_api_url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"

task_response = Transcription.async_call(
    model='paraformer-v2',
    file_urls=['{YOUR_AUDIO_URL}'],
    language_hints=['zh', 'en']  # The "language_hints" parameter only supports the paraformer-v2 model.
)

transcribe_response = Transcription.wait(task=task_response.output.task_id)
if transcribe_response.status_code == HTTPStatus.OK:
    print(json.dumps(transcribe_response.output, indent=4, ensure_ascii=False))
    print('transcription done!')

Async submission + async polling

image
  1. Call the async_call method of the Transcription core class and set the request parameters.
    • The file transcription service processes tasks submitted through the API on a best-effort basis. After submission, a task enters the queued (PENDING) state. The queue time depends on the queue length and file duration and cannot be precisely estimated, but typically completes within a few minutes. Once processing begins, speech recognition completes at several hundred times the real-time speed.
    • After each task completes, the recognition result and URL download link are valid for 24 hours. After expiration, you cannot query the task or download results through the previously provided URL.
  2. Poll the fetch method of the core class (Transcription) until the task completes. Stop polling when the status is SUCCEEDED or FAILED and process the result. The fetch method returns a TranscriptionResponse.
from http import HTTPStatus
from dashscope.audio.asr import Transcription
import json

# If you have not configured the API Key in an environment variable,
# uncomment the following line and replace "apiKey" with your own API Key.
# import dashscope
# dashscope.api_key = "apiKey"
# China (Beijing): Replace {WorkspaceId} with your actual workspace ID. The configuration varies by region.
dashscope.base_http_api_url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"

transcribe_response = Transcription.async_call(
    model='paraformer-v2',
    file_urls=['{YOUR_AUDIO_URL}'],
    language_hints=['zh', 'en']  # The "language_hints" parameter only supports the paraformer-v2 model.
)

while True:
    if transcribe_response.output.task_status == 'SUCCEEDED' or transcribe_response.output.task_status == 'FAILED':
        break
    transcribe_response = Transcription.fetch(task=transcribe_response.output.task_id)

if transcribe_response.status_code == HTTPStatus.OK:
    print(json.dumps(transcribe_response.output, indent=4, ensure_ascii=False))
    print('transcription done!')

Request parameters

Pass these parameters to the async_call method of the core class (Transcription).
ParameterTypeDefaultRequiredDescription
modelstr
YesThe model name for Paraformer audio and video file transcription. Supported models.
file_urlslist[str]
YesA list of URLs for audio and video file transcription. The HTTP and HTTPS protocols are supported. A single request supports only 1 URL.If audio files are stored in Alibaba Cloud OSS, the SDK does not support temporary URLs with the oss:// prefix.
vocabulary_idstr
NoThe custom vocabulary ID. Supported for v2 series models; requires language configuration. Disabled by default. Custom Vocabularies.
channel_idlist[int][0]NoSpecifies the audio track indexes to recognize in a multi-track audio file. Indexes start from 0. For example, [0] means recognizing the first track, and [0, 1] means recognizing both the first and second tracks simultaneously. If this parameter is omitted, only the first track is processed by default.
Each specified track is billed independently. For example, requesting [0, 1] for a single file incurs two separate charges.
disfluency_removal_enabledboolFalseNoFilters filler words. Disabled by default.
timestamp_alignment_enabledboolFalseNoEnables timestamp alignment. Disabled by default.
special_word_filterstr
NoSpecifies sensitive words to process during speech recognition and supports setting different processing methods for different sensitive words.If this parameter is not provided, the system uses the built-in sensitive word filtering logic, and words matching the Alibaba Cloud Model Studio sensitive word list in the recognition results will be replaced with * of equal length.If this parameter is provided, the following sensitive word processing strategies can be implemented:
  • Replace with *: Replace matching sensitive words with * of equal length.
  • Filter directly: Completely remove matching sensitive words from the recognition results.
The value of this parameter should be a JSON string with the following structure:
{
  "filter_with_signed": {
    "word_list": ["test"]
  },
  "filter_with_empty": {
    "word_list": ["start", "happen"]
  },
  "system_reserved_filter": true
}
JSON field descriptions:
  • filter_with_signed
    • Type: Object.
    • Required: No.
    • Description: Configures the list of sensitive words to be replaced with *. Matching words in the recognition results will be replaced with * of equal length.
    • Example: Using the JSON above, the speech recognition result for "Help me test this code" would be "Help me **** this code".
    • Internal fields:
      • word_list: An array of strings listing the sensitive words to be replaced.
  • filter_with_empty
    • Type: Object.
    • Required: No.
    • Description: Configures the list of sensitive words to be removed (filtered) from the recognition results. Matching words will be completely deleted.
    • Example: Using the JSON above, the speech recognition result for "The game is about to start, right?" would be "The game is about to, right?".
    • Internal fields:
      • word_list: An array of strings listing the sensitive words to be completely removed (filtered).
  • system_reserved_filter
    • Type: Boolean.
    • Required: No.
    • Default: true.
    • Description: Whether to enable the system's built-in sensitive word rules. When set to true, the system's built-in sensitive word filtering logic is also enabled, and words matching the Alibaba Cloud Model Studio sensitive word list in the recognition results will be replaced with * of equal length.
language_hintslist[str]["zh", "en"]NoSpecifies the language codes of the speech to be recognized.This parameter is only applicable to the paraformer-v2 model.Supported language codes:
  • zh: Chinese
  • en: English
  • ja: Japanese
  • yue: Cantonese
  • ko: Korean
  • de: German
  • fr: French
  • ru: Russian
diarization_enabledboolFalseNoAutomatic speaker diarization. Disabled by default.Only applicable to mono audio. Multi-channel audio does not support speaker diarization.When this feature is enabled, the recognition results will include a speaker_id field to distinguish different speakers.
If speaker diarization is enabled, it is recommended that the audio duration does not exceed 2 hours, otherwise recognition may fail or time out.
For an example of speaker_id, see Description of recognition results.
speaker_countint
NoReference speaker count. Integer from 2 to 100.Takes effect only when diarization_enabled is true.Determined automatically by default. Setting this parameter guides the algorithm but does not guarantee the exact count.

Response

TranscriptionResponse

A TranscriptionResponse contains task_id, task_status, and the execution result in the output property. See TranscriptionOutput.
The TranscriptionResponse returned by async_call does not include submit_time or scheduled_time.
{
    "status_code":200,
    "request_id":"251aceab-a6aa-9fc4-b7f7-0cc6d3e2a9f3",
    "code":null,
    "message":"",
    "output":{
        "task_id":"7d0a58a3-1dbe-4de9-8cff-5f48213128b0",
        "task_status":"PENDING"
    },
    "usage":null
}
To get submit_time and scheduled_time, use the wait() or fetch() methods instead of the async_call() return value directly. The TranscriptionResponse returned by wait() or fetch():
{
    "status_code":200,
    "request_id":"251aceab-a6aa-9fc4-b7f7-0cc6d3e2a9f3",
    "code":null,
    "message":"",
    "output":{
        "task_id":"7d0a58a3-1dbe-4de9-8cff-5f48213128b0",
        "task_status":"PENDING",
        "submit_time":"2025-02-13 16:55:08.573",
        "scheduled_time":"2025-02-13 16:55:08.592",
        "task_metrics":{
            "TOTAL":1,
            "SUCCEEDED":0,
            "FAILED":0
        }
    },
    "usage":null
}
Key parameters:

Parameter

Description

status_code

The HTTP request status code.

code

  • The outermost code can be ignored.

  • In output.results, the code field is the error code. Use it with the message field and refer to Error codes to troubleshoot.

message

  • The outermost message can be ignored.

  • The message under output.results is the error message. Use it with the code field and refer to error codes to troubleshoot.

task_id

The task ID.

task_status

The task status.

The four statuses are PENDING, RUNNING, SUCCEEDED, and FAILED.

When a task contains multiple subtasks, if any subtask succeeds, the entire task status is marked as SUCCEEDED. Check the subtask_status field to determine the result of each specific subtask.

results

The recognition results of the subtasks.

subtask_status

The subtask status.

The four statuses are PENDING, RUNNING, SUCCEEDED, and FAILED.

file_url

The URL of the audio file to be recognized.

transcription_url

The URL corresponding to the audio recognition result.

The recognition result is saved as a JSON file. Download the file from the URL in transcription_url or read its content via an HTTP request. For JSON file content details, see Recognition result description.

TranscriptionOutput

A TranscriptionOutput object is the output property of a TranscriptionResponse object, containing the task execution result.
  • PENDING status
  • RUNNING status
  • SUCCEEDED status
  • FAILED status
{
    "task_id":"f2f7c2fa-0cd9-4bb2-a283-27b26ee4bb67",
    "task_status":"PENDING",
    "submit_time":"2025-02-13 17:59:27.754",
    "scheduled_time":"2025-02-13 17:59:27.789",
    "task_metrics":{
        "TOTAL":1,
        "SUCCEEDED":0,
        "FAILED":0
    }
}
Important parameters:

Parameter

Description

code

The error code. Use with the message field. See Error codes.

message

The error message. Use with the code field. See Error codes.

task_id

The task ID.

task_status

The task status.

The four statuses are PENDING, RUNNING, SUCCEEDED, and FAILED.

When a task contains multiple subtasks, if any subtask succeeds, the entire task status is marked as SUCCEEDED. Check the subtask_status field to determine the result of each specific subtask.

results

The recognition results of the subtasks.

subtask_status

The subtask status.

The four statuses are PENDING, RUNNING, SUCCEEDED, and FAILED.

file_url

The URL of the audio file to be recognized.

transcription_url

The URL corresponding to the audio recognition result.

The recognition result is saved in a JSON file. Download the file from transcription_url or read its content via an HTTP request. For JSON content details, see Recognition result description.

Recognition result description

The recognition result is saved as a JSON file.
{
    "file_url":"{YOUR_AUDIO_URL}",
    "properties":{
        "audio_format":"pcm_s16le",
        "channels":[
            0
        ],
        "original_sampling_rate":16000,
        "original_duration_in_milliseconds":3834
    },
    "transcripts":[
        {
            "channel_id":0,
            "content_duration_in_milliseconds":3720,
            "text":"Hello world, this is the Alibaba speech laboratory.",
            "sentences":[
                {
                    "begin_time":100,
                    "end_time":3820,
                    "text":"Hello world, this is the Alibaba speech laboratory.",
                    "sentence_id":1,
                    "speaker_id":0, //This field is only displayed when automatic speaker diarization is enabled
                    "words":[
                        {
                            "begin_time":100,
                            "end_time":596,
                            "text":"Hello ",
                            "punctuation":""
                        },
                        {
                            "begin_time":596,
                            "end_time":844,
                            "text":"world",
                            "punctuation":", "
                        }
                        // Other content omitted here
                    ]
                }
            ]
        }
    ]
}
The key parameters are as follows:

Parameter

Type

Description

audio_format

string

The audio format of the source file.

channels

array[integer]

The audio track index information of the source file. Returns [0] for mono audio, [0, 1] for dual-track audio, and so on.

original_sampling_rate

integer

The sampling rate (Hz) of the audio in the source file.

original_duration

integer

The original audio duration (ms) of the source file.

channel_id

integer

The audio track index of the transcription result, starting from 0.

content_duration

integer

The duration (ms) of content identified as speech in the audio track.

The Paraformer speech recognition model service only transcribes and meters content identified as speech in the audio track, and bills accordingly. Non-speech content is neither metered nor billed. Typically, the speech content duration is shorter than the original audio duration. Since the determination of whether speech content exists is made by an AI model, there may be some deviation from the actual situation.

transcript

string

The paragraph-level speech transcription result.

sentences

array

The sentence-level speech transcription result.

words

array

The word-level speech transcription result.

begin_time

integer

The start timestamp (ms).

end_time

integer

The end timestamp (ms).

text

string

The speech transcription result.

speaker_id

integer

The index of the current speaker, starting from 0, used to distinguish different speakers.

This field is only displayed in the recognition results when speaker diarization is enabled.

punctuation

string

The predicted punctuation after the word (if any).

API reference

Core class (Transcription)

Import the Transcription class: from dashscope.audio.asr import Transcription.
Member methodMethod signatureDescription
async_call
@classmethod
def async_call(cls,
               model: str,
               file_urls: List[str],
               phrase_id: str = None,
               api_key: str = None,
               workspace: str = None,
               **kwargs) -> TranscriptionResponse
Asynchronously submits a speech recognition task.This method returns TranscriptionResponse.
wait
@classmethod
def wait(cls,
         task: Union[str, TranscriptionResponse],
         api_key: str = None,
         workspace: str = None,
         **kwargs) -> TranscriptionResponse
Blocks the current thread until the asynchronous task completes (status is SUCCEEDED or FAILED).This method returns TranscriptionResponse.
fetch
@classmethod
def fetch(cls,
          task: Union[str, TranscriptionResponse],
          api_key: str = None,
          workspace: str = None,
          **kwargs) -> TranscriptionResponse
Asynchronously queries the task execution result.This method returns a TranscriptionResponse.

Error codes

If you encounter errors, see Error codes for troubleshooting. If the issue persists, join the developer community to report the issue and provide the Request ID for further investigation. When a task contains multiple subtasks, as long as any subtask succeeds, the overall task status is marked as SUCCEEDED. You need to check the subtask_status field to determine the result of each subtask. Error response example:
{
    "task_id": "7bac899c-06ec-4a79-8875-xxxxxxxxxxxx",
    "task_status": "SUCCEEDED",
    "submit_time": "2024-12-16 16:30:59.170",
    "scheduled_time": "2024-12-16 16:30:59.204",
    "end_time": "2024-12-16 16:31:02.375",
    "results": [
        {
            "file_url": "{YOUR_AUDIO_URL}",
            "code": "InvalidFile.DownloadFailed",
            "message": "The audio file cannot be downloaded.",
            "subtask_status": "FAILED"
        }
    ],
    "task_metrics": {
        "TOTAL": 1,
        "SUCCEEDED": 0,
        "FAILED": 1
    }
}

More examples

Explore more examples on GitHub.

FAQ

Features

Q: Does it support Base64-encoded audio?

No. Base64-encoded audio is not supported. Only audio accessible via publicly accessible URLs is supported. Binary streams and direct local file recognition are not supported.

Q: How to provide audio files as publicly accessible URLs?

Generally, follow these steps (this provides a general approach; specifics vary by storage product. We recommend uploading audio to Alibaba Cloud OSS):
For example:
  • Object Storage Service (recommended):
    • Use a cloud provider's object storage service (such as Alibaba Cloud OSS) to upload audio files to a bucket and set them to public access.
    • Advantages: High availability, CDN acceleration support, easy management.
  • Web server:
    • Place audio files on a web server that supports HTTP/HTTPS access (such as Nginx or Apache).
    • Advantages: Suitable for small projects or local testing.
  • Content Delivery Network (CDN):
    • Host audio files on a CDN and access them through the CDN-provided URL.
    • Advantages: Accelerated file delivery, suitable for high-concurrency scenarios.
Upload audio files based on your chosen storage/hosting method, for example:
  • Object Storage Service:
    • Log in to the cloud provider's console and create a bucket.
    • Upload audio files and set file permissions to "public read" or generate temporary access links.
  • Web server:
    • Place audio files in the server's designated directory (such as /var/www/html/audio/).
    • Ensure files are accessible via HTTP/HTTPS.
For example:
  • Object Storage Service:
    • After uploading, the system automatically generates a public access URL (typically in the format https://<bucket-name>.<region>.aliyuncs.com/<file-name>).
    • If you need a more user-friendly domain, you can bind a custom domain and enable HTTPS.
  • Web server:
    • The file access URL is typically the server address plus the file path (such as https://your-domain.com/audio/file.mp3).
  • CDN:
    • After configuring CDN acceleration, use the CDN-provided URL (such as https://cdn.your-domain.com/audio/file.mp3).
In a public network environment, ensure the generated URL is accessible, for example:
  • Open the URL in a browser and check whether the audio file can be played.
  • Use tools (such as curl or Postman) to verify whether the URL returns a correct HTTP response (status code 200).
When using the SDK, if audio files are stored in Alibaba Cloud OSS, temporary URLs with the oss:// prefix are not supported. When using the RESTful API, if audio files are stored in Alibaba Cloud OSS, temporary URLs with the oss:// prefix are supported:
  • The temporary URL is valid for 48 hours and cannot be used after it expires. Do not use it in a production environment.
  • The API for obtaining an upload credential is limited to 100 QPS and does not support scaling out. Do not use it in production environments, high-concurrency scenarios, or stress testing scenarios.
  • For production environments, use a stable storage service such as OSS to ensure long-term file availability and avoid rate limiting issues.

Q: How long does it take to get recognition results?

After submission, the task enters a queued (PENDING) state. The queue time depends on the queue length and file duration and cannot be precisely estimated, but typically completes within a few minutes. Please wait patiently. Longer audio files require more processing time.

Troubleshooting

For code errors, see Error codes.

Q: What should I do if the recognition results are out of sync with the audio playback?

Set the request parameter timestamp_alignment_enabled to true to enable timestamp calibration, which synchronizes the recognition results with the speech playback.

Q: What should I do if the task returns an InvalidFile.DownloadFailed error?

Check whether the file URL contains spaces or other non-ASCII characters (such as Chinese characters). If the file name includes spaces (for example, my audio recording.mp4), replace each space with %20 to URL-encode the file name before passing it to the file_urls parameter.
Q: Unable to get results after continuous polling?
This may be due to rate limiting. Please wait patiently. If you need capacity expansion, join the developer community to apply.
Q: Why is there no recognition result (unable to recognize speech)?
  • Check whether the audio meets the requirements (format, sampling rate).
  • If you are using the paraformer-v2 model, check whether the language_hints setting is correct.
  • If none of the above resolves the issue, you can customize hot words to improve the recognition of specific words.

More questions

Visit the QA page on GitHub.
Text Generation
Image Generation
  • FAQ
Video Generation
Audio
Realtime API
Text Embedding
Model Production
Paraformer non-real-time speech recognition Python SDK - Alibaba Cloud Model Studio