Skip to content

Latest commit

 

History

History
526 lines (416 loc) · 24.8 KB

batches.md

File metadata and controls

526 lines (416 loc) · 24.8 KB

Batch requests

Prepare requests locally, then submit the file with one command. Waiting and downloading use existing Batches and Files APIs.

Find your way around

Use the command guide without contacting the API:

openai batches
openai explore batches

batches explains the workflow and lists available actions. explore batches searches the CLI's commands and examples locally. Add --help to any command for its options.

Open the interactive browser to choose an existing batch without copying its ID:

openai batches browse

Choose a batch to read its results, errors, input requests, or details. Expand Save a file to choose output, error, or input files. Press Enter to save the suggested filename. Press Ctrl+L to choose another folder. The next file from that batch uses the selected folder. You can also type a full path. Press / from a batch row to focus Find batch. You can also start typing to filter the loaded page. In the field, type or paste an endpoint such as /v1/responses. Use Next page to reach older batches. Press Ctrl+R to reload the first page. Escape returns to batch actions or the batch list. At the batch list, Escape exits. Ctrl+C exits from any screen. Downloads ask for a destination and never overwrite an existing file. The browser uses your configured connection, API key, and project. Press Ctrl+N to start guided creation from a local JSONL file, an uploaded input, or a CSV. With Input file selected, type or paste a local file path. Press Enter to use it. Press Enter on an empty field to browse CSV and JSONL files in the displayed folder. Type a filename to filter. Press Enter on a folder to open it. With the filter empty, Backspace goes up one folder. Ctrl+L opens a path directly. Relative paths start in the displayed folder. Choose an uploaded file opens the project file list. Set Metadata and Output expiration directly on Create batch. Enter applies an edit. Esc cancels it. Choosing JSONL opens Review batch directly. The browser reads the endpoint from the file unless you chose one yourself. Review the request count, endpoint, and project before submitting. Press Enter or Ctrl+S on Review batch to submit. Press Esc to edit. The browser opens the new batch and updates progress every ten seconds while you view its actions. The browser does not cancel remote batches.

Search requests by ID or content. Short answers appear beside their request IDs. Press Enter to expand the selected request in the list. Use ↑ or ↓ to inspect another request. A single request error opens expanded. It shows the API message and available recovery steps. Use the HTTP status, error code, and request ID to investigate further. Press → to open the full record. Press j there to switch between Readable and Raw JSON. Press p in batch details or a request record to print it into shell scrollback.

For scripts, use list, retrieve, results, and --format json. The browser requires an interactive terminal and readable output.

Start from a CSV

For example, a support team can classify tickets from a CSV with ticket_id and message columns.

  1. Open openai batches browse.
  2. Press Ctrl+N.
  3. Type the path to your UTF-8 CSV in Input file.
  4. Press Enter.
  5. On Set up CSV, choose the text column, such as message. The browser selects a single column automatically.
  6. Describe the task, such as “Classify each ticket as billing, login, or other.”
  7. Enter a Responses model available to your project.
  8. On Review batch, check the row count and first input.
  9. Press Enter to submit the batch.
  10. When processing ends, open View rows with answers.
  11. To save a copy, press s. Ctrl+S lets you choose another filename or folder.

The row list opens in compact view. The controls appear below the frame. Press Ctrl+G to switch between compact and full view. Both views keep your search and selected row. The filename and row count appear above the spreadsheet. Press / to filter by row text or answer. Use the arrows to select a match. Enter expands its details. Escape returns to the matches. Saving exports the entire spreadsheet, including rows outside the filter.

CSV preparation runs locally. The browser uses the API to list existing batches. Confirmation uploads one Responses request per row and starts a batch that can incur charges. Each request contains the selected text, model, instructions, and a fingerprint derived from the entire row, its position, and headings. The spreadsheet's other columns do not become prompt text.

Keep the original CSV. After restarting the browser, open View results or View input for the completed batch. Press Ctrl+O when original CSV appears. Choose the original spreadsheet. The browser verifies each row before displaying its answer. Row order stays the same when answers arrive out of order. The saved CSV adds result and error columns. Failed or missing answers appear on their original rows. The suggested destination is a new file beside the original, such as tickets-results.csv. Export never overwrites an existing file. It prefixes formula-like cells with an apostrophe for spreadsheet safety. If a filename already exists, enter another name or press Ctrl+L to choose another folder. Use the local synthetic demo to try this without an API key or charges.

Prepare and submit

Each JSONL line contains one request with a unique custom_id. Use the same model throughout one input file. Use a model supported by your account and the selected endpoint. This example uses two synthetic requests:

{"custom_id":"example-1","method":"POST","url":"/v1/responses","body":{"model":"gpt-4.1-mini","input":"Return the word blue."}}
{"custom_id":"example-2","method":"POST","url":"/v1/responses","body":{"model":"gpt-4.1-mini","input":"Return the word green."}}

Save these lines as requests.jsonl. Upload and create one batch with one command:

openai batches submit requests.jsonl

submit requires an existing regular JSONL request file. It does not write requests. An omitted endpoint comes from the file's request URLs. An omitted completion window defaults to 24h. Explicit flags override these defaults. --file requests.jsonl remains available instead of the positional path. Create, wait, and save without copying the batch ID:

openai batches submit requests.jsonl --wait --output-dir ./results

It accepts the same request fields as create, except input_file_id. You can provide those fields through flags or piped JSON/YAML. Stdin remains available for request fields; --file - is not supported. Use files upload followed by batches create for stdin or custom input-file retention.

The CLI validates local request fields before uploading. The API performs content and account validation. Upload and creation each run once, without automatic retries. If creation fails, the CLI keeps the uploaded file and reports its ID. Direct HTTP 401/403 responses report an access rejection. Correct access before retrying the failed operation. If upload succeeded, reuse its file ID with create instead of repeating submit. Transport failures, server failures, and redirected access failures can leave the mutation outcome uncertain. Check files list after an uncertain upload, or batches list after uncertain creation. Reuse the same connection and project settings. Then use create --input-file-id if you confirm that no batch exists. Structured submission errors include the original error under error and recovery data under batch_submission. If local waiting stops before completion, resume with batches wait and the returned batch ID. If the batch has finished, inspect its status, results, and errors instead of waiting again. Keep saved files. Download only missing files after a partial download failure. Use the same connection and project settings. Do not repeat submit to recover results.

You can also upload separately:

openai files upload --file requests.jsonl --purpose batch

Use the returned file ID when creating the batch:

openai batches create \
  --input-file-id file_example \
  --endpoint /v1/responses \
  --completion-window 24h \
  --metadata '{"job":"example"}' \
  --output-expires-after.anchor created_at \
  --output-expires-after.seconds 86400

The expiration example keeps output and error files for one day after each file's creation. Omit expiration flags to use the service default. Create also accepts --output-expires-after '{"anchor":"created_at","seconds":86400}'. Omission does not promise indefinite retention. The Batch guide documents automatic output deletion after 30 days. Inspect the file's expires_at value for its returned expiration timestamp.

Input files have a separate retention policy. Files uploads with purpose batch expire after 30 days by default. For example, keep an input file for seven days:

openai files upload --file requests.jsonl --purpose batch \
  --expires-after.anchor created_at --expires-after.seconds 604800

Current Batch creation limits include 50,000 requests and 200 MB per input file. The completion window is 24h. Output expiration accepts 3,600 through 2,592,000 seconds, anchored at created_at. The CLI forwards fields without adding endpoint or model restrictions.

The create reference includes /v1/videos. However, the deprecation notice lists September 24, 2026 as its shutdown date. This documentation conflict does not establish current Videos availability.

Wait and save results

Wait for an existing batch and save its available result files:

openai batches wait --batch-id batch_example --output-dir ./results

The command creates results if absent. Its parent directory must already exist. It saves exact bytes as output.jsonl and error.jsonl when the API returns those file IDs. An absent file produces no file. Human terminals show which files were saved or absent. The final stdout response remains the Batch object, including explicit JSON and extraction modes. Quiet mode suppresses optional progress and receipts.

openai batches wait batch_example --output-dir ./results --poll-interval 5s --wait-timeout 30m
openai --format json batches wait batch_example --output-dir ./results > batch.json

The deadline includes polling, downloads, and final output. The command saves available files for completed, failed, expired, and cancelled batches. Failed requests and unsuccessful terminal states still return nonzero after saving available files. An unknown status prints the response and exits nonzero without downloading. Neither command creates another batch or cancels remote work.

Existing result paths fail preflight before API requests. Downloads never overwrite another process's files. A new directory uses private permissions. Each new file uses private permissions where the operating system supports them. Successful files remain saved if another download fails or local work stops. Ordinary download failures still allow the other available file to download. Ctrl+C stops further local work.

After a partial download, retry only the missing file to a new path:

openai batches download batch_example --file error --output ./results/errors-retry.jsonl

Reuse the same connection options. Do not create another batch because waiting or downloading failed. Omit --output-dir to wait without downloading files. The existing batches retrieve --wait interface also remains available.

Inspect and wait

Existing commands retrieve once or list batches:

openai batches list --limit 20 --max-items 20
openai batches retrieve --batch-id batch_example
openai --format json batches retrieve batch_example

Explicit JSON includes creation time, completion window, lifecycle timestamps, metadata, and complete errors. The default readable view summarizes those fields. For example, extract the exact ID without copying labels:

openai --transform id --raw-output batches retrieve batch_example

Readable output includes UTC lifecycle dates, the completion window, and metadata. Completed batches include the duration from creation to completion. Explicit formats and extraction retain the original timestamps and API fields.

Refresh the selected Batch list every ten seconds:

openai batches list --watch --limit 100 --max-items -1

Each refresh prints another snapshot without clearing terminal history. Pagination runs serially within each refresh. Each refresh uses private temporary storage for its records and pagination cursors. Memory use depends on one API page and one rendered record, instead of the complete Batch history. Temporary storage must hold the current snapshot and its index. The command removes this storage before the next refresh, including after failure or handled interruption. The API does not provide an atomic snapshot across pages. API failures stop watching with a nonzero exit. A partial snapshot is marked as incomplete. Ctrl+C stops local watching without changing remote batches.

Use JSONL for machine consumption:

openai --format jsonl batches list --watch --poll-interval 5s --watch-count 3

Each line contains object, sequence, observed_at, complete, and the data array. --watch-count 0 watches until interrupted. Watch supports readable output and JSONL. Omit --watch for other formats, extraction, or raw output.

Add --wait to poll until the batch reaches a terminal status:

openai batches retrieve batch_example --wait
openai batches retrieve batch_example --wait --poll-interval 5s --wait-timeout 30m

The default polling interval is 10 seconds after each successful check. The default wait timeout is zero, meaning no overall deadline. A positive timeout includes requests, retries, and polling delays. It does not change the SDK's individual request timeout behavior. The CLI retains SDK retries and supported Retry-After handling; exhausted retries stop waiting. Malformed batch JSON stops local waiting with an error. On batches retrieve, timing flags require --wait.

Human terminal progress uses returned counts:

Processing: 420 of 1000 requests finished.
Completed: 995 succeeded, 5 failed.

These numbers illustrate output; actual counts come from the API. Finished requests include successes and failures. The final response retains the existing readable fields. Explicit formats retain complete API data:

openai --format json batches retrieve batch_example --wait > batch.json
openai --transform status --raw-output batches retrieve batch_example --wait

Progress and create hints use stderr only with human output and terminal stdout/stderr. Pipes, extraction, and machine formats for either results or errors receive no progress or hints. A successful create can show a follow-up command containing its returned batch ID. Explicit connection or authentication overrides instead show a reminder to reuse those options. The CLI never copies private option values into a hint. Interruption during final output can leave partial data on stdout.

Wait continues through validating, in_progress, finalizing, and cancelling. It stops at completed, failed, expired, or cancelled. An unknown future status prints the response and exits nonzero.

Wait result Exit status
Completed with consistent counts and zero request failures 0
Request failures, failed, expired, cancelled, unknown status, or missing/inconsistent counts 1
API or transport error 1
Overall local wait timeout 124
SIGINT while waiting or writing output 130
SIGTERM during local batch work 143

Ordinary retrieve retains its existing HTTP-success exit behavior, regardless of batch status. --wait=false also retains ordinary retrieval. Ctrl+C and SIGTERM stop local work. Neither cancels the remote batch. Inside the interactive explorer, q or keyboard Ctrl+C closes the view and retains the batch outcome. The wait deadline also stops the explorer. Terminal restoration gets up to 200 ms of cleanup time. Cancellation diagnostics use a separate 250 ms delivery window. A fallback diagnostic, if needed, has its own 250 ms window. For canceled batch work, --format-error explore emits static JSON instead of opening another view. Cancellation can omit its diagnostic when the interrupted output destination also carries errors. The exit status still distinguishes interruption from timeout.

Cancel remote work

Remote cancellation requires an explicit command:

openai batches cancel --batch-id batch_example

The service can remain in cancelling for up to ten minutes. Cancelled and expired work can still have partial output or error files. See the Batch guide for lifecycle behavior.

Download files

Choose a file path without a trailing path separator. A trailing path separator fails before any API request.

Download one selected batch file to an explicit destination:

openai batches download batch_example --output results.jsonl
openai batches download batch_example --file error --output errors.jsonl
openai batches download batch_example --file input --output original.jsonl

--file defaults to output; it also accepts error and input. The command retrieves the batch once, then downloads its selected file through Files. It works whenever that file ID exists, including partial results. An absent file ID or unavailable remote file returns nonzero. The command does not wait automatically.

Downloads preserve JSONL bytes and ordering exactly. Result order can differ from input order; match records using custom_id. The command never opens results or resubmits failed requests.

Existing destinations remain unchanged, including symlinks. File downloads use private temporary files and publish only complete content without replacing another file. Interrupted or failed downloads remove their owned temporary file. A filesystem without atomic no-replace support fails safely. Previously completed downloads remain intact.

Use --output - for an exact stream:

openai batches download batch_example --output - | jq -c '.custom_id'

Interrupted stdout can contain partial data. Output formats affect saved-file receipts and errors, never file bytes. Use the existing Files command when you already know the file ID:

openai files download --file-id file_example --output results.jsonl

Unlike the batch convenience, files download --output PATH can overwrite its destination.

Browse and manage linked files

Use the batch's input, output, or error file ID to inspect file details:

openai --format json files get file_example

The response includes filename, byte size, purpose, creation time, expiration, and any returned status details. Find existing Batch input files by filename or ID:

openai batches inputs --search requests --max-items -1

Search uses a case-insensitive literal substring. The command searches subsequent pages until it finds the requested number of matches or reaches the end. The default limit is 100 matching files; -1 searches all pages. Files retain their newest-first order and complete metadata.

Select one file interactively:

openai batches inputs --select

Selection requires terminal stdin and stderr. The picker returns the selected File metadata without creating a batch. Use its ID with batches create --input-file-id. For structured stdout, explicitly keep picker errors readable:

openai --format json --format-error text batches inputs --select

Browse a batch's JSONL records without a separate download:

openai batches results batch_example --file input
openai batches results batch_example --custom-id example-1 --match-input
openai --format explore batches results batch_example --match-input

--file defaults to output; it also accepts input and error. --custom-id filters by an exact, case-sensitive ID. --match-input groups inputs and selected results by custom_id, never by row position. Each group includes inputs, results, a match status, and any issues. Duplicate IDs remain grouped and visibly ambiguous. Missing IDs and unmatched records remain visible. Without matching, records retain their original order.

Default readable results show the request outcome and its meaningful content. The CLI reports refusals and incomplete answers separately from HTTP success. Multiple answers, tool calls, annotations, and unfamiliar content remain visible. --match-input also shows the original input instructions and content. Duplicate IDs do not create artificial input/result pairs. Missing results refer only to the selected file, not the complete batch. Inspecting a failed record successfully exits zero; this does not mean the request succeeded. The default output remains readable when piped.

Use explicit JSON when you need every API field:

openai --format json batches results batch_example --custom-id example-1 --match-input

Readable results omit reviewed configuration fields that repeat request settings. JSON, JSONL, raw output, and extraction retain the complete selected data. Large records and unfamiliar structures use a labeled detailed view. The detailed view preserves complete content without imposing a new size limit.

The response contains a data array and supports normal output formats and extraction. The explorer supports interactive inspection when a terminal is available. The command reports unavailable files and malformed JSONL without submitting requests again. Use batches download when you need exact file bytes. Non-matching results emit each validated record in text, JSON, JSONL, and raw formats. Ordinary records use bounded memory without temporary files. Oversized records use one reusable private file, with temporary space proportional to the largest record. Matching and browser navigation use indexed temporary storage for the selected files. Large object labels can also use temporary storage during readable rendering. The command removes owned temporary storage after success, failure, or handled interruption. JSON, JSONL, and raw output retain one envelope containing the complete selected data array. A late read or validation failure returns a nonzero exit status and can leave incomplete output. After an error, the CLI briefly drains queued output before stopping a blocked destination. Always check the exit status before using formatted results. Readable projections buffer at most 256 KiB per record before switching to streaming detail. Matching keeps groups on disk and renders their records separately. Large IDs and object keys do not have new size limits. Extraction, terminal exploration, pretty output, and YAML retain the selected document in memory. Use --custom-id to reduce that document before these operations.

Files also provides explicit deletion:

openai files delete file_example

Deletion removes the file, including its use in vector stores. File deletion and batch cancellation are separate operations.

Access and safety

Use a supported API key with the required project access. Existing organization, project, custom header, base URL, and mTLS options also apply to waiting and downloading. The CLI does not enforce dashboard permissions locally.

Public RBAC documentation defines api.batch.read and api.batch.write. Batch permissions imply relevant Files permissions. The server checks both API key permissions and the caller's project role. Admin keys do not authorize ordinary Batch operations.

Select regional endpoints according to your project's approved data controls. Batches and Files are not eligible for Zero Data Retention. Availability, model access, retention, and residency remain server-side policies.

Authentication, permission, validation, conflict, and transport errors retain the CLI's normal error presentation. Readable Batch errors state the outcome, the supported cause, and the next safe action. Unknown submission outcomes remain explicit because a disconnected request can still reach the API. Missing receipt IDs identify which response was incomplete. Check existing files or batches before repeating a submission. Connection failures suggest checking the connection, proxy, and --base-url setting. Partial input lists retain received records and identify the incomplete result. Retry that read-only search with the same connection and project settings. Explicit machine errors preserve API fields; submission errors add the documented recovery envelope. Local synthetic tests verify client behavior; they do not establish account entitlements or live server policy.