Skip to content

Advanced

Batch Inference (Batch API)

Batch Inference (Batch API) is designed for offline data synthesis, large-scale evaluation, and asynchronous generation. Upload a JSONL file of requests to your workspace, let PyTRIO process them asynchronously, and download the output and error files when the job finishes.

You can manage batch jobs via either:

1. Prepare the Input File ​

Prepare a non-empty JSONL file (0-byte files are rejected by the SDK) with one JSON request per line:

json
{"custom_id": "req-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "Qwen/Qwen3.5-4B", "messages": [{"role": "user", "content": "Explain LoRA in one sentence."}], "max_tokens": 128}}
{"custom_id": "req-2", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "Qwen/Qwen3.5-4B", "messages": [{"role": "user", "content": "What is GRPO?"}], "max_tokens": 128}}

body.model accepts either a base model ID or a saved Sampler checkpoint path.

2. End-to-End CLI Workflow ​

bash
# 1. Upload and submit in one step (or upload first with `trio file upload`)
trio batch create --file requests.jsonl \
  --completion-window 24h \
  --description "nightly eval"

# 2. List jobs or inspect status and request counts
trio batch list --limit 20
trio batch info BATCH_ID

# 3. Download output and error JSONL files (<BATCH_ID>-output.jsonl / <BATCH_ID>-error.jsonl)
trio batch download BATCH_ID -o ./results

# Cancel a running job if needed
trio batch cancel BATCH_ID --yes

All trio file and trio batch subcommands support --format json and --format csv before the subcommand:

bash
trio --format json batch info BATCH_ID

3. Python SDK Workflow (RestClient) ​

python
import time
import pytrio as trio

with trio.RestClient() as rest_client:
    # 1. Upload the JSONL input file
    uploaded = rest_client.upload_file("requests.jsonl").result()
    print("Uploaded file:", uploaded.file_id)

    # 2. Submit the batch job
    batch = rest_client.create_batch(
        input_file_id=uploaded.file_id,
        completion_window="24h",
        description="nightly eval",
    ).result()
    print("Created batch:", batch.batch_id, batch.status)

    # 3. Poll until completion
    while batch.finished_at is None:
        time.sleep(10)
        batch = rest_client.get_batch(batch.batch_id).result()
        counts = batch.request_counts
        print(
            f"status={batch.status} "
            f"completed={counts.completed}/{counts.total} failed={counts.failed}"
        )

    # 4. Download output and error files
    if batch.output_file_id:
        out_path = rest_client.download_file(
            batch.output_file_id,
            f"results/{batch.batch_id}-output.jsonl",
        ).result()
        print("Saved output to:", out_path)

    if batch.error_file_id:
        err_path = rest_client.download_file(
            batch.error_file_id,
            f"results/{batch.batch_id}-error.jsonl",
        ).result()
        print("Saved errors to:", err_path)

For async code, use upload_file_async, create_batch_async, get_batch_async, and download_file_async (single await, returning the domain object directly).

4. Constraints and Troubleshooting ​

  • completion_window: Must be a whole-hour string from "24h" to "168h" (default "24h").
  • description: At most 512 characters after trimming leading and trailing whitespace.
  • Uncertain submission (submission_unknown): To prevent duplicate jobs, create_batch() does not replay submissions automatically on transport uncertainty. If a PyTrioError has err.details.get("submission_unknown") is True, check trio batch list or rest_client.list_batches_page() before resubmitting.
  • Input validation errors: When the input JSONL fails validation, batch.validation_errors lists each entry's line, code, and message.
  • File deletion: An input file still referenced by an active batch cannot be deleted (file_in_use).