Workspace/Research Articles/How to recover and safely retry failed rows in an LLM batch

How to recover and safely retry failed rows in an LLM batch

Summary: Recover missing batch outputs using stable row IDs, distinguish completed from expired requests, and safely retry only the rows that need attention.

Suppose a spreadsheet has 500 product records, but your batch returns only 497 results. Resubmitting the entire file could repeat work that already succeeded. Pasting the 497 results back into the spreadsheet in their returned order could also assign descriptions to the wrong products from the first missing record onward.

This is an illustrative example. Developers have reported both kinds of problems: one batch expired after its final request returned a 503, and another developer found that the returned identifiers did not match their UUIDs. These reports show why you need to verify both request outcomes and row identities. They do not establish a failure rate. Final request failure · Results that could not be matched to input rows

Save a manifest before you submit

Assign every task a stable identifier. If a product SKU can appear more than once, combine the import's identifier with the original row number. Save the original input, prompt version, model, and required attachment references. Retain the same task identifier on subsequent attempts and record the attempt number separately. Otherwise, merging results from two downloads becomes unreliable.

Read both the output and error files

Save the batch ID after creation and use it to check the batch status. A successful creation request means the batch was accepted for submission. When processing finishes, download the available output and error files. OpenAI support has explained when those file identifiers become available. Official community response

Treat the original manifest as the left side of a join and use custom_id to match records from both files. Account for every input identifier. Investigate unexpected or duplicate identifiers separately before replacing any previously saved results.

Input IDDownloaded resultNext action
import7-001200, content passes validationKeep the result
import7-002503Investigate the error and retry this request separately, recording the attempt
import7-003Missing from both filesCheck batch status, download completeness and identifiers
import7-004200, required field is emptyInspect the original response and test revised output instructions on a small sample

Find retryable original requests with an offline script

The following script applies to an OpenAI text batch that has finished. Save the complete original JSONL file as input.jsonl and the provider's two downloaded files as output.jsonl and errors.jsonl. If the batch genuinely has no file of one type, create an empty file for that type. The script copies only requests explicitly marked batch_expired into a retry file and leaves other failures for manual review.

import json
from pathlib import Path

def records(filename):
    for line in Path(filename).read_text(encoding="utf-8").splitlines():
        if line.strip():
            yield json.loads(line)

original = {}
for request in records("input.jsonl"):
    key = request["custom_id"]
    if key in original:
        raise ValueError(f"Duplicate input ID: {key}")
    original[key] = request

seen, expired, review = set(), set(), {}
for filename in ("output.jsonl", "errors.jsonl"):
    for item in records(filename):
        key = item["custom_id"]
        if key not in original or key in seen:
            raise ValueError(f"Unexpected or duplicate result ID: {key}")
        seen.add(key)
        response = item.get("response") or {}
        error = item.get("error") or {}
        if error.get("code") == "batch_expired":
            expired.add(key)
        elif response.get("status_code") != 200 or error:
            review[key] = error.get("code") or response.get("status_code") or "unknown"

# Create the retry file only after validating all input and result IDs.
with open("retry-expired.jsonl", "x", encoding="utf-8") as out:
    for key, request in original.items():
        if key in expired:
            out.write(json.dumps(request, ensure_ascii=False) + "\n")

print("IDs missing from both files:", sorted(original.keys() - seen))
print("Request errors to review:", review)
print("Rows to review for resubmission:", len(expired))

The script preserves the original model, instructions and input fields. Inspect the retry file before submitting it. Records with a 200 status still need content validation, such as checking that a classification label belongs to the allowed set. Investigate other error codes before deciding to retry. The script deliberately creates the output file exclusively, so choose a new filename for another run.

Validate the results after retrying

Keep earlier successful results. Mark a retried row as successful only when its new output passes field validation. Check that the number of unique validated IDs plus the number of unique IDs still needing attention equals the number of unique input IDs. Then inspect the first and last rows, retried rows, and inputs containing line breaks. Equal counts show that every ID is accounted for; spot checks help catch incorrect labels, empty content or truncated text.

If a previous request timed out and its outcome is unknown, look up the existing job first. When its outcome cannot be checked, communicate the possibility of duplicate charges before deciding whether to submit the request again.