# 查询批量任务状态

> 通用

根据 batch 任务 ID 查询任务详情与状态。
当 `status` 变为 `completed` 后，可从响应中取 `output_file_id`（成功结果）
与 `error_file_id`（失败明细，若有）用于下载结果。

## 请求地址

`GET https://api.modelverse.cn/v1/batches/{batch_id}`

## 请求参数

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| batch_id | path | string | Yes | batch 任务 ID，即创建任务返回的 `id`。 |

## 响应

- **200** — batch 任务详情。
- **404** — 任务不存在或无权限访问。
- **default** — 错误响应。

## OpenAPI 定义

```json
{
  "openapi": "3.1.0",
  "x-language": "zh-CN",
  "info": {
    "title": "批量任务-查询批量任务状态接口文档",
    "version": "1.0.0",
    "description": "这是 ModelVerse 上 **OpenAI 兼容批量（Batch）异步任务**的第 4 步：轮询 batch 任务状态。\n创建任务后需轮询任务状态，直至进入终态（`completed` / `failed` / `expired` / `cancelled`）。\n接口路径为 `GET /v1/batches/{batch_id}`。建议轮询间隔不小于 30 秒。\n"
  },
  "servers": [
    {
      "url": "https://api.modelverse.cn",
      "description": "ModelVerse API endpoint."
    }
  ],
  "tags": [
    {
      "name": "批量异步任务",
      "description": "OpenAI 兼容的批量异步任务接口。"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/v1/batches/{batch_id}": {
      "get": {
        "tags": [
          "批量异步任务"
        ],
        "operationId": "retrieveBatch",
        "summary": "查询批量任务状态",
        "description": "根据 batch 任务 ID 查询任务详情与状态。\n当 `status` 变为 `completed` 后，可从响应中取 `output_file_id`（成功结果）\n与 `error_file_id`（失败明细，若有）用于下载结果。\n",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "batch_id",
            "in": "path",
            "required": true,
            "description": "batch 任务 ID，即创建任务返回的 `id`。",
            "schema": {
              "type": "string"
            },
            "example": "batch_abc123"
          }
        ],
        "responses": {
          "200": {
            "description": "batch 任务详情。",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BatchObject"
                },
                "examples": {
                  "completed": {
                    "summary": "任务已完成",
                    "value": {
                      "id": "batch_abc123",
                      "object": "batch",
                      "endpoint": "/v1/chat/completions",
                      "errors": null,
                      "input_file_id": "file-abc123",
                      "completion_window": "24h",
                      "status": "completed",
                      "output_file_id": "file-out456",
                      "error_file_id": "",
                      "created_at": 1781600010,
                      "in_progress_at": 1781600030,
                      "finalizing_at": 1781600300,
                      "completed_at": 1781600360,
                      "request_counts": {
                        "total": 2,
                        "completed": 2,
                        "failed": 0
                      }
                    }
                  },
                  "inProgress": {
                    "summary": "任务执行中",
                    "value": {
                      "id": "batch_abc123",
                      "object": "batch",
                      "endpoint": "/v1/chat/completions",
                      "errors": null,
                      "input_file_id": "file-abc123",
                      "completion_window": "24h",
                      "status": "in_progress",
                      "created_at": 1781600010,
                      "in_progress_at": 1781600030,
                      "request_counts": {
                        "total": 2,
                        "completed": 1,
                        "failed": 0
                      }
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "任务不存在或无权限访问。",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                },
                "examples": {
                  "error": {
                    "$ref": "#/components/examples/BatchError"
                  }
                }
              }
            }
          },
          "default": {
            "description": "错误响应。",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                },
                "examples": {
                  "error": {
                    "$ref": "#/components/examples/BatchError"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "API key",
        "description": "通过 HTTP 头 `Authorization` 携带 API Key，格式为 `Bearer <your_api_key>`。\n"
      }
    },
    "schemas": {
      "BatchObject": {
        "type": "object",
        "additionalProperties": true,
        "required": [
          "id",
          "object",
          "endpoint",
          "input_file_id",
          "completion_window",
          "status",
          "created_at"
        ],
        "properties": {
          "id": {
            "type": "string",
            "description": "batch 任务 ID。"
          },
          "object": {
            "type": "string",
            "const": "batch",
            "description": "对象类型，固定为 `batch`。"
          },
          "endpoint": {
            "type": "string",
            "description": "处理请求的目标接口。"
          },
          "errors": {
            "type": [
              "object",
              "null"
            ],
            "description": "错误信息对象；无错误时为 `null`。"
          },
          "input_file_id": {
            "type": "string",
            "description": "输入文件 ID。"
          },
          "completion_window": {
            "type": "string",
            "description": "任务完成时间窗口。"
          },
          "status": {
            "type": "string",
            "description": "任务状态：`validating`（校验输入文件）、`in_progress`（执行中）、\n`finalizing`（准备结果）、`completed`（已完成）、`failed`（失败）、\n`expired`（超时）、`cancelling`（取消中）、`cancelled`（已取消）。\n",
            "enum": [
              "validating",
              "in_progress",
              "finalizing",
              "completed",
              "failed",
              "expired",
              "cancelling",
              "cancelled"
            ]
          },
          "output_file_id": {
            "type": "string",
            "description": "结果文件 ID，任务完成后用于下载成功结果。"
          },
          "error_file_id": {
            "type": "string",
            "description": "错误明细文件 ID，部分请求失败时用于下载错误明细。"
          },
          "created_at": {
            "type": "integer",
            "format": "int64",
            "description": "创建时间戳（秒）。"
          },
          "in_progress_at": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "开始执行时间戳（秒）。"
          },
          "finalizing_at": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "开始准备结果时间戳（秒）。"
          },
          "completed_at": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "完成时间戳（秒）。"
          },
          "failed_at": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "失败时间戳（秒）。"
          },
          "expired_at": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "超时时间戳（秒）。"
          },
          "cancelled_at": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "取消完成时间戳（秒）。"
          },
          "request_counts": {
            "$ref": "#/components/schemas/BatchRequestCounts"
          },
          "metadata": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            },
            "description": "创建时传入的自定义元数据。"
          }
        }
      },
      "BatchRequestCounts": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "total": {
            "type": "integer",
            "description": "总请求数。"
          },
          "completed": {
            "type": "integer",
            "description": "已完成请求数。"
          },
          "failed": {
            "type": "integer",
            "description": "失败请求数。"
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "$ref": "#/components/schemas/Error"
          }
        }
      },
      "Error": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "message",
          "type"
        ],
        "properties": {
          "message": {
            "type": "string",
            "description": "错误说明。"
          },
          "type": {
            "type": "string",
            "description": "错误类型。",
            "examples": [
              "invalid_request_error"
            ]
          },
          "code": {
            "type": "string",
            "description": "机器可读的错误码。"
          },
          "param": {
            "type": "string",
            "description": "出错的参数名或请求 ID。"
          }
        }
      }
    },
    "examples": {
      "BatchError": {
        "summary": "标准 JSON 错误响应",
        "value": {
          "error": {
            "message": "batch not found",
            "type": "invalid_request_error",
            "code": "not_found",
            "param": "batch_id"
          }
        }
      }
    }
  }
}
```
