Using previous item output in loop

Hello, :wave:

I am using a loop to generate content using the LLM node and I want my writing style to be consistent even though I am using loop to generate each section.

Right now, I am cheating by processing item 1 first, and looping all following items using this first processed item as guideline for the style of the content:

For various reasons (writing transitions from previous section, keeping numbering of headings consistent, etc.), I would like to loop using the output of the previous item if it exists.

:point_right: So I was wondering if there is a way to use the N-1 item output when generating output for item N in the loop.

Thank you very much for your advice!
Cheers,
Joachim

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

@Joachim_Brindeau , yes, you can achieve it by utilizing $runIndex, something like this assuming one item is iterated each time

{{ $('Loop Over Items').first(1, $runIndex - 1) }}

See Output of other nodes for details.

In short,

  • first(): applies to the first and hopefully single item in the loop
  • 1 refers to the output “loop” of the loop node
  • $runIndex refers to the current iteration (zero-based)

Therefore, you also need to add a check to prevent referencing the loop on the 1st iteration.

Hi @ihortom

Thanks a lot for this, it’s almost what I want.

For clarity, I try to retrieve the output of the N-1 item as processed by the loop, not as it enters the loop. Thus, if I use the output of the loop node, it does not quite work as I intended.

Please find here a minimal workflow to show where I am stuck:

The expression I am trying to use is as follows:
{{ $runIndex = 1 ? $('test').first(0, $runIndex - 1).json.text : '' }}

:point_right: How can I reference to the output of a node inside the loop without triggering the error below?

Referenced node is unexecuted
An expression references the node ‘section_generate’, but it hasn’t been executed yet. Either change the expression, or re-wire your workflow to make sure that node executes first.

I understand from @barn4k’s answer, on another post, that it’s unavoidable behavior at this stage

But I cannot figure out an alternative workflow for my needs.

Thank you very much for your help :smiley:
Cheers

Solved!

I asked Claude to help me write a script to concatenate the output of the loop node and the output of the last item’s test node in the same JSON.

Took a few trials (a lot if I’m being honest) but it works and I can properly reuse the output of any node within the loop from previous items.

Here is the workflow:

Here is the script:

const newItems = [];

for (let i = 0; i < items.length; i++) {
  const item = items[i];
  
  // Get the JSON from the "test" node of the previous item, if it exists
  let previousItemTestJson = null;
  if ($runIndex > 0) {
    previousItemTestJson = $('test').first(0, $runIndex - 1).json;
  }
  
  // Get the JSON from the last node of the current item in the loop
  const loopJson = item.json;
  
  // Create a new object with both JSON data
  const newItem = {
    previousItemTestJson: previousItemTestJson,
    loopJson: loopJson
  };
  
  newItems.push(newItem);
}

return newItems;

and here it’s working:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.