Asynchronous task management API

Updated at:

Some Model Studio models (like image and video generation) use asynchronous invocation due to long processing times. The typical workflow is: create a task to get an ID, then query the result using that ID. Model Studio provides general-purpose task APIs to query individual results, check multiple task statuses in batch, and cancel queued tasks.

Prerequisites

You can call the asynchronous task APIs over HTTP.

Before you call the APIs, get and configure an API key, and then set the API key as an environment variable.

Query the result of an asynchronous task

API description: Queries the status and result of a task based on its task_id.

Rate limit: 20 QPS per Alibaba Cloud account (includes all RAM users).

Important

  • You can query all tasks under the Alibaba Cloud account that owns the current API key, including tasks submitted with any API key under that account. You cannot query tasks from other accounts.
  • Completed tasks are retained for 24 hours (check specific task API reference for exact period). After expiration, the system automatically deletes task data.

NoteThe number of result queries does not affect task execution. We recommend setting an appropriate query interval based on the task type:

  • For fast tasks (such as text embeddings), you can use a shorter query interval.
  • For slow tasks (such as image and video generation), use a longer query interval.
  • Avoid querying task status too frequently to prevent triggering the rate limit (20 QPS).

Request endpoint

GET https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/tasks/{task_id}

Request parameters

Parameter passing

Field

Type

Required

Description

Example

Header

Authorization

String

Yes

API key, for example: Bearer sk-xxx

Bearer sk-xxx

Path

task_id

String

Yes

The task ID to query.

a8532587-xxxx-xxxx-xxxx-0c46b17950d1

Response parameters

Field

Type

Description

Example

request_id

String

The unique ID for this request.

7574ee8f-xxxx-xxxx-xxxx-11c33ab46e51

output

Object

  • If the task succeeds, output contains the result object generated by the model. The content varies by task type.

  • If the task fails or partially fails, output returns the code and message fields that explain the failure reason.

  • For tasks with multiple subtasks, output may contain both successful subtask results and error messages for failed ones.

-

output.task_id

String

The ID of the queried task.

a8532587-xxxx-xxxx-xxxx-0c46b17950d1

output.task_status

String

The task status.

  • For multi-subtask tasks, the task succeeds if at least one subtask succeeds.

  • Failed subtasks show specific errors in the output.

Task status:

  • PENDING: The task is queued.

  • RUNNING: The task is being processed.

  • SUCCEEDED: The task completed successfully.

  • FAILED: The task failed.

  • UNKNOWN: The task does not exist or its status is unknown.

output.submit_time

String

The time the task was submitted.

2023-12-20 21:36:31.896

output.scheduled_time

String

The time the task was scheduled (when it started running).

2023-12-20 21:36:39.009

output.end_time

String

The time the task ended.

2023-12-20 21:36:45.913

output.code

String

The error code (returned only when the task fails).

-

output.message

String

The error message (returned only when the task fails).

-

output.task_metrics

Object

Task metrics including subtask status statistics.

{

"TOTAL": 4, // Total number of subtasks

"SUCCEEDED": 3, // Number of successful subtasks

"FAILED": 1 // Number of failed subtasks

}

usage

Object

The billing information for this request (varies by task).

"usage": {"image_count": 1}

Request example

curl

curl -X GET 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/tasks/73205176-xxxx-xxxx-xxxx-16bd5d902219' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"

Python SDK

The asynchronous task API is independent of the task type. The fetch, list, and cancel methods can be called on any asynchronous task resource (such as client.images.jobs, client.videos.jobs, client.audio.asr.jobs, etc.). They all request /api/v1/tasks under the hood and can query any task under the current account.

import os
import dashscope

# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx"
client = dashscope.Client(api_key=os.getenv('DASHSCOPE_API_KEY'), workspace="{WorkspaceId}", region="cn-beijing")

task_id = '73205176-xxxx-xxxx-xxxx-16bd5d902219'
job = client.images.jobs.fetch(task_id)
print(job.status, job.results)

dashscope CLI

export DASHSCOPE_API_KEY="your-api-key"
# Query a specific task status (video-synthesis as an example)
dashscope video-synthesis fetch <task_id>

NoteThe dashscope CLI currently only supports fetch/list/cancel task management for the video-synthesis subcommand; image-synthesis, transcription, and other subcommands do not yet support this. To query other task types, use the Python SDK or curl above.

ImportantThe SDK Expert interactive assistant can accomplish the same development and troubleshooting in natural language. See

DashScope SDK Expert.

For the complete region table, see Base URL overview.

NoteIf $DASHSCOPE_API_KEY is not set as an environment variable, replace it with your actual API key (format: Bearer sk-xxx).

Response example

{
    "request_id": "45ac7f13-xxxx-xxxx-xxxx-e03c35068d83",
    "output": {
        "task_id": "73205176-xxxx-xxxx-xxxx-16bd5d902219",
        "task_status": "SUCCEEDED",
        "submit_time": "2023-12-20 21:36:31.896",
        "scheduled_time": "2023-12-20 21:36:39.009",
        "end_time": "2023-12-20 21:36:45.913",
        "results": [
            {
                "url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/xxx1.png"
            },
            {
                "url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/xxx2.png"
            },
            {
                "url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/xxx3.png"
            },
            {
                "code": "DataInspectionFailed",
                "message": "Output data may contain inappropriate content.",
            }
        ],
        "task_metrics": {
            "TOTAL": 4,
            "SUCCEEDED": 3,
            "FAILED": 1
        }
    },
    "usage": {
        "image_count": 3
    }
}

Query the status of multiple asynchronous tasks

API description: Query multiple asynchronous tasks using various conditions. Check multiple task statuses in a single request.

Rate limit: 20 QPS per Alibaba Cloud account (includes all RAM users).

Important

  • You can query all tasks under the Alibaba Cloud account that owns the current API key, including tasks submitted with any API key under that account. You cannot query tasks from other accounts.
  • After the retention period expires, the system deletes the task, and its data becomes unqueryable.

Request endpoint

GET https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/tasks

Request parameters

Parameter passing

Field

Type

Required

Description

Example

Header

Authorization

String

Yes

The API key, in the format Bearer sk-xxx.

Bearer sk-xxx

Params

task_id

String

No

The task ID to query.

If you specify a task_id, the API returns only the status of that task; to query the statuses of multiple tasks in batch, do not pass this parameter.

a8532587-xxxx-xxxx-xxxx-0c46b17950d1

start_time

String

No

The query start time in the format YYYYMMDDhhmmss.

  • If the start time is not specified (start_time is empty), the system queries data from the 24 hours before the specified end time (end_time).

  • If neither the start time nor the end time is specified (both empty), the system queries data from the last 24 hours by default.

  • Make sure the gap between the start time and end time you set does not exceed 24 hours.

20230420193058 represents 19:30:58 on April 20, 2023.

end_time

String

No

The query end time in the format YYYYMMDDhhmmss.

  • If the end time is not specified (end_time is empty), the system queries data from the 24 hours after the specified start time (start_time).

  • Make sure the gap between the start time and end time you set does not exceed 24 hours.

model_name

String

No

The model name.

wanx-v1

status

String

No

The task status:

  • PENDING: The task is queued.

  • RUNNING: The task is being processed.

  • SUCCEEDED: The task completed successfully.

  • FAILED: The task failed.

  • CANCELED: The task was canceled successfully.

  • UNKNOWN: The task does not exist or its status is unknown.

page_no

Integer

No

The page number of the results to return. Default: 1.

-

page_size

Integer

No

The number of entries per page. Default: 10.

-

Response parameters

FieldTypeDescriptionExample

request_id

String

The unique ID for this request.

7574ee8f-xxxx-xxxx-xxxx-11c33ab46e51

data

Array

A list of query results.

"data": [
    {
        "api_key_id": "235",
        "caller_uid": "1808342417264262",
        "end_time": 1682527200093,
        "gmt_create": 1682514589152,
        "model_name": "paraformer-16k-1",
        "region": "cn-hangzhou",
        "request_id": "32b67b58-xxxx-xxxx-xxxx-230f0aee64d9",
        "start_time": 1682515862179,
        "status": "FAILED",
        "task_id": "cf52b16b-xxxx-xxxx-xxxx-17f9c211440c",
        "user_api_unique_key": "apikey:v1:audio:asr:transcription:paraformer-16k-1"
    }
]

data[].api_key_id

String

The API key ID.

data[].caller_parent_id

String

The Alibaba Cloud primary account ID.

data[].caller_uid

String

The Alibaba Cloud account ID.

data[].gmt_create

Long

The task creation time (milliseconds since the epoch).

data[].start_time

Long

The time the task started, in milliseconds since the epoch.

data[].end_time

Long

The time the task ended, in milliseconds since the epoch.

data[].region

String

The region. Example: cn-hangzhou

data[].request_id

String

The request ID for the task submission.

data[].status

String

The task status:

  • PENDING: The task is queued.
  • RUNNING: The task is being processed.
  • SUCCEEDED: The task completed successfully.
  • FAILED: The task failed.
  • CANCELED: The task was canceled successfully.
  • UNKNOWN: The task does not exist or its status is unknown.

data[].task_id

String

The task ID.

data[].user_api_unique_key

String

A unique API key generated from the model's API parameters at task submission.

data[].model_name

String

The model name.

page_no

Integer

The current page number.

"page_no": 1

page_size

Integer

The number of entries per page.

"page_size": 10

total_page

Integer

The total number of pages.

"total_page": 4

total

Integer

The total number of entries.

"total": 39

code

String

The error code returned when the call fails.

"code": "Throttling.RateQuota"

message

String

The error message returned when the call fails.

"message": "Requests rate limit exceeded, please try again later."

Request example

curl

curl -X GET 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/tasks?start_time=xxx&end_time=xxx&status=xxx' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"

Python SDK

import os
import dashscope

# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx"
client = dashscope.Client(api_key=os.getenv('DASHSCOPE_API_KEY'), workspace="{WorkspaceId}", region="cn-beijing")

page = client.images.jobs.list(
    start_time='20230420193000',
    end_time='20230420203000',
    status='RUNNING',
    page_no=1,
    page_size=10,
)
print(page.total, page.tasks)

The list method also supports filtering by model_name, api_key_id, region, and other conditions. The parameters correspond one-to-one with the HTTP API query parameters.

Notefetch, list, and cancel are supported on both the workspace-specific domain ({WorkspaceId}.{region}.maas.aliyuncs.com) and the general domain (dashscope.aliyuncs.com), and can be called across regions (such as cn-beijing, cn-shanghai, and cn-hangzhou). The API key must match the corresponding workspace.

dashscope CLI

export DASHSCOPE_API_KEY="your-api-key"
# List video synthesis tasks (supports --start-time/--end-time/--status/--model-name filters)
dashscope video-synthesis list --status RUNNING -s 10

NoteThe dashscope CLI currently only supports task management for the video-synthesis subcommand; to query other task types (image, transcription, etc.), use the Python SDK or curl above.

ImportantThe SDK Expert interactive assistant can accomplish the same development and troubleshooting in natural language. See

DashScope SDK Expert.

For the complete region table, see Base URL overview.

Response example

{
    "total": 2,
    "data": [
        {
            "api_key_id": "15xxxx",
            "caller_parent_id": "xxxxxxxxx",
            "caller_uid": "xxxxxxxxx",
            "gmt_create": 1745568428109,
            "model_name": "wanx2.1-kf2v-plus",
            "region": "cn-beijing",
            "request_id": "1abfc3c8-dd25-98da-ad0b-xxxxxx",
            "start_time": 1745568428138,
            "status": "RUNNING",
            "task_id": "50e2ccea-abc4-43d7-a0dc-xxxxxx",
            "user_api_unique_key": "apikey:v1:aigc:image2video:video-synthesis:wanx2.1-kf2v-plus"
        },
        {
            "api_key_id": "15xxxx",
            "caller_parent_id": "xxxxxxxxx",
            "caller_uid": "xxxxxxxxx",
            "end_time": 1745568302481,
            "gmt_create": 1745568293253,
            "model_name": "wanx2.1-t2i-turbo",
            "region": "cn-beijing",
            "request_id": "f6bf34d9-bf87-9e8b-9ed4-xxxxxx",
            "start_time": 1745568293273,
            "status": "SUCCEEDED",
            "task_id": "3c777dbc-8cc6-4d80-aa90-xxxxxx",
            "user_api_unique_key": "apikey:v1:aigc:text2image:image-synthesis:wanx2.1-t2i-turbo"
        }
    ],
    "total_page": 1,
    "page_no": 1,
    "request_id": "f6756b7e-d0bb-9b74-813a-xxxxxx",
    "page_size": 10
}

Cancel an asynchronous task

API description: Cancels an asynchronous task. Only tasks in PENDING state (queued and not yet started) can be canceled; tasks in other states cannot be canceled.

Rate limit: 20 QPS per Alibaba Cloud account (includes all RAM users).

Important

  • You can cancel any task under the Alibaba Cloud account that owns the current API key, including tasks submitted with any API key under that account. You cannot cancel tasks from other accounts.

Request endpoint

POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/tasks/{task_id}/cancel

Request parameters

Parameter passing

Field

Type

Required

Description

Example

Header

Authorization

String

Yes

The API key, in the format Bearer sk-xxx.

Bearer sk-xxx

Path

task_id

String

Yes

The ID of the task to cancel.

a8532587-xxxx-xxxx-xxxx-0c46b17950d1

Response parameters

Field

Type

Description

Example

request_id

String

The unique ID for this request.

7574ee8f-xxxx-xxxx-xxxx-11c33ab46e51

code

String

The error code returned when the call fails.

"code": "Throttling.RateQuota"

message

String

The error message returned when the call fails.

"message": "Requests rate limit exceeded, please try again later."

Request example

curl

curl -X POST 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/tasks/73205176-xxxx-xxxx-xxxx-16bd5d902219/cancel' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"

Python SDK

import os
import dashscope

# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx"
client = dashscope.Client(api_key=os.getenv('DASHSCOPE_API_KEY'), workspace="{WorkspaceId}", region="cn-beijing")

task_id = '73205176-xxxx-xxxx-xxxx-16bd5d902219'
job = client.images.jobs.cancel(task_id)
print(job)

Only tasks with PENDING status can be canceled. Calling on tasks in other states raises a DashScopeError exception (UnsupportedOperation).

dashscope CLI

export DASHSCOPE_API_KEY="your-api-key"
# Cancel a task (video-synthesis as an example, only PENDING status can be canceled)
dashscope video-synthesis cancel <task_id>

NoteThe dashscope CLI currently only supports cancel for the video-synthesis subcommand; to cancel other task types, use the Python SDK or curl above.

ImportantThe SDK Expert interactive assistant can accomplish the same development and troubleshooting in natural language. See

DashScope SDK Expert.

For the complete region table, see Base URL overview.

Response example

{
    "request_id": "45ac7f13-xxxx-xxxx-xxxx-e03c35068d83"
}

Error codes

HTTP status code

Error code

Error message

Description

400

UnsupportedOperation

Failed to cancel the task, please confirm if the task is in PENDING status.

Task cancellation failed. Confirm that the task status is PENDING.

Only tasks in PENDING status can be canceled; tasks in other statuses cannot be canceled.

FAQ

What does the DataInspectionFailed error code returned by a task query mean?

DataInspectionFailed indicates that the output data may contain inappropriate content and was blocked by content moderation. For asynchronous tasks with multiple subtasks (such as batch image generation), some subtasks may return this error code while other subtasks still complete normally. In this case, the overall task status may be SUCCEEDED, but task_metrics shows the number of failed subtasks.

How do I determine whether an asynchronous task has finished?

Check the task_status field returned by the task query API:

  • PENDING: The task is queued and has not started processing.
  • RUNNING: The task is being processed.
  • SUCCEEDED: The task completed successfully.
  • FAILED: The task failed. Check the code and message in output for the failure reason.

Does querying task status frequently affect task execution?

No. Task status query requests are independent of task execution, and the number of queries does not affect the progress or results of a task. However, note that the query API has a rate limit of 20 QPS. Overly frequent queries may trigger throttling and return an error response. We recommend setting an appropriate query interval based on the task type.