Sub topic 9
Resubmitting Only What Failed
This chapter assumes you can build a JSONL request file, submit a batch, and read its output and error files. It also assumes you know that results are matched by custom_id. Here you will learn how to find the requests that did not succeed and send only those again.
Why partial failure is normal
A batch with 10,000 requests rarely finishes with 10,000 successes. A job that ends as completed can still contain individual failures. Three things can go wrong:
- A request is rejected or errors. Its line appears in the error file, or in the output file with a non-200 status code.
- A request expires. If the completion window closes first, the batch ends as
expired. Requests that did not run are missing from both files. - The whole batch fails validation. Then the batch status is
failedand no per-request results exist.
Resubmitting the entire file wastes money and time. You only need the difference between what you sent and what you collected.
Sort every request into three buckets
Every custom_id you submitted ends up in exactly one bucket:
| Bucket | Where it shows up | Usual cause |
|---|---|---|
| Succeeded | Output file, status 200, no error | Normal result |
| Failed | Error file, or output file with an error or non-200 status | Bad request body, rate limit, server error |
| Missing | In neither file | Batch expired or was cancelled before the request ran |
The third code example from the previous chapter computed these buckets. The line missing = set(documents) - set(results) - set(failed) is the key step. It uses your original inputs as the source of truth, not the files the provider returned.
Decide which failures are worth retrying
Not every failure should be retried. A request with a malformed body will fail the same way again.
- Retry missing requests, server errors (status 5xx), and rate-limit errors (status 429).
- Do not retry validation errors (status 400) until you fix the request body.
- Cap the number of attempts, for example three rounds. A request that keeps failing needs a person to look at it.
Build the retry batch
Reuse the original custom_id values. That keeps one identity per document across attempts, so your results dictionary needs no translation layer. The code below assumes documents, results, failed, and missing exist from the previous chapter. It also assumes client is an OpenAI() client.
import json
RETRYABLE = {429, 500, 502, 503, 504}
def is_retryable(failure):
if not isinstance(failure, dict):
return True
code = failure.get("status_code")
return code is None or code in RETRYABLE
retry_ids = set(missing)
for custom_id, failure in failed.items():
if is_retryable(failure):
retry_ids.add(custom_id)
with open("batch_retry.jsonl", "w", encoding="utf-8") as f:
for custom_id in sorted(retry_ids):
request = {
"custom_id": custom_id,
"method": "POST",
"url": "/v1/chat/completions",
"body": {
"model": "gpt-4o-mini",
"messages": [
{"role": "system", "content": "Summarize the text in one sentence."},
{"role": "user", "content": documents[custom_id]},
],
"max_tokens": 100,
},
}
f.write(json.dumps(request) + "\n")
print(len(retry_ids), "requests queued for retry")is_retryable is deliberately cautious. If a failure has no status code, it retries. Failures that carry an error object rather than a response may lack a status_code, so this default avoids silently dropping them. Adjust it if your error records look different.
Submit batch_retry.jsonl exactly as before: upload with purpose batch, create the batch, poll, then read the output. Merge the new successes into the same results dictionary and remove those IDs from failed.
Loop until done or out of attempts
A simple driver repeats the cycle: compute what is outstanding, stop if nothing is left or the attempt cap is reached, otherwise submit a retry batch. Persist results to disk after every round. If your process dies, you can restart and recompute the outstanding set from saved results instead of starting over.
Exercise
A batch ended with status expired. The output file holds 700 successes, and the error file holds 20 failures. You submitted 1,000 requests. How many are missing, and which of the failed ones should you retry?
Show answer
280 are missing (1,000 - 700 - 20). Retry all 280 missing requests. Of the 20 failures, retry only those with retryable errors such as 429 or 5xx. Fix and then resubmit any with a 400 validation error, because they would fail again unchanged.