Skip to content
Software capabilities
OpenAI API

Retrieve OpenAI batch results

Poll a batch until it reaches a terminal state, then use its output_file_id or error_file_id to download JSONL results. Match each line by custom_id instead of assuming request order.

Documentation reviewed · 3 methods with sources

Ways to do it

  1. API: GET /v1/batches/{batch_id}
  2. SDK: client.batches.retrieve() and client.files.content()
  3. API: GET /v1/files/{file_id}/content
API

GET /v1/batches/{batch_id}

Read batch status, request counts, output_file_id, and error_file_id before downloading results.

Read source: OpenAI retrieve batch reference
Requirements
An OpenAI API key and the batch ID.
Limitations
File IDs can be null. An expired batch may still contain completed outputs, while unfinished requests appear in the error file with batch_expired.

Example shell

curl --fail "https://api.openai.com/v1/batches/$OPENAI_BATCH_ID" -H "Authorization: Bearer $OPENAI_API_KEY"
SDK

client.batches.retrieve() and client.files.content()

Inspect the file IDs, then read each available JSONL file with the Python SDK.

Read source: OpenAI Batch API guide
Requirements
The openai Python package, OPENAI_API_KEY, and a batch ID.
Limitations
Use custom_id to match lines to input requests. The API can return results in a different order.

Example text

import os
from openai import OpenAI

client = OpenAI()
batch = client.batches.retrieve(os.environ["OPENAI_BATCH_ID"])
for file_id in (batch.output_file_id, batch.error_file_id):
    if file_id:
        with client.files.with_streaming_response.content(file_id) as response:
            response.stream_to_file(f"{file_id}.jsonl")
API

GET /v1/files/{file_id}/content

Download the JSONL content for a completed output or error file.

Read source: OpenAI retrieve file content reference
Requirements
An OpenAI API key and a file ID returned by the batch object.
Limitations
Output and error files expire 30 days after creation by default. A batch can set a shorter retention window.

Example shell

curl --fail "https://api.openai.com/v1/files/$OPENAI_OUTPUT_FILE_ID/content" -H "Authorization: Bearer $OPENAI_API_KEY" > batch-output.jsonl