Different pipes. One standard.

Type to search this site.

Search runs in your browser against a static index built with this site (Pagefind). Nothing you type is sent to a server.

Batch mode — let the runner hand your script a batch

Calling an API once per record is slow and expensive, and so is any script that pays a round trip for every line.

The problem #

A step that runs in another process pays for every record: a line to parse, a line to print, a round trip through two operating-system pipes. Three kinds of step feel it most. A request to a language model, an embedding service or a database costs per call, whatever the call carries. R and NumPy-style Python are fast on vectors and slow on scalars. And tools such as DuckDB want a file, not a stream of lines.

The usual answer is to buffer inside the program, with a home-made flush timer. Then every script reinvents it, and the runner — the program that runs pipes, and the place where waiting and memory are decided — knows nothing about it.

The fix #

The pipe says in its manifest that it wants its input in batches. The runner collects records and hands the script up to maxItems at a time, and never holds a record back for longer than maxWaitMs milliseconds. This is the manifest of the R example in the repository, with the port declarations left out:

yaml
cardinality: one-to-one
deterministic: true
batch:
  maxItems: 1000
  maxWaitMs: 200
# … inputs and outputs: one port each …
implementation:
  type: script
  language: r
  path: tidy.R
permissions:
  subprocess: true

maxItems is required. maxWaitMs is optional and defaults to 0: the runner never waits for a batch to fill. While the script works on one batch the runner keeps collecting the next, so a busy script gets fuller batches by itself.

The script reads one JSON array per line and answers each line with one JSON array. The answer line is also how the runner knows the batch is done, so the script flushes it. This is the loop of the same example, tidy.R; tidy is a vectorised function defined above it in the file:

r
con <- file("stdin", open = "r")
while (length(line <- readLines(con, n = 1, warn = FALSE)) > 0) {
  df <- fromJSON(line)                       # a batch: array of records -> data frame
  cat(toJSON(tidy(df), digits = NA), "\n", sep = "")   # one answer line per batch
  flush(stdout())
}

Some tools want a file. With delivery: file each batch is a plain NDJSON file, one record per line. If the command contains {batch.input}, the runner starts it once per batch and puts the path of the file there; {batch.output} is where the tool writes its result. This DuckDB pipe has no script at all ({{ is a literal {):

yaml
batch:
  maxItems: 50000
  maxWaitMs: 1000
  delivery: file
# … inputs and outputs …
implementation:
  type: executable
  command:
    - duckdb
    - -no-init
    - -bail
    - -c
    - >-
      copy (
        select id, qty, unitCents, taxRate,
               qty * unitCents as netCents,
               cast(round(qty * unitCents * taxRate) as bigint) as taxCents,
               qty * unitCents + cast(round(qty * unitCents * taxRate) as bigint) as grossCents
        from read_json('{batch.input}', format = 'newline_delimited',
                       columns = {{id: 'BIGINT', qty: 'BIGINT', unitCents: 'BIGINT', taxRate: 'DOUBLE'})
      ) to '{batch.output}' (format json)

Without {batch.output} the output of the batch is read from standard output. A command without placeholders is started once and is told the paths on standard input, one line per batch.

The precise terms #

  • Batch mode is declared with batch: {maxItems, maxWaitMs, delivery}. maxItems is required. maxWaitMs (default 0) and delivery (lines, the default, or file) are optional.
  • It is for pipes whose code lives outside the runner or in a script: executable pipes and script pipes (Starlark, R, Python, Node). A Starlark script defines process_batch(items, params) and always receives a list in memory; delivery: file is for executables and for R, Python and Node scripts. Built-in, expression, composite and HTTP pipes cannot declare batch mode.
  • The pipe must have exactly one input port, of kind records, values or windows.
  • A batch is delivered when it holds maxItems items, when maxWaitMs has passed since its first item arrived, or when the input ends. There is no minimum size, and one batch is in flight at a time.
  • Batch boundaries carry no meaning. A conformant pipe gives the same output, in the same order, however its input is cut. A sum or a rank across a batch is a bug. Grouping that means something is a window (std:window); a statistic over the whole input is scope: dataset.
  • Memory stays bounded: a node holds at most twice maxItems items. A pipeline node or a test may only tighten the settings, never loosen them.
  • Batch mode grants nothing. A pipe that sends batches to a remote service still declares network and remoteData. The examples in the repository call no network service; for an API the script has the same shape — one request per array instead of one per record.
  • Batch mode is experimental and in release v0.0.1 of the reference runner. It was accepted in RFC 0003; the wire format is in the CLI specification, sections 5.1 and 5.2.
  • Runnable examples: Python, R, Starlark and DuckDB with batch files.

Normative text: Pipe specification, section 5.7 (batch mode)