Loop execution order goes crazy!

Describe the problem/error/question

I’m trying to create a workflow where a sequence of lipsync videos are generated through a loop passing by all the given files found in a given folder. It goes like this (in short) - “for each image and audio files you find in the given folders, do this”:

  1. Read the first image you find in the folder “inputs/img”;
  2. From this image, create a similar image (save the output in folder “outputs/img”);
  3. From the generated image, generate a video (save the ouput in folder “ouputs/video”);
  4. Now, read the first audio file you find in the folder “inputs/audio”, send it to my Supabase bucket, and get the output URL;
  5. Merge both inputs (audio and video URL’s) and send them to the lipsync generation;
  6. Save the output in folder “outputs/lipsync”;
  7. Go back to the beginning and start over from the next img / audio files.

The input folders “input/img” and “input/audio” are filled with img and audio files named as “01.png” / “01.mp3”; "02.png / “02.mp3” and so on.

The problem is that after the first run, the loops go crazy and starts to go back and forth through random nodes, making requests for lipsync and vide generation, making me waste my API credits!

What am I doing wrong, and how can I fix it? Should I abandon loops and make the steps iterate through another way?

P.S.: I’m using V0 legacy Execution Order, so the Merge node wait for both inputs to be ready before continue to the next node.

What is the error message (if any)?

No error message

Please share your workflow

Since I unfortunatelly can’t share my workflow here due to character limitation, I’ll paste a screenshot here:

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.109.2
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Windows 10
1 Like

It would be nice an image with the workflow, or part of the workflow that makes issues posted .

I was trying to share the workflow, but it’s saying that I’ve reached character limit =/

Pasted a screenshot instead.

Yea… it seems is quite big.

Then an image… you explained very well the situation, but usually an image or workflow makes more than 1000 words :slight_smile:

1 Like

Reset option that can be enabled. When turned on, the node will reset with the current input data newly initialized with each loop. This is useful when you want the Loop Over Items node to treat incoming data as a new set of data instead of a continuation of previous items.

This ensures the loop resets when new data arrives from the specified node, but not when the loop feeds itself, preventing infinite loops.

After the confirmation of lipsync generation will be a good signal.

And another thing, can you post another image with execution ?

Wonder which branch executes first and the order… Just hurt my brain lol

1 Like

Hmmm, this could really do the trick! I noticed that after the first complete loop run, all the “check” green icons of executed nodes were still there! Maybe the second and third loops are “jumping” straight forward to video and lipsync generation nodes, since the previous outputs are “already done”.

So this is the “Reset” option you are talking about, right?

I shall give it a try. But first I’ll have to wait for my next payment, since this crazy loop wasted all my API credits and now I have barely $ 1 left to run more tests :cry:

As for the execution order, it goes like this:

The red arrows stands for the workflow of input branches that “feeds” the Merge node. The yellow arrows stands for the workflow from Merge node output, and finally, the purple arrows stands for the feedback branches that goes back to the loop nodes (one loop to iterate between image files, and other to iterate between audio files).

Now thinking about the “Reset” option… I’m afraid that it resets even the “Read files” input, so the loop keeps running over files “01.png” and “01.mp3” over and over! :face_with_crossed_out_eyes:

Nope, it will get next item from loop until is done :grinning_face: .

U can use pinned data to test each node.

No need for real run (just pin data on the nodes that requires $).

Glad that I could point in a direction. :slight_smile: And yea… I am still thinking about the timing as well for your branches… actually the both loops should start with new data( but how do you know that audio url finishes first, then video starts… that’s why I mentioned about “confirmation when lipsync is done, so it can be triggered the flow again(image, audio and video), 3 separate jobs that has to be in sync …

1 Like

I made a minor test, leaving only the image generation branch, to validate the loop in a cheaper way :sweat_smile:

Even with “Reset” option enabled, the loop kept using only the first image source file to generate the variations :confused:

Here’s the code:

I managed to solve half of the problem by restricting the loop only within the “Write img to HD” node, and using the “{{ $runIndex }}” parameter in “Write img to HD” node to save the files, so they are now saved using the loop run sequence as reference, saving the files as “0.jpg”; “1.jpg”; “2.jpg” and so forth!

Now the main problem is about the “If/Else” loop for checking image status. Once the images are not generated all at once, sometimes there are images that get ready within the first 5 seconds after the “Wait” node, passing through the “If/Else” node and going forward (images that got the “COMPLETED” status), and some images that requires a second run (images that got “IN_PROGRESS” status on the first run). The problem is that generating an output with 2 or more runs causes a bug where only one of the runs reaches the final “Write img to HD” node.

So, let’s say that are 6 generated images getting out from the “GENERATE IMAGE” node, but when the “Check Image Status” node is first executed, after the first 5s “Wait” node, only 4 images are ready - or, in other words, only 4 get the “COMPLETED” status - and therefore, passes through the “If/Else” node, reaching the “IMAGE OUTPUT” node. The 2 images left get ready after the second run, reaching the “IMAGE OUTPUT” afterwards, creating a 2nd run item - which won’t be passed on to the “Write img to HD” node.

So, how can I solve this problem? I think that the better way to do this is using a node after the “If/Else” node that waits until all the requested images are ready (all get “COMPLETED” status) and join all the "response_url"s in a single JSON before passing to the next node. But how can I do that? I thought use the “Merge” node (“Append” mode), but it requires two inputs. Any ideas?

After your “If/Else” node, connect the “Done” output of your loop to an Aggregate node.

Configure the Aggregate node to collect the relevant fields (e.g., “response_url”) into an array.

Pass the output of the Aggregate node to your “Write img to HD” node or any further processing… and this should solve it :slight_smile: