Skip to main content

Function: batch()

batch<TBot>(config): Processable<unknown[], BatchItemResult<InferBotOutput<TBot>>[]>

Defined in: packages/core/src/batch.ts:72

Run a bot over a list of inputs under a concurrency limit, returning one outcome per item — failures included.

Use this instead of mapBot when a single bad item must not sink the run, or when the list is large enough that unbounded fan-out would trip a provider rate limit.

Type Parameters​

TBot​

TBot extends Processable<unknown, unknown>

The processable applied to each item; per-item output is inferred from it, so r.data is typed rather than unknown

Parameters​

config​

BatchConfig<TBot>

Returns​

Processable<unknown[], BatchItemResult<InferBotOutput<TBot>>[]>

Examples​

// Classify 500 tickets, 10 at a time, keeping partial results
const classifyAll = batch({ bot: classifyTicket, concurrency: 10 });

const results = await classifyAll.process(tickets);
const topics = results.filter((r) => r.success).map((r) => r.data);
const retry = results.filter((r) => !r.success && !r.notStarted).map((r) => tickets[r.index]);
// Through the providers' batch endpoints: one submission, polled until done
const classifyAll = batch({ bot: classifyTicket, providerBatch: true });
const results = await classifyAll.process(tickets);
// In a pipeline, with the whole batch abandoned on the first failure
const enrich = pipeline(
loadRows,
batch({ bot: enrichRow, concurrency: 5, failFast: true }),
saveRows
);