Unexpected behavior from unvisited nodes in a Switch-and-Merge workflow

I have a set of items that I want to process conditionally, and tried using a switch-and-merge pattern (using the Append mode) to do this. When the switch node outputs at least one item of each branch, the merge node behaves as expected. But when the Switch node outputs no items of some branch, the nodes in the unvisited branch still run.

Here is a minimal example:

Output returned by the last node

[
	{
		"Type": "A",
		"x": 1
	},
	{
		"Type": "A",
		"x": 1
	},
	{
		"Type": "A",
		"x": 1
	},
	{
		"shouldneverhappen": 1
	}
]

This feels like a bug, but perhaps I’m just misunderstanding something about how Merge is supposed to work.

n8n setup

  • n8n version: 0.156.0
  • Database you’re using (default: SQLite): postgresql
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Cloudron

Welcome to the community @ari !

Yes, that is currently expected behavior. The Merge-Node needs always both inputs to execute to move forward and executes the second by itself, if it did not before. There was recently another discussion about it here:

Additionally also here a feature request regarding this behavior:

1 Like

Thank you for the clarification, @jan! Based on this, I switched approaches from Switch-and-Merge to iterating over items using SplitInBatches, and was able to solve my problem.

Glad to hear that it was helpful and you found a solution. Have fun!

Hi, Ari. Would be great if you can share the alternative. I’m struggling with a Switch-Merge solution and I cannot figure it out. Thanks.

@Nelson_Paz_y_Mino Looping like this feels wrong, but at least it avoids the problem.

2 Likes

Thanks a lot, I found that calling other flows from my flow with simpler code and specific tasks also resolve the problem.

Fyi there is also an open PR about the Merge-Node behavior which will fix that issue once merged (planned with n8n version 1.0).

4 Likes

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

New version [email protected] got released which includes the GitHub PR 4238.

1 Like