> ## 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.

# Lấy một prediction Replicate

> Truy vấn GET /replicate/v1/predictions/{id} để lấy chi tiết prediction Replicate và tiến độ theo thời gian thực theo ID tác vụ cho các quy trình tạo ảnh.

Sử dụng endpoint này sau khi bạn đã có prediction id của Replicate. Endpoint này báo cáo trạng thái hiện tại của tác vụ và trả về các URL output khi prediction hoàn tất.

## Kiểm tra các trường này trước

* `status` để xem prediction vẫn đang chạy hay đã hoàn tất
* `output` cho các URL tài nguyên được tạo
* `error` cho các lỗi từ phía provider
* `metrics` khi bạn cần thời gian thực thi hoặc chi tiết số lượng ảnh

## Mẫu polling

<Steps>
  <Step title="Tạo prediction trước">
    Bắt đầu với [Tạo một prediction Replicate](./create-predictions-general).
  </Step>

  <Step title="Polling theo prediction id">
    Tiếp tục truy vấn cho đến khi `status` chuyển sang trạng thái kết thúc và `output` đã được điền hoặc lỗi từ provider được trả về.
  </Step>

  <Step title="Lưu trữ tài nguyên đã hoàn tất">
    Xem các URL tài nguyên được trả về là URL phân phối và chuyển chúng vào hệ thống lưu trữ của riêng bạn nếu cần lưu giữ lâu dài.
  </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.

````