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).
- 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
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 |
| - |
output.task_id | String | The ID of the queried task. | a8532587-xxxx-xxxx-xxxx-0c46b17950d1 |
output.task_status | String | The task status.
| Task status:
|
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. |
|
usage | Object | The billing information for this request (varies by task). |
|
Request example
- curl
- Python SDK
- dashscope CLI
$DASHSCOPE_API_KEY is not set as an environment variable, replace it with your actual API key (format: Bearer sk-xxx).Response example
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).
Request endpoint
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
| 20230420193058 represents 19:30:58 on April 20, 2023. | |
end_time | String | No | The query end time in the format
| ||
model_name | String | No | The model name. | wanx-v1 | |
status | String | No | The task status:
| ||
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
| Field | Type | Description | Example |
|---|---|---|---|
| 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 | 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:
| |
| 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
- Python SDK
- dashscope CLI
Response example
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).
Request endpoint
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. |
|
message | String | The error message returned when the call fails. |
|
Request example
- curl
- Python SDK
- dashscope CLI
Response example
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 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 thecodeandmessageinoutputfor the failure reason.