An asynchronous batch file can give you eleven results for twelve inputs, and the first result need not belong to the first request. The OpenAI Batch guide describes separate output and error files and tells clients to map results by custom_id. Keep the original request IDs as a manifest and account for each one explicitly before importing any generated text.

Prerequisites and complete example

Use a Node.js release with ES modules and the built-in assertion module. Save as batch.mjs. The no-argument demonstration uses invented records. In file mode, supply a JSON array of the original request IDs in submission order, then one or more locally saved JSONL result files. No SDK or network is needed.

import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';

function reconcile(ids, ...files) {
  if (!Array.isArray(ids) || ids.some(id => typeof id !== 'string' || !id)) {
    throw Error('expected an array of nonempty IDs');
  }
  const wanted = new Set(ids);
  if (wanted.size !== ids.length) throw Error('duplicate manifest ID');
  const found = new Map();
  for (const file of files) {
    for (const line of file.trimEnd().split(/\r?\n/)) {
      if (line === '' && file.trimEnd() === '') continue;
      const {custom_id: id, response, error} = JSON.parse(line);
      if (!wanted.has(id) || found.has(id)) throw Error('unknown or duplicate ID');
      if (response === null && typeof error?.code === 'string') {
        found.set(id, error.code);
      } else if (error === null && Number.isInteger(response?.status_code)
                 && response.status_code >= 100 && response.status_code <= 599) {
        found.set(id, 'HTTP ' + response.status_code);
      } else throw Error('invalid result envelope');
    }
  }
  return ids.map(id => [id, found.get(id) ?? 'missing']);
}

const args = process.argv.slice(2);
if (args.length) {
  if (args.length < 2) throw Error('give manifest and one or more result files');
  const [manifest, ...files] = args.map(p => readFileSync(p, 'utf8'));
  for (const row of reconcile(JSON.parse(manifest), ...files)) {
    console.log(JSON.stringify(row));
  }
} else {
  const ids = ['label-a', 'label-b', 'label-c', 'label-d'];
  const lines = [
    {custom_id: 'label-c', response: {status_code: 200, body: {}}, error: null},
  ].map(x => JSON.stringify(x)).join('\n');
  const errors = [
    {custom_id: 'label-b', response: null, error: {code: 'batch_expired'}},
    {custom_id: 'label-a', response: {status_code: 400, body: {}}, error: null},
  ].map(x => JSON.stringify(x)).join('\n');
  const report = reconcile(ids, lines, errors);
  assert.deepEqual(report, [['label-a', 'HTTP 400'], ['label-b', 'batch_expired'],
                            ['label-c', 'HTTP 200'], ['label-d', 'missing']]);
  assert.throws(() => reconcile(ids, lines, lines), /duplicate/);
  assert.throws(() => reconcile(['label-d'], lines), /unknown/);
  assert.throws(() => reconcile(ids, '{"custom_id":"label-a"}'), /envelope/);
  for (const [id, status] of report) console.log(id, status);
}

Run and interpret the report

node batch.mjs
label-a HTTP 400
label-b batch_expired
label-c HTTP 200
label-d missing

With saved files, run node batch.mjs ids.json output.jsonl errors.jsonl. Omit a result path if there was no corresponding export. This mode prints one JSON array per line, in manifest order. It reads all supplied records before printing the report, so a duplicate across files fails rather than silently letting the last row win. The HTTP 200 label records transport status from the envelope. It does not certify that the model body contains completed, usable content.

If a row is missing, check that the batch reached a terminal state and that you downloaded every available result file for the same batch. A missing row is not the same as an explicit expiration and is not by itself a reason to resubmit work. If an ID is unknown, verify the manifest and batch association before examining the body. For exports too large to fit comfortably in memory, switch to streaming line reads and keep the same duplicate and membership rules.

OpenAI Batch guide and result file examples · Java Samples Node.js tutorials