How to dynamically handle variable number of chat messages in prompt nodes without creating separate nodes for each length?

,

Describe the problem/error/question

I’m trying to pass my chat history correctly to the node. My question is how to pass dynamic number of variables to each chat message. The chat history can be any number. The example below shows how I can pass 3 chat messages, but let’s say the next time it will have 4 messages, how to automatically pass those messages to new prompt section.

The only way that I can think of now is to check the length of chat history and have predefined nodes for every length. But that seems excessive and repetitive. Is there any better way of achieving this?

Please share your workflow

This workflow shows how I can pass 1 system message, 2 user messages and 1 ai response.

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

Hey @matt_000,

Have you tried using a chat agent and adding memory to it? It sounds like that may solve the issue you are seeing.

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