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:

{
  "name": "Minimal example of the Switch/Merge issue",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        140,
        260
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {json: {\"Type\": \"A\"}},\n  {json: {\"Type\": \"A\"}},\n  {json: {\"Type\": \"A\"}}\n]\n\n"
      },
      "name": "Generate sample data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        360,
        260
      ]
    },
    {
      "parameters": {
        "functionCode": "item.x = 1;\nreturn item;"
      },
      "name": "Process A's",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        780,
        140
      ]
    },
    {
      "parameters": {
        "functionCode": "item.shouldneverhappen = 1;\nreturn item;"
      },
      "name": "Process B's",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        780,
        340
      ]
    },
    {
      "parameters": {},
      "name": "Merge all results",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1000,
        240
      ]
    },
    {
      "parameters": {
        "dataType": "string",
        "value1": "={{$json[\"Type\"]}}",
        "rules": {
          "rules": [
            {
              "value2": "A"
            },
            {
              "value2": "B",
              "output": 1
            }
          ]
        }
      },
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        580,
        260
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Generate sample data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate sample data": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process A's": {
      "main": [
        [
          {
            "node": "Merge all results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process B's": {
      "main": [
        [
          {
            "node": "Merge all results",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Process A's",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Process B's",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": 23
}

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!