Subworkflow fails in 2% of calls with Cannot read properties of undefined (reading 'map')

Hi, I am on “poor man’s n8n” (self-hosted private instance) and try to compose approx. 60 agents that collaborate with each other. Since having those many agents in one workflow does not scale, neither as individual nodes, nor as tool nodes, I setup each agent as workflow and have one workflow that coordinates them.

To make configuration of all those 60 sub-workflows more easy I introduced a couple of “variables” via “Edit Fields” node, which are then passed into each sub workflow. Among them is e.g. the model name to use for the AI agent in the sub workflow.

My challenge: approx. 2% of my sub workflow invocations fail with this error message: Cannot read properties of undefined (reading 'map').They always and everytime fail at the model of AI agent, if they fail.

Yet, the “variable” always holds the valid model name (actually, for testing I set it to the same across all sub-workflows). Here is a (simplified) version of one of my sub workflows, but even in this simplified version it fails:

In the model I tried it with {{$('Input').item.json.model}}, {{$('Input').first().json.model}}, and {{$('Input').last().json.model}} but without any effect. In 2% of invocations as sub-workflow this one fails.

I am on n8n v1.114.4 on Docker 28.4.0 on Ubuntu 24.04.2 . The n8n has always sufficient resources, i.e. CPU and Memory nevery exceed 20% of granted resources. –max_old_space_size is set to 16GB .

One addition I forgot to mention: when I set the model to the static value, it succeeds. Thus, I assume it has to do something with how the Expression is evaluated.

I think I found the problem and the solution in this thread: https://community.n8n.io/t/cannot-read-properties-of-undefined-reading-map/158143/2 .

So what I did is switching from Simple Memory to PostgreSQL memory and on every loop and error, run this SQL statement:
`DELETE FROM chat_memory WHERE message->>‘type’ IS NULL OR message->>‘content’ IS NULL OR message->>‘additional_kwargs’ IS NULL OR message->>‘response_metadata’ IS NULL;`