Ghost Message in chat History

Describe the problem/error/question

I have a slack chatbot. That works great for the most part however I’m struggling to make it context aware.

Follow on threads load chat history from postgress. However, previous Ai agents appear to be “adding” 2 undefined messages to the chat history. On loading the history Anthropic/The Ai Agent states “Cannot read properties of undefined (reading ‘map’)”

The root of the issue appears to be in previous executions receiving the following message

"formatting_instructions": "IMPORTANT: For your response to user, you MUST use the `format_final_json_response` tool with your complete answer formatted according to the required schema. Do not attempt to format the JSON manually - always use this tool. Your response will be rejected if it is not properly formatted through this tool. Only use this tool once you are ready to provide your final answer.",
    "chat_history": [
      {
        "lc": 1,
        "type": "constructor",
        "id": [
          "langchain_core",
          "messages",
          "HumanMessage"
        ],
        "kwargs": {
          "content": "oo7-3658",
          "additional_kwargs": {},
          "response_metadata": {}
        }
      },
      {
        "lc": 1,
        "type": "constructor",
        "id": [
          "langchain_core",
          "messages",
          "AIMessage"
        ],

I’ve not seen the empty AI or Human message without the Format response message.

What is the error message (if any)?

Cannot read properties of undefined (reading ‘map’)
or For your response to user, you MUST use the format_final_json_response

Information on your n8n setup

  • n8n version : 1.84.1
  • Database: Postgres
  • n8n EXECUTIONS_PROCESS : Runners Enabled??
  • Running n8n : Docker, Helm Kube

I add a node that executes a postgres query deleting the ghost messages. It’s a very ugly work around.

DELETE FROM n8n_chat_histories
WHERE 
  (message::jsonb->'kwargs'->>'content' IS NULL)
  AND (message::jsonb->>'content' IS NULL);

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