Sub topic 8
Reading the Output and Error Files
This chapter assumes you can write basic Python, parse JSON Lines, and have already submitted a batch with the OpenAI Batch API. Once a batch reaches a terminal status, you have to collect its results. That step is easy to get wrong because results come back in two files, and they are not in input order.
Two files, two kinds of outcome
A finished batch can expose two file IDs on the batch object:
output_file_idpoints to a JSONL file with one line per request that was processed and produced a response.error_file_idpoints to a JSONL file with one line per request that failed.
Either field can be None. A batch where every request succeeded has no error file. A batch where every request failed has no output file. Always check each field before you download anything.
A request can also fail in two different ways, and both show up in the output file:
- The request ran, and the API returned a non-200 HTTP status in
response.status_code. - The request never ran properly, and the record has a non-null
errorfield.
So a line in the output file is not automatically a success. You must check it.
Anatomy of an output line
Each line is a JSON object with these fields:
| Field | Meaning |
|---|---|
| `id` | The batch request ID, assigned by the service. |
| `custom_id` | The ID you set in the input file. |
| `response` | Contains `status_code`, `request_id`, and `body`. |
| `error` | `null` on success, or an object describing the failure. |
For a chat completion, the model's text is at response["body"]["choices"][0]["message"]["content"].
A diagram of the collection step
Reading both files
The code below reads both files and keys every record by custom_id. It assumes client, batch, and the documents dictionary from the earlier chapters are still in scope.
import json
results = {}
failed = {}
if batch.output_file_id:
text = client.files.content(batch.output_file_id).text
for line in text.splitlines():
record = json.loads(line)
custom_id = record["custom_id"]
response = record.get("response")
if record.get("error") is None and response and response["status_code"] == 200:
results[custom_id] = response["body"]["choices"][0]["message"]["content"]
else:
failed[custom_id] = record.get("error") or response
if batch.error_file_id:
text = client.files.content(batch.error_file_id).text
for line in text.splitlines():
record = json.loads(line)
failed[record["custom_id"]] = record.get("error") or record.get("response")
missing = set(documents) - set(results) - set(failed)
print(len(results), "succeeded;", len(failed), "failed;", len(missing), "missing")Three details matter here:
- The
ifconditions guard against aNonefile ID. - Both
resultsandfailedare dictionaries keyed bycustom_id, so order never matters. - The
missingset is the difference between what you sent and what you accounted for.
Why "missing" exists
A batch can end as expired when it does not finish inside the completion window. In that case, some requests completed and some never ran. The unfinished ones may appear in neither file. Only a comparison against your original inputs reveals them. This is why you keep the set of submitted custom_id values, either in memory or in a manifest file on disk.
Common errors
- Reading by position. Zipping the output lines with the input list silently attaches the wrong answer to the wrong document.
- Treating every output line as success. A line with
status_code429 or 500 sits in the output file, andbodyhas nochoices. Indexing into it raises aKeyError. - Forgetting the error file. If you read only the output file, failures disappear without a trace.
- Assuming the counts add up. Compare
batch.request_countswith your own totals. A mismatch means your parser or your input bookkeeping is wrong.
The failed and missing sets are exactly what the next chapter uses to resubmit only the requests that need it.
Exercise
A batch finished with status completed. You read the output file and see 98 lines, and batch.error_file_id is None. You submitted 100 requests. What should you check, and how do you find the two requests that are unaccounted for?
Show answer
Check the status codes and error fields in the 98 lines, since some may be failures with a non-200 status. Then compute set(documents) - set(results) - set(failed), keyed by custom_id, to find the inputs with no record at all. Compare the totals with batch.request_counts to confirm which side has the discrepancy.