Sub topic 6
The life of a batch job: statuses and completion windows
This chapter assumes you can upload a JSONL request file and create a batch with the OpenAI Python SDK, as covered in the earlier chapters. Here you learn what happens to a batch after you create it, and how to read its status field correctly.
What a batch job is
A batch is a server-side job object. It points at your uploaded input file and carries a status, timestamps, and counters. Creating it does not run your requests immediately. The provider queues them and processes them when capacity allows. You only need to know the job's state to decide what to do next.
The completion window
When you create a batch, you pass completion_window="24h". This is a promise about the maximum time before the job reaches a final state. It is not an estimate. Many batches finish much sooner, but you cannot rely on that, so design for the full window.
The clock starts when the batch is created. If the provider has not finished every request when the window closes, the batch ends as expired.
Statuses
The OpenAI Batch API uses these statuses:
| Status | Meaning | Final? |
|---|---|---|
| `validating` | The input file is being checked | No |
| `failed` | Validation rejected the file | Yes |
| `in_progress` | Requests are running | No |
| `finalizing` | Requests are done and results are being written | No |
| `completed` | Finished, output available | Yes |
| `expired` | The window closed before all requests finished | Yes |
| `cancelling` | You asked to cancel, and it is winding down | No |
| `cancelled` | Cancellation finished | Yes |
What each final state means for your data
Reaching a final state does not mean every request succeeded. Treat each one differently:
completed: The job ran. Individual requests can still have failed, so you must still read the error file.failed: Nothing ran. The file was invalid, for example malformed JSON, a duplicatecustom_id, or a mixed set of models or endpoints. Details are inbatch.errors. Fix the file and submit a new batch.expired: The window closed first. Requests that finished are in the output file. The rest produced no result, so you must find them and resubmit them.cancelled: Same shape asexpired. Finished requests are kept, and the rest are not run.
Cancellation is not instant. The batch sits in cancelling while in-flight requests wind down, which can take several minutes.
Reading progress with request_counts
While a batch is non-final, batch.request_counts shows total, completed, and failed. Use it for progress logs, not for decisions. Only a final status tells you the results are ready.
The helper below fetches a batch by ID and says what to do next:
from openai import OpenAI
client = OpenAI()
NEXT_STEP = {
"completed": "Read output_file_id and error_file_id.",
"expired": "Read partial results, then resubmit missing requests.",
"cancelled": "Read partial results, then resubmit missing requests.",
"failed": "Inspect batch.errors, fix the input file, create a new batch.",
}
def describe(batch_id: str) -> str:
batch = client.batches.retrieve(batch_id)
counts = batch.request_counts
if batch.status in NEXT_STEP:
return f"{batch.status}: {NEXT_STEP[batch.status]}"
return (
f"{batch.status}: {counts.completed}/{counts.total} done, "
f"{counts.failed} failed so far"
)Common errors
- Polling too fast. A batch takes minutes to hours. Polling every 30 to 60 seconds is plenty, and longer is fine for big jobs.
- Treating
completedas all-success. Always check the error file and compare returned IDs with the IDs you sent. - Ignoring
expired. A loop that waits only forcompletedwill never exit if the batch expires. Test against the full set of final statuses. - Assuming a short wait. Do not build a user-facing flow that expects an answer within minutes.
Exercise
A batch you created 24 hours ago now shows the status expired, and request_counts shows total 500 and completed 430. What do you do next, and why not simply wait longer?
Show answer
Waiting does not help, because expired is a final state and no more requests will run. Download the output file, which holds the 430 finished results (some may be in the error file), key everything by custom_id, and compute which of the 500 IDs have no result. Then build a new JSONL file from only those requests and submit it as a new batch.