Nodes/KJNodes for ComfyUI/Open Tensor Loop
ComfyUI Node Runs on cloud

Open Tensor Loop

TensorLoop Open

By kijai·Created 3 years ago·Updated 2 days ago· 3,208
Open Tensor Loop
  • initial_value
  • flow_control
  • previous_value
  • accumulated_count
  • current_iteration
mode

If you've ever made a video model generate past its context window by hand, you know the ritual: take the last frames, feed them back in as the next iteration's start, recount, retrim, pray the seams don't show. TensorLoopOpen is the "loop header" end of a pair that automates all of it. It comes from kijai's ComfyUI-NativeLooping_testing repo, which the README calls what it is - a temporary testing ground for what ComfyUI's native loop nodes might look like. This is one of the two nodes in the pack you'll actually touch (the other is TensorLoopClose). The rest are hidden machinery.

What it actually is

TensorLoopOpen starts a loop that collects outputs. You pair it with TensorLoopClose: the Open node hands out loop control and per-iteration values, you run your generation inside the loop body, and the Close node collects what came out. It handles IMAGE, MASK, and LATENT - including video latents, which is where this gets interesting, because it understands 5D/nested video tensors and counts their frames, not just their batch items.

ComfyUI's execution model has no loops - nodes run once with whatever inputs they're given. This pack fakes a loop using ComfyUI's graph-expansion API (enable_expand): when TensorLoopClose runs, it expands into a subgraph that re-runs the loop body with the previous iteration's outputs fed back in, until a condition says stop. TensorLoopOpen's job is to hold the initial state and expose the values you need to actually use each iteration.

The inputs that matter

Only two inputs exist, and only mode is required. mode is the loop termination mode, with two sub-options:

  • iterations (default 4) - run a fixed number of iterations. Set it to 0 and the whole loop is bypassed: TensorLoopClose just passes initial_value through. Handy for switching a workflow between "loop" and "single pass."
  • total_frames (default 100) - keep looping until that many frames have accumulated across all iterations. This is the one you want for "I need a 20-second video but the model only makes 5-second chunks."

initial_value is optional and becomes previous_value on the first iteration - for a video continuation loop that's your starting image or starting latent.

The outputs

  • flow_control - the token you wire into TensorLoopClose. It's what ties the pair together; without it there's no loop.
  • previous_value - the value from the previous iteration (or initial_value on the first pass). This is the one you feed into your sampler as "the last frame we just made."
  • accumulated_count - how many items/frames have been collected so far (0 on the first iteration).
  • current_iteration - 1-based iteration index.

A minimal loop: previous_value → your i2v/last-frame conditioning → generated output → TensorLoopClose's processed. The countdown math is handled for you - in total_frames mode the internal counter goes negative and the iteration index still comes out right, so don't panic if you peek at internals and see odd numbers.

Install

Same story as every node in this pack:

cd ComfyUI/custom_nodes
git clone https://github.com/kijai/ComfyUI-NativeLooping_testing

…or search "NativeLooping" in ComfyUI Manager. Restart ComfyUI. There's no requirements.txt and nothing to download - but the pack imports comfy_api.latest and comfy.nested_tensor, which only exist in recent ComfyUI builds. If it fails to load, update ComfyUI before anything else.

Where people get burned

Remember this is a _testing repo. kijai is one of the most prolific custom-node devs in the scene (KJNodes, WanVideoWrapper, plus a string of core-ComfyUI contributions), but this is explicitly a candidate implementation, not a finished product - expect API changes and occasional breakage, and don't anchor a production pipeline to it yet. And if you're pairing it with TensorLoopClose, keep accumulate on if you use total_frames mode; total-frames counting is disabled when you're not accumulating, which exists precisely so the loop can't spin forever.

Categoryadvanced/looping

Inputs (2)

NameTypeDefaultDescription
modeCOMBOLoop termination mode.
initial_valueoptCOMFY_MATCHTYPE_V3Optional value to use as `previous_value` on the first iteration.

Outputs (4)

NameTypeDescription
flow_controlFLOW_CONTROL
previous_valueCOMFY_MATCHTYPE_V3The value from the previous iteration (or initial_value on first pass).
accumulated_countINTNumber of items collected so far (0 on first iteration).
current_iterationINTCurrent iteration index (1-based).