Batch inference

Updated at:

Batch inference lets you submit a large number of requests for asynchronous processing and download the results when they are ready. You can manage everything from the console or call the API directly. The endpoints are compatible with the OpenAI Batch API, so you can use the official OpenAI SDK.

Use cases

Batch inference supports the following scenarios:

  • Evaluation: Submit test cases in bulk to evaluate model performance.

  • Data labeling: Process data for annotation at scale.

  • Offline generation: Generate content such as articles or code in large quantities.

  • Regression testing: Verify output quality after model updates.

The following endpoint paths are supported:

  • /v1/chat/completions

  • /v1/responses

  • /v1/completions

Prerequisites

Before using batch inference, complete the following setup:

  • Configure Redis: Batch processing relies on Redis to store task status. A reachable Redis instance must be configured for your workspace. Otherwise, batch inference is unavailable.

  • Configure OSS storage: Input and result files are stored in OSS. On the Batch Inference page in the console, click Configure OSS Storage and set the OSS path before you create a task.

Input file format (JSONL)

Both the console and API require JSONL input files — one JSON object per line.

Format requirements

  • Each line is an independent JSON object (one request). Don't use a JSON array.

  • Maximum 50,000 lines per file. Maximum file size: 200 MB.

Fields

Field

Required

Description

custom_id

Yes

A unique identifier for the request. This value is written back to the corresponding result line, which lets you match requests to responses.

method

Yes

Fixed value: "POST".

url

Yes

The endpoint path. Supported values: /v1/chat/completions, /v1/responses, and /v1/completions.

body

Yes

The full request body, in the same format as a direct API call (including model, messages, and other fields).

Example input

{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "qwen-plus", "messages": [{"role": "user", "content": "Hello"}]}}
{"custom_id": "request-2", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "qwen-plus", "messages": [{"role": "user", "content": "What's the weather like today?"}]}}
{"custom_id": "request-3", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "qwen-plus", "messages": [{"role": "user", "content": "Write me a poem"}]}}

Use the console

The Batch Inference page in the console provides a complete workflow: upload files, create tasks, monitor progress, and download results.

Upload an input file

  1. On the Batch Inference page, click the Files tab.

  2. Click Upload File and select a local JSONL file.

  3. After the upload finishes, you can Preview, Download, create a new task, or Delete the file.

Create a task

  1. On the Batch Inference page, click the Tasks tab.

  2. Click Create Task. Select an uploaded file, or click Upload New File to upload a JSONL file.

  3. Select an endpoint path from the Endpoint drop-down list: /v1/chat/completions, /v1/responses, or /v1/completions.

  4. (Optional) Enter a description in the Notes field, such as the purpose or model name.

  5. (Optional) Set the number of Concurrency shards.

    • If left empty, the system splits requests into shards of approximately 1,000 requests each (shards = total requests ÷ 1,000, rounded up).

    • If specified, the task is split into the given number of shards. Each shard is processed by one thread, so this value controls the maximum parallelism for the task. The system distributes requests evenly across shards (total requests ÷ shards), with the last shard receiving the remainder. This setting applies only to the current task.

  6. Click Create. The task appears in the task list after it's created.

View results

After the task status changes to Completed, view or preview the results:

  • On the Tasks tab, click Download Results in the task's operation column.

  • Switch to the Files tab. Find the output file (status: Successful) to Preview or Download the results.

Use the API

To automate batch inference from your code, use the OpenAI Batch API–compatible endpoints.

The workflow has four steps: upload an input file → create a task → poll the status → download the results.

curl / Shell example

# 0. Preparation: Store the API Key in an environment variable; click the call info button in the upper-right corner of the workgroup to get the endpoint URL
export LLM_GATEWAY_API_KEY="<YOUR-API-KEY>"
BASE="<YOUR-BASE-URL>"

# 1. Upload the input file (JSONL); set purpose to batch
curl -X POST "$BASE/v1/files" \
  -H "Authorization: Bearer $LLM_GATEWAY_API_KEY" \
  -F "purpose=batch" \
  -F "file=@batch_input.jsonl"
# → {"id": "batch_input_xxx", ...}  Note this id

# 2. Create a batch task using the uploaded file
#    num_shards (optional): splits the task into N shards (= max number of parallel threads for this task).
#    The system divides requests evenly as "total requests ÷ number of shards" (the last shard gets the remainder).
#    If not specified, the system uses the default lines-per-shard value (default: 1000 lines per shard).
curl -X POST "$BASE/v1/batches" \
  -H "Authorization: Bearer $LLM_GATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input_file_id": "batch_input_xxx",
    "endpoint": "/v1/chat/completions",
    "completion_window": "24h",
    "num_shards": 2
  }'
# → {"id": "batch_xxx", "status": "pending", ...}

# 3. Poll the task status (validating → in_progress → completed)
curl "$BASE/v1/batches/batch_xxx" \
  -H "Authorization: Bearer $LLM_GATEWAY_API_KEY"

# 4. After completion, download the results using output_file_id
curl "$BASE/v1/files/<output_file_id>/content" \
  -H "Authorization: Bearer $LLM_GATEWAY_API_KEY" -o output.jsonl

# (Optional) Cancel an in-progress task
curl -X POST "$BASE/v1/batches/batch_xxx/cancel" \
  -H "Authorization: Bearer $LLM_GATEWAY_API_KEY"

Python (OpenAI SDK) example

# The API is compatible with the OpenAI Batch API. You can use the official openai SDK directly.
# pip install openai
import time
from openai import OpenAI

client = OpenAI(
    base_url="<YOUR-BASE-URL>/v1",
    api_key="<YOUR-API-KEY>",
)

# 1. Upload the input file
inp = client.files.create(
    file=open("batch_input.jsonl", "rb"),
    purpose="batch",
)

# 2. Create a batch task
#    num_shards (optional): number of shards (= max number of parallel threads). Requests are divided evenly as "total requests ÷ number of shards".
#    This is an extended parameter; pass it via extra_body. If not specified, the system uses the default lines-per-shard value (default: 1000 lines per shard).
batch = client.batches.create(
    input_file_id=inp.id,
    endpoint="/v1/chat/completions",
    completion_window="24h",
    extra_body={"num_shards": 2},
)

# 3. Poll until the task reaches a terminal state
terminal = {"completed", "failed", "expired", "cancelled"}
while batch.status not in terminal:
    time.sleep(10)
    batch = client.batches.retrieve(batch.id)
    print(batch.status, batch.request_counts)

# 4. Download results and errors
if batch.output_file_id:
    print(client.files.content(batch.output_file_id).text)
if batch.error_file_id:
    print(client.files.content(batch.error_file_id).text)

Usage notes

  • Endpoint and URL: Use only the supported endpoints: /v1/chat/completions, /v1/responses, and /v1/completions.

  • completion_window: Currently fixed at "24h". Tasks that don't complete within 24 hours expire automatically.

  • num_shards: Optional. Splits the task into the specified number of shards, where each shard runs as one thread (maximum parallelism). Requests are distributed evenly across shards (total requests ÷ shards). If left empty, the system uses the default shard size of approximately 1,000 requests (configurable by the deployment). Actual parallelism is also limited by the global thread count and the number of backend instances.

  • Input file limits: Maximum 50,000 lines and 200 MB per file. Failed requests are written to a separate error file.

  • Authentication: Uses a Bearer API key. Console operations reuse the current sign-in identity. During task execution, the corresponding API key is used for gateway callbacks.