# Split In Batches

> Splits an array into individual items or batches for parallel or iterative processing.

Source: https://docs.sommatic.ai/en/nodes/flow.split/

- Slug: `flow.split`
- Family: Flow control
- Version: 1.0.0
- Status: Available
- Execution: sync

Splits an array into individual items or batches for parallel or iterative processing.

This operator allows you to iterate over a list of items found in the input payload. For each item (or batch of items), it triggers the 'loop' output port. Once all items have been processed, it triggers the 'done' port.

**Behavior:**
- **Iteration:** Emits a message for each item/batch in the array.
- **Context Preservation:** Child executions inherit the parent context.
- **Batching:** Can group items into batches (e.g., size 10) for efficiency.

**Use Cases:**
- Processing a list of new users from a CSV.
- Sending emails to a segment of subscribers.
- Processing order line items individually.

## Configuration

- `path` (string, optional, accepts expressions): JSONata path to the array in the input payload (e.g. `data.items`). If empty, the root payload is treated as the array.
- `batch_size` (integer, optional): Number of items to include in each emitted batch. Default: `1`.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `out` (default): Emits for each item or batch in the array.
- `done`: Activated when all items have been processed.
- `error`: Activated if the input is not a valid array.

## Input contract

- `payload` (object, optional)

## Output contract

- `payload` (array<object>, optional): The batch of items.

## In expressions

What a later node can read from this node's output with `$("Split In Batches").item.json.<path>` (write your node's label instead of `Split In Batches`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Split In Batches").item.json.payload` (array): The batch of items. **The worker does not return it: reads as undefined.**
- `$("Split In Batches").item.json.order_id` (string) = `"ord_1"` (Not suggested by the editor)
- `$("Split In Batches").item.json.total` (number) = `10` (Not suggested by the editor)


## How it works

Fans out an array: the engine runs the nodes connected to `out` once per item (or batch), then signals `done` with the item count.

1. Resolves `path`. A `{{ }}` that returns an array or object (`{{ $json.items }}`) is used as is; plain text is evaluated as JSONata against the payload (`data.items`); an empty `path` uses the whole payload.
2. Recovers an object with numeric keys (`{ "0": {...}, "1": {...} }`) as an array in key order; an empty object becomes `[]`.
3. Fails when the value is not an array.
4. An empty array leaves directly through `done` with `{}`; nothing goes through `out`.
5. Otherwise returns the array with `meta.split` and `meta.batch_size`. The engine queues every node connected to `out` once per item (`batch_size` 1) or per slice of `batch_size` items, each with that item or slice as payload.
6. Then queues the nodes connected to `done` with `{ total_items }`, the number of items.

## In a workflow

### Node

```json
{
  "node_id": "node-each-order",
  "label": "Each order",
  "operator_slug": "flow.split",
  "operator_version": "1.0.0",
  "config": {
    "path": "{{ $json.orders }}",
    "batch_size": 1
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": [
    {
      "order_id": "ord_1",
      "total": 10
    },
    {
      "order_id": "ord_2",
      "total": 25
    },
    {
      "order_id": "ord_3",
      "total": 7
    }
  ],
  "meta": {
    "split": true,
    "batch_size": 1
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

Once per item (or batch array when `batch_size` > 1). The payload is the item itself.

```json
{
  "order_id": "ord_1",
  "total": 10
}
```

### `done`

After all items were queued. With an empty array the payload is `{}`.

```json
{
  "total_items": 3
}
```

### `error`

The value at `path` is not an array, or JSONata failed.

```json
{
  "ok": false,
  "errors": [
    "Target at path 'data.items' is not an array. Got: object. Keys found: [id, name]"
  ],
  "port": "error"
}
```

## Behaviour in the code

- `batch_size`: With a value above 1 each `out` payload is an array slice, but `done` still reports the number of items, so a `flow.join` downstream never fires.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Target at path '<path\|root>' is not an array. Got: <type>. Keys found: [<keys>]` | `error` | `path` points to an object, string or nothing. Check the path against the real payload. |
| `<JSONata message>` | `error` | Invalid JSONata in `path`; the message is in `data.error`. |

## Notes and limits

- Every node execution counts towards the engine limit of 100 steps per run; past it the run fails with `Max step ceiling reached (100)`. Items are processed one after another in queue order, not in parallel.
- `done` is queued right after the items reach their first node, so nodes on `done` can run before the item branches finish. Only `flow.join` waits for every item.
- When the envelope has no `data` (or `data` is falsy), the engine stores the whole envelope as the node output: on `error` the next node receives `{ ok: false, errors: [...], port: "error" }` as its payload.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "flow.split",
  "name": "Split In Batches",
  "category": "flow",
  "version": "1.0.0",
  "description": "Splits an array into individual items or batches for parallel or iterative processing.\n\n  This operator allows you to iterate over a list of items found in the input payload. For each item (or batch of items), it triggers the 'loop' output port. Once all items have been processed, it triggers the 'done' port.\n\n  **Behavior:**\n  - **Iteration:** Emits a message for each item/batch in the array.\n  - **Context Preservation:** Child executions inherit the parent context.\n  - **Batching:** Can group items into batches (e.g., size 10) for efficiency.\n\n  **Use Cases:**\n  - Processing a list of new users from a CSV.\n  - Sending emails to a segment of subscribers.\n  - Processing order line items individually.",
  "capabilities": [
    "loop",
    "iteration",
    "array",
    "fan-out"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "path": {
        "type": "string",
        "title": "Array Input Path",
        "description": "JSONata path to the array in the input payload (e.g. `data.items`). If empty, the root payload is treated as the array.",
        "format": "expression"
      },
      "batch_size": {
        "type": "integer",
        "title": "Batch Size",
        "default": 1,
        "minimum": 1,
        "description": "Number of items to include in each emitted batch."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "description": "Payload containing the array to split.",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "description": "A single item or a batch of items from the source array.",
    "properties": {
      "payload": {
        "type": "array",
        "items": {
          "type": "object"
        },
        "description": "The batch of items."
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Loop Item",
      "is_default": true,
      "description": "Emits for each item or batch in the array."
    },
    {
      "port_id": "done",
      "label": "Done",
      "description": "Activated when all items have been processed."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Activated if the input is not a valid array."
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "loop",
    "iterator",
    "fan-out",
    "batch"
  ],
  "is_stateful": true,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "path": {
      "type": "string",
      "title": "Array Input Path",
      "description": "JSONata path to the array in the input payload (e.g. `data.items`). If empty, the root payload is treated as the array.",
      "format": "expression"
    },
    "batch_size": {
      "type": "integer",
      "title": "Batch Size",
      "default": 1,
      "minimum": 1,
      "description": "Number of items to include in each emitted batch."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "description": "Payload containing the array to split.",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "description": "A single item or a batch of items from the source array.",
  "properties": {
    "payload": {
      "type": "array",
      "items": {
        "type": "object"
      },
      "description": "The batch of items."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Loop Item",
    "is_default": true,
    "description": "Emits for each item or batch in the array."
  },
  {
    "port_id": "done",
    "label": "Done",
    "description": "Activated when all items have been processed."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Activated if the input is not a valid array."
  }
]
```
