Merge items from two runs

Describe the problem/error/question

Hey, so I have my workflow where I call an LLM to process some tickets and then merge the output of the LLM with said tickets. That seems to work fine, but I have tried to implement retry logic because the LLM returns an error way too often. The problem lies in that it sometimes creates more than one run, that persists even after a Merge node (append), when the LLM returns an error.

What is frustrating is that I have executions where the LLM returns an error for some items, but the logic works as intended and all the runs get appended into one.

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

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

Thanks a lot :folded_hands:t4:

@Camila welcome to the n8n community!
Could you please share the error that was generated by the LLM, which made you alter the workflow?

another point, there are two retries in your workflow

Hello @tamy.santos ! I hope you’re doing well

The error state returned by the LLM was an item that looked like this:

image

About the retries, there are two control points (IF nodes), one checks if there was an error from the LLM and the other one checks if the max number of retries has been reached. I’m not sure if you refer to that

Hi @Camila Have you tried switching to a different model provider? Or an AI agent? Instead of Gemini?

Hello @Anshul_Namdev !

I’m trying to stay within Google’s environment for now, plus gemma is free to use and effective for my usecase (when it works).

I avoided using agents because I don’t know how they work and I could figure out the steps to create this myself, but if they work and this doesn’t, well :sweat_smile:

I have tested the flow, and it seems to be working fine!

And I can understand your problem with the AI being unpredictable that is why I would recommend that if you can, then consider using the AI agent where you can just specify a clear system prompt for behavioural control and can attach an output parser, which would help you regulate the unpredictability of the AI. Also, if your use for the AI is to extract the information, then I would highly recommend using the information extractor that would be very easy to set up.

Let me know how it goes.

Yeah, it’s inconsistent, which makes it stranger. I have executions where everything works, and a recent execution where it got divided into two runs :woozy_face:.

I changed the LLM node to not retry by itself if it fails (maybe that can cause the split?) and to return errors hopefully similar to when the LLM responds with an error, and just have my logic figure out the retries.

If that fails I’ll see how to set up an agent, thank you!

Edit: better to just Continue without using the error output :sweat_smile:.

I hope you’re doing well too @Camila :blush:
I would test disabling the Retry on Fail on the Message to model node itself, because today you already control the attempts in the workflow; keeping the node’s internal retry on can create additional passages and misalign the Merge, especially when using combineByPosition.

Excellent we seem to have reached the same conclusion! That’s great. I’ll give that a try and try agents if it keeps failing, thanks a lot!!

Come back to let us know, it helps other community members.

Alas, it has created two runs again :melting_face:

How should I set up an agent to do the retry logic? I don’t really know how they work..

@Camila, please share your new code.

There you go! Thank you @tamy.santos

Is the “If there is no error” condition connected to the correct path?
Please try setting it to false.

I think the issue is that the Merge node is not able to merge items from different runs.
On the second+ run, only one input of the Merge node will be affected.

Try this one

Ah, that explains it — the Merge node only merges data from the same execution run. In a retry loop, the second time the workflow fires, Input 2 of the Merge node is coming from the “new” run, but Input 1 is still waiting for data that will never arrive from the previous run. They aren’t running at the same time, so Merge just sits there with one input incomplete.

What barn4k posted is the right approach: ditch the Merge completely and use $(‘Reduce tokens (lossless)’).last().json to grab the data from the most recent finished run. That way you always have access to the previous state, without trying to merge live streams. The $runIndex trick for the retry limit is also cleaner than keeping your own CURRENT_RETRY counter — you just compare $runIndex to MAX_RETRIES.

So Camila, try importing barn4k’s workflow as-is. The only thing to watch out for: make sure your Message a model node has onError: continueErrorOutput set (not continueRegularOutput), because that’s what feeds the error branch now. If you have any issues after import, drop the exact error here and we’ll get it sorted.

Wow, that’s so much cleaner, thank you so so much! It seems to be working well now. I didn’t know that you could do that with .last(), I’m gonna do some reading.

Thank you all for your help!!!

Edit: I changed the .last() method to .itemMatching($itemIndex) so it can work if it has multiple items (as is my case, one item per ticket). Otherwise it seemed to grab the same item every time.