Working with results
Read results, rerun rows, cancel a batch and make retries safe.
The snippets continue the quickstart script, so client and batch are already set.
Read the results
output_file_id: rows that succeeded.error_file_id: rows that failed, including rows that never ran because the batch was cancelled or expired.errors: problems with the input file itself, with line numbers.
Each line in the result files is one row, matched to your request by custom_id. Rows can arrive in any order.
{
"id": "batch_req_example",
"custom_id": "ticket-001",
"response": {
"status_code": 200,
"request_id": "request-example",
"body": {
"id": "chatcmpl-example",
"object": "chat.completion",
"model": "qwen3.8-27b",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "billing"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 2,
"total_tokens": 22
}
}
},
"error": null
}{
"id": "batch_req_failed",
"custom_id": "ticket-002",
"response": null,
"error": {
"code": "batch_expired",
"message": "This request could not be executed before the completion window expired."
}
}A row the model rejected has an error of null and a non-200 response.status_code instead. To count successes, failures and missing rows:
import json
requests = {}
for line in open("requests.jsonl"):
if line.strip():
requests[json.loads(line)["custom_id"]] = line.rstrip("\n")
succeeded, failed = set(), set()
for file_id in (batch.output_file_id, batch.error_file_id):
if file_id:
for line in client.files.content(file_id).text.splitlines():
row = json.loads(line)
ok = not row["error"] and row["response"]["status_code"] == 200
(succeeded if ok else failed).add(row["custom_id"])
missing = requests.keys() - succeeded - failed
print(len(succeeded), "succeeded,", len(failed), "failed,", len(missing), "missing")A completed batch can still have failed rows, so check the counts.
Rerun some rows
Read each failed row's error first: a row with a bad parameter will fail again until you fix it. Then write the failed requests to a new file:
with open("retry.jsonl", "w") as out:
for custom_id in sorted(failed):
out.write(requests[custom_id] + "\n")Upload retry.jsonl and create a new batch, as in the quickstart.
Cancel a batch
batch = client.batches.cancel(batch.id)
print(batch.status) # cancelling, then cancelledRows already running finish; the rest end with batch_cancelled. Results so far stay in the result files.
Safe retries
Creating a batch. Send an Idempotency-Key header. If the create times out, send it again with the same key and body: you get the original batch back, not a second one. The same key with a different body returns HTTP 409.
batch = client.batches.create(
input_file_id=batch_file.id,
endpoint="/v1/chat/completions",
completion_window="24h",
extra_headers={"Idempotency-Key": "tickets-2026-10-19"},
)Uploading a file. Uploads have no idempotency key. List your files (GET /v1/files?purpose=batch) and look for the upload by filename, size and time before uploading again.
Queue full. HTTP 429 batch_queue_full means wait for Retry-After, then create again with the same file.
Using the resumable program
If batch.py or batch.mts stops, rerun the same command: it picks up from its state directory without uploading or creating twice. To retry failed rows, run it on retry.jsonl with a new state directory.
If an upload's reply is lost, the program stops with "Upload outcome uncertain" rather than upload twice. Find the file in your file list, then save its ID once its content matches your input:
# Run next to batch.py/batch.mts.
STATE_DIR="run1" # the --state-dir you used
FILE_ID="file-replace_with_verified_id" # a candidate from your file list
# Download that file, check it matches your input, then save its ID.
test ! -e "$STATE_DIR/file-id.txt" &&
curl -s --fail-with-body \
"https://api.reka.ai/v1/files/$FILE_ID/content" \
-H "Authorization: Bearer $REKA_API_KEY" \
-o "$STATE_DIR/recovered-input.jsonl" &&
cmp "$STATE_DIR/requests.jsonl" "$STATE_DIR/recovered-input.jsonl" &&
printf '%s\n' "$FILE_ID" > "$STATE_DIR/file-id.txt"Then rerun the program command.
Files and keys
- Files are deleted 31 days after upload (
expires_at). Download results before then. - Deleting the input file of an unfinished batch returns HTTP 409. Deleting a file never cancels a batch.
- Revoking the key that created a batch stops it within about a minute; its remaining rows fail with HTTP 403.
- When asking for help, share the batch and file IDs, never your API key.