Sub topic 2
Using `custom_id` to Match Batch Results to Inputs
Assume you know basic Python, JSON Lines, and how to call an LLM API. A batch API processes many requests asynchronously. Because results may finish in a different order than inputs, position is not a reliable way to match them.
Using `custom_id` to match results to inputs
Give every request a unique custom_id. Keep the identifier meaningful or store a separate mapping to your source record.
{"custom_id":"doc-001","method":"POST","url":"/v1/chat/completions","body":{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Summarize this document."}]}}
{"custom_id":"doc-002","method":"POST","url":"/v1/chat/completions","body":{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Classify this document."}]}}When results arrive, use each returned custom_id as a dictionary key. Do not assume that the first output belongs to the first input.
flowchart LR
A[Input request with custom_id] --> B[Batch job]
B --> C[Output record]
C --> D[Match by custom_id]The life of a batch job
A batch usually moves through these states:
validating: the provider checks the uploaded file.in_progress: requests are being processed.finalizing: output files are being prepared.completed: results are ready.failed,expired, orcancelled: the job did not finish normally.
A completion window such as 24h means the provider may take up to that period. It is not a promise that every request finishes at the same time.
Uploading, submitting, and polling a batch
The OpenAI Batch API accepts an uploaded JSONL file. The following example submits a job and waits for a terminal status:
import time
from openai import OpenAI
client = OpenAI()
input_file = client.files.create(
file=open("batch_input.jsonl", "rb"),
purpose="batch",
)
batch = client.batches.create(
input_file_id=input_file.id,
endpoint="/v1/chat/completions",
completion_window="24h",
)
terminal = {"completed", "failed", "expired", "cancelled"}
while batch.status not in terminal:
time.sleep(60)
batch = client.batches.retrieve(batch.id)
print(batch.status, batch.output_file_id, batch.error_file_id)Store the batch ID outside the process. A restart should retrieve the existing job instead of submitting duplicate work.
Reading output and error files
Read both file IDs when they exist. A response can fail even when the batch itself completes.
import json
results = {}
failed = {}
if batch.output_file_id:
for line in client.files.content(batch.output_file_id).text.splitlines():
record = json.loads(line)
if record.get("error") is None:
results[record["custom_id"]] = record["response"]["body"]
else:
failed[record["custom_id"]] = record["error"]
if batch.error_file_id:
for line in client.files.content(batch.error_file_id).text.splitlines():
record = json.loads(line)
failed[record["custom_id"]] = record.get("error") or record.get("response")Compare the input ID set with the successful and failed ID sets. Any remaining IDs are missing and need investigation or resubmission.
Exercise
Why should a pipeline match batch results by custom_id instead of line position?
Show answer
Results can complete and appear in a different order from the input requests, so line position can associate a response with the wrong input.