Gemini image generation with prompt

Describe the problem/error/question

Hi everyone,
I am facing an issue with a nested-like execution behavior inside a Loop Over Images node.

My Setup:

  1. Loop Over Images: Processes a list of images fetched from Google Drive one by one (Batch Size = 1).
  2. Inside the Loop: For each image, it creates a folder (Create Concept Folder) and downloads the image file.
  3. Item Expansion: Then, a Code node (Build Prompts) takes the single image data and generates 6 different prompt variations (successfully expanding 1 incoming item into 6 outgoing items).
  4. Processing: The subsequent nodes (Gemini data, HTTP Request, Upload Concept Image) execute 6 times perfectly for the first image.
  5. Loop Return: At the end of the execution chain, I use a Limit node set to Max Items = 1 to scale the 6 items back down to exactly 1 item before feeding it back into the Loop Over Images input port.

The Problem:

  • The first iteration works flawlessly (creates the folder, generates and uploads all 6 concept images).
  • On the second iteration (for the second image), the loop correctly triggers, and the Create Concept Folder node executes successfully.
  • However, right after creating the folder, the workflow completely stops. The subsequent nodes (Download Image, Build Prompts, etc.) do not execute at all for the second item.
    It seems n8n loses track of the execution sequence or item index during the second loop iteration because the item count was expanded from 1 to 6 inside the loop body, even though I used a Limit node to return exactly 1 item back to the loop counter.
    How can I properly reset the item context/index inside the loop so that the second iteration runs completely?
    (Note: I am attaching the screenshot of my workflow layout below)

What is the error message (if any)?

Please share your workflow


(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

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

Hi, this looks like a loop-structure issue rather than something you can fix by “resetting” the item index. The Limit node only reduces the number of items being passed forward; it does not restore the original outer-loop item context. In n8n, item linking matters when a node expands or transforms items, especially when expressions later depend on .item or previous-node data.

I would restructure it as a proper nested loop:

Outer loop: Loop Over Images
Create Concept Folder
Download Image
Build Prompts

Then inner loop: Loop Over Prompts
Gemini data
Generate Concept Image
Prepare Binary
Upload Concept Image
→ back to Loop Over Prompts

After the inner Loop Over Prompts finishes, connect its Done output back to the outer Loop Over Images node so the next image starts. Don’t feed one of the six generated prompt/image items back into the outer image loop. n8n’s Loop Over Items node is meant to process batches and then continue from the loop/done paths, and nodes often process lists automatically, so the item count should be controlled by the loop structure rather than patched with Limit.

Also, in the Build Prompts Code node, copy the original image/folder fields into each of the 6 generated prompt items, for example imageId, imageName, folderId, and downloaded file path. That way the upload step doesn’t need to rely on fragile outer-loop item references. If you are using .item expressions later, make sure the Code node preserves item linking/pairedItem, because n8n needs that link when one input item becomes multiple output items.

Hi @Gokhan_ARSLANTAS Welcome!
You can delete the loop entirely. Create Concept Folder already runs once per incoming image, so feed the Drive file list straight into it, then let one Code node emit every image x prompt combination as flat items (2 images, 6 prompts = 12 items), each carrying its own folderId, fileId and prompt:

const scenarios = ['scenario 1', 'scenario 2', 'scenario 3', 'scenario 4', 'scenario 5', 'scenario 6'];
const images = $('List Images').all();

return $input.all().flatMap((folder, i) => scenarios.map((s, n) => ({
  json: {
    folderId: folder.json.id,
    fileId: images[i].json.id,
    filename: `${images[i].json.name}-v${n + 1}.png`,
    prompt: `${images[i].json.name}, ${s}`
  }
})));

Swap List Images for the name of your Drive list node. Download Image, Gemini data, HTTP Request and Upload Concept Image then each run once per item, so the whole thing is one straight line with no loop node and no edge feeding back into anything.
If the image API rate limits you, throttle the HTTP Request node with Add Option > Batching (Items per Batch = 1, Batch Interval = 1000) rather than putting a loop back in.

Hello James, Thank you so much for your feedback. I tried but cannot fix the problem. I am also getting help also from AI agent to put info into the nodes. May be this is why I cannot. Best regards

Dear Anshul, I am going to try it now. Thank you for your feedback. Therefore, I am using per image for 6 different prompt. There is no two input images at the same time. Your solution still OK?

Hi @Gokhan_ARSLANTAS
Yes. One image just means the Code node emits 6 items instead of 12, and Download Image, Gemini data, HTTP Request and Upload Concept Image each run 6 times. Nothing else changes.
If it is always exactly one image, you can drop the index pairing and keep it flat:

const scenarios = ['scenario 1', 'scenario 2', 'scenario 3', 'scenario 4', 'scenario 5', 'scenario 6'];
const image = $('List Images').first().json;
const folderId = $input.first().json.id;

return scenarios.map((s, n) => ({
  json: {
    folderId,
    fileId: image.id,
    filename: `${image.name}-v${n + 1}.png`,
    prompt: `${image.name}, ${s}`
  }
}));

Leave the Code node on Mode: Run Once for All Items, which is the default, since the code returns the whole 6 item array itself.

I confirm James’s diagnosis, it’s exactly the classic problem of mixing loop levels in n8n.

The Limit node doesn’t “reset” anything from the outer loop’s context — it only filters how many items pass through. The Loop Over Images keeps expecting the flow that returns to its input to have the same “shape” (item lineage) as what came out, and when you stick a Code node that expands 1→6 in the middle, you break that lineage even if you later reduce it back to 1 with Limit.

The nested loop solution James proposes is the correct one. A couple of things to watch out for when you implement it:

  • In the Build Prompts Code node, make sure you explicitly return pairedItem in each of the 6 generated items, pointing to the index of the original item. If you don’t, any later expression that depends on parent item data (imageId, folderId, etc.) can fail silently instead of throwing an error.
  • The inner loop (Loop Over Prompts) needs its own Batch Size properly configured — if you accidentally leave it at the same size as the outer loop due to copy-paste error, you’re back to the same symptom.
  • Verify that the “Done” output of the inner loop (not the “Loop” output) is the one that connects back to the outer one — it’s a common mistake to connect the wrong port and end up stuck in an infinite or short loop.

With this it should resolve from the second iteration onwards.