Skip to content

A ForEach node initializes its iterator once and completes on an empty array

The rule

Normative: this is the rule
  1. A ForEach node's iterator belongs to that node, so two ForEach nodes in one workflow never share an iterator, even where their loops merge (SG-18).
  2. A ForEach node MUST initialize its iterator where the state carries none for it.
  3. Resetting a loop the node is in (SG-18) clears its iterator, so a ForEach nested in another loop starts over from its first item each time the enclosing loop goes round.
  4. It requires items to iterate.
  5. A value that is not an array MUST be wrapped as a single item.
  6. The items MUST be re-indexed so that iteration is positional.
  7. An empty array MUST complete the node immediately rather than entering the loop.
  8. On completion the node MUST emit its defined output set, including the results collected across the rounds it ran.

What it means

An empty array is not a loop that runs zero times — it is completion on the first round, with the node's whole defined output set produced immediately, current item included as absent rather than left unset. A value that is not an array at all is not refused either: it is wrapped as the one item a single round iterates, re-indexed the same way any array would be, so a round that receives a bare scalar behaves exactly like a round over a one-item array.

Each ForEach node keeps its own iterator, so two ForEach nodes in one workflow never share one, even where their loops merge. When an enclosing loop goes round, it clears the iterators of the ForEach nodes nested in it, and a nested ForEach starts again from its first item.

Example

A ForEach launched with no items
{"items": []}
What it emits on that roundcompleted
{"loop_active": false, "current_item": null, "total_count": 0, "collected_results": []}
A ForEach launched with one scalar, not an array
{"items": "scalar_value"}
What it emits on that roundwrapped
{"current_item": "scalar_value", "current_index": 0, "total_count": 1, "is_first": true, "is_last": true}

Why

Recorded under OPEN-25, OPEN-58.

Rule identifiers are permanent and are never renumbered. Each implementation publishes its own standing against these rules; this specification does not.spec 1.0 · SG-4 · changed in spec 1.1