> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 获取 Replicate prediction

> 通过任务 ID 查询 GET /replicate/v1/predictions/{id}，获取 Replicate prediction 详情以及图像生成工作流中的实时进度。

当你已经拥有一个 Replicate prediction id 后，请使用此端点。它会报告当前任务状态，并在 prediction 完成时返回输出 URL。

## 先检查这些字段

* 使用 `status` 查看 prediction 是否仍在运行或已经完成
* 使用 `output` 获取已生成资源的 URL
* 使用 `error` 查看提供商侧故障
* 当你需要执行耗时或图像数量等详细信息时，查看 `metrics`

## 轮询模式

<Steps>
  <Step title="先创建 prediction">
    从 [创建 Replicate prediction](./create-predictions-general) 开始。
  </Step>

  <Step title="按 prediction id 轮询">
    持续查询，直到 `status` 变为终态，并且 `output` 已填充，或者返回提供商错误。
  </Step>

  <Step title="持久化已完成的资源">
    将返回的资源 URL 视为交付 URL；如果你需要长期保留，请将它们转存到你自己的存储中。
  </Step>
</Steps>


## OpenAPI

````yaml api/openapi/image/replicate/get-replicate-query.openapi.json GET /replicate/v1/predictions/{id}
openapi: 3.1.0
info:
  title: Replicate Query API
  version: 1.0.0
  description: >-
    Query a Replicate prediction created through CometAPI and read the final
    output URLs when the prediction completes.
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /replicate/v1/predictions/{id}:
    get:
      summary: Query a Replicate prediction
      description: >-
        Use the prediction id returned by the create endpoint. Keep polling
        until `output` is populated or `error` is not null.
      operationId: replicate_query
      parameters:
        - name: id
          in: path
          required: true
          description: Prediction id returned by the create endpoint.
          schema:
            type: string
      responses:
        '200':
          description: Current prediction state.
          content:
            application/json:
              schema:
                type: object
                required:
                  - id
                  - input
                  - model
                  - output
                  - status
                  - created_at
                  - data_removed
                properties:
                  id:
                    type: string
                  logs:
                    type: string
                  urls:
                    type: object
                    properties:
                      get:
                        type: string
                      cancel:
                        type: string
                      stream:
                        type: string
                  error:
                    type:
                      - string
                      - 'null'
                  input:
                    type: object
                  model:
                    type: string
                  output:
                    type:
                      - array
                      - 'null'
                    items:
                      type: string
                  status:
                    type: string
                  metrics:
                    type: object
                    properties:
                      image_count:
                        type: integer
                      predict_time:
                        type: number
                  version:
                    type: string
                  created_at:
                    type: string
                  started_at:
                    type:
                      - string
                      - 'null'
                  completed_at:
                    type:
                      - string
                      - 'null'
                  data_removed:
                    type: boolean
              example:
                id: <prediction_id>
                model: black-forest-labs/flux-schnell
                status: succeeded
                output:
                  - https://replicate.delivery/xezq/.../out-0.webp
                error: null
      x-codeSamples:
        - lang: Shell
          label: Default
          source: >
            PREDICTION_ID="<prediction_id>"


            curl
            "https://api.cometapi.com/replicate/v1/predictions/$PREDICTION_ID" \
              -H "Authorization: Bearer $COMETAPI_KEY"
        - lang: Python
          label: Default
          source: |
            import os
            import time
            import requests

            prediction_id = "<prediction_id>"
            headers = {"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]}

            while True:
                prediction = requests.get(
                    f"https://api.cometapi.com/replicate/v1/predictions/{prediction_id}",
                    headers=headers,
                ).json()
                print(prediction["status"])
                if prediction["status"] in ("succeeded", "failed", "canceled"):
                    break
                time.sleep(3)

            if prediction["status"] == "succeeded":
                print(prediction["output"])
        - lang: JavaScript
          label: Default
          source: >
            const predictionId = "<prediction_id>";

            const headers = { Authorization: `Bearer
            ${process.env.COMETAPI_KEY}` };


            let prediction;

            while (true) {
                prediction = await (await fetch(
                    `https://api.cometapi.com/replicate/v1/predictions/${predictionId}`, { headers }
                )).json();
                console.log(prediction.status);
                if (["succeeded", "failed", "canceled"].includes(prediction.status)) break;
                await new Promise((resolve) => setTimeout(resolve, 3000));
            }


            if (prediction.status === "succeeded")
            console.log(prediction.output);
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Bearer token authentication. Use your CometAPI key.

````