Resolving 'messages with role "tool" must be a response to preceding "tool_calls"' in n8n
The error indicates a message sequencing contract violation when using tool-enabled conversational flows - messages labeled role:'tool' must follow a prior tool invocation that requested a tool response. This is typically an API or integration mismatch and requires ensuring your message envelopes follow the provider's tool-calling protocol.
The community error - "messages with role 'tool' must be a response to a preceeding message with 'tool_calls'" - reflects a semantic mismatch between how the automation constructs chat messages and the expectations of a tool-enabled LLM API. Tool messages are not free-form responses; they must be produced in reaction to an explicit tool invocation in the message history (a 'tool_calls' flag or similar). Sending tool-role messages outside that protocol triggers a bad request.
Practically, this happens when a workflow inserts a tool-result object into the message array without first issuing the 'call tool X' instruction or when there is a version mismatch between the integration node and the target API. In n8n flows, ensure the sequence is: user prompt -> model issues tool call -> system or external tool executes -> tool response is appended. Any deviation risks malformed requests.
To remediate: (1) Verify you are using the correct node version and API contract for the target LLM provider. (2) Inspect the serialized message array sent in the API call and confirm that tool-role entries are only present after an explicit tool_calls marker. (3) Add validation logic in the workflow that enforces the required ordering before sending payloads. (4) If using custom code, add unit tests that simulate the tool-calling lifecycle.
For leaders building tool-enabled conversational products: codify API contracts between orchestration layers and model endpoints, version-pin critical nodes, and create integration tests that cover tool calls. These practices reduce intermittent failures and accelerate safe deployment of complex multi-component LLM workflows.
Original Source
n8n Community
