Fold
Squish a list down into one value
- function
- initial
- items
- *
Fold is the accumulator. Map turns a list into another list; Fold turns a list into one value. It's functools.reduce wearing a ComfyUI mask - you start with an initial value, then walk the list one element at a time, updating the accumulator with each one. Sums, string concatenation, chained transforms, "run this pass over the whole history": that's Fold territory.
The inputs
function- aCLOSUREthat takes two arguments: the current accumulator and the current element, in that order.initial- the starting accumulator, any type.items- theLISTyou're reducing (Basic Data Handling's LIST, not the native Data List).- output - the final accumulator after every element has been consumed.
Concrete example: a function (acc, img) that concatenates an image into a growing list, initial = empty LIST, and Fold stacks a batch of images into one result. Or a function that adds acc + element and you've built a sum node from scratch.
How it works
Same coroutine machinery as Map, but the loop is different: the accumulator from the previous iteration feeds into the next call. So the sequence is f(initial, items[0]) → f(result1, items[1]) → … until the list runs out. Under the hood each step expands the function's body into real graph nodes with the previous result threaded through - which means every step re-runs the whole body, no caching. That's the honest cost of a reduce in this pack, and it's why Fold over a long list with a heavy body will feel slow. Budget for it.
The COMFYUI_FUNCTIONAL_COROUTINE_LIMIT (default 100) caps iterations; long lists trip it, and you'd raise it with the env var if you're folding over hundreds of elements.
The classic trap: infinite loops inside the body
The README's troubleshooting sheet is blunt about this: "Add counters into your Fold bodies." Because your function runs per element, a body that itself calls back into a loop or a recursion with no stop condition can spin forever. Fold's own iteration count is bounded by list length, but nothing bounds what the body does per step. If a Fold workflow freezes, look inside the function first.
Where people get burned
- Native Data Lists → deadlock. Convert to LIST.
- Argument order - it's
(accumulator, element), not the other way around. If your results look scrambled or crash with an index error, that's the usual suspect. - Heavy bodies - Fold multiplies cost by list length, and there's no caching. Don't fold a model load.
Installing it
Ships in Duanyll/comfyui_functional. ComfyUI Manager: search "Duanyll/comfyui_functional", or:
cd ComfyUI/custom_nodes
git clone https://github.com/Duanyll/comfyui_functional
# restart ComfyUI
No models, no pip deps; grab Basic Data Handling for the LIST type. Fold is one of the pack's genuinely useful nodes - just keep the body cheap.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| function | CLOSURE | — | |
| initial | * | — | |
| items | LIST | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| * | * | — |