Skip to main content

Function: batch()

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

Defined in: packages/core/src/pipeline/fan-out.ts:232

Create a step that runs 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>

Configuration object with bot, concurrency, and failFast

Returns

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

A processable taking an array and resolving per-item results

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]);
// In a pipeline, with the whole batch abandoned on the first failure
const enrich = pipeline(
loadRows,
batch({ bot: enrichRow, concurrency: 5, failFast: true }),
saveRows
);