Invalid parameter: messages with role 'tool' must be a response to a preceding message with 'tool_calls' using Postgres Chat Memory

I am facing an issue with n8n’s persistent chat memory nodes and OpenAI tool calling, and I am completely stuck on how to resolve it for a production environment.

The Context & Version Change

  • Previous n8n Version: 2.11.3 (Everything worked perfectly)
  • Current n8n Version: 2.30.0 (The issue appeared immediately after upgrading to this version)
  • LLM Node: Azure OpenAI Chat Model (gpt-4o) connected to an AI Agent node with several custom HTTP Request tools.

What I have already tried to debug:

  1. The Database Switch: Originally, I was using MongoDB Chat Memory when this started happening. I recently migrated the workflow completely over to a freshly configured Postgres Chat Memory instance. The exact same crash still happens.
  2. Fresh Sessions: If I change the Session ID to a brand-new string or try a different user account completely, the first turn works, but it breaks again 2-3 conversations later.
  3. The Quick Fix (Simple Chat Memory): As a temporary workaround, I swapped out the persistent databases and used the built-in Simple Chat Memory node. When using Simple Chat Memory, the workflow runs perfectly without ever throwing this error.

Since Simple Chat Memory stores conversations in active RAM, I cannot use it for our production environment where persistent chat histories are mandatory. It seems like the persistent memory nodes (MongoDB/Postgres) are saving or retrieving the LangChain tool message array format incorrectly, truncating it, or dropping the critical tool_call_id metadata needed by OpenAI on subsequent turns.

Any help or structural workarounds would be highly appreciated!

Error message

Bad request - please check your parameters. Invalid parameter: messages with role ‘tool’ must be a response to a preceding message with ‘tool_calls’.

Information on your n8n setup

  • n8n version: 2.30.0 (self hosted)
  • Database (default: SQLite): yes
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker

@Prathvika

This sounds like a chat model regresssion in making tool calls.
Before you file a bug report, can you try updating to the latest stable version of n8n?

Hi @Prathvika Welcome!
Postgres and MongoDB Chat Memory write the agent’s tool-call and tool-result messages into the table as their own rows, and Context Window Length replays a slice of those rows on the next turn, so once the slice starts on a tool row the assistant message carrying its tool_calls is no longer in front of it and Azure rejects the payload. That is why a fresh session survives the first turn and fails two or three turns later.
For production, keep Postgres but write to it yourself instead of connecting it to the agent:

  1. Disconnect the Postgres Chat Memory node from the AI Agent and connect it to a Chat Memory Manager placed before the agent, Operation Mode Get Many Messages with Simplify Output on, then feed that list into the agent’s prompt as the earlier conversation.
  2. After the agent, add a second Chat Memory Manager on the same Postgres Chat Memory, Operation Mode Insert Messages, Insert Mode Insert Messages, with one User message holding the chat input and one AI message holding the agent output.

The table then only holds user and ai rows, so there is no tool row left to replay.

Thank you, @Anshul_Namdev! I will try this setup.

Thank you for the feedback, @kjooleng! I will definitely try this out.

We have taken a look at this and it looks like it might be fixed in a recent release, Please update and see if you are still running into the same problem.

Thank you! Will try updating