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.
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
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 : '' }}
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.
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;