ConversationChain fails when combining Redis memory with custom ChatPromptTemplate

I’m attempting to establish a conversation chain that employs Redis to keep track of chat history alongside a personalized prompt template, but I’m facing some persistent problems.

const chatMemory = new BufferMemory({
  chatHistory: new RedisChatMessageHistory({
    sessionId: "user_session_456",
    sessionTTL: 600,
    config: {
      URL: "redis://localhost:6379", 
    },
  }),
  memoryKey: "chat_history",
});

const promptTemplate = ChatPromptTemplate.fromPromptMessages([
  SystemMessagePromptTemplate.fromTemplate(
    "You are a helpful assistant."
  ),
  new MessagesPlaceholder("chat_history"),
  HumanMessagePromptTemplate.fromTemplate("{user_input}"),
]);

const conversationChain = new ConversationChain({
  memory: chatMemory,
  prompt: promptTemplate,
  llm: languageModel,
});

const result = await conversationChain.call({
  user_input: userQuery,
});

When I execute this code, the error message._getType is not a function appears. Oddly enough, if I set up the ConversationChain without the custom prompt template, everything functions correctly. This leads me to believe that there may be an issue with how I’m managing the history placeholder or formatting the messages.

Has anyone else faced this challenge? What could be the underlying cause of this type error?

Yeah, I’ve hit this too. Redis saves messages as JSON, but when they come back they lose all the LangChain message methods. Set returnMessages: true in your BufferMemory config - that usually fixes the deserialization. Also double-check your Redis version since older ones can mess up message formatting.

Hit this exact same problem last year building our internal chat system. BufferMemory messes up the message flow between Redis and custom prompt templates.

Your Redis setup’s fine, but BufferMemory wants messages formatted a specific way with MessagesPlaceholder. Switch to ConversationBufferWindowMemory - it handles Redis deserialization way better with custom templates.

const chatMemory = new ConversationBufferWindowMemory({
  chatHistory: new RedisChatMessageHistory({
    sessionId: "user_session_456",
    sessionTTL: 600,
    config: {
      URL: "redis://localhost:6379",
    },
  }),
  memoryKey: "chat_history",
  k: 10, // keeps last 10 exchanges
  returnMessages: true
});

The k parameter caps memory size and returnMessages: true gives you proper message objects. This fixed our _getType error because ConversationBufferWindowMemory has better message validation baked in.

Stuck with BufferMemory? Add a custom message transformer in your chain preprocessing step.

Had this exact problem 6 months ago while building a customer support chatbot. It’s a message serialization issue between Redis and ChatPromptTemplate. Messages lose their LangChain class structure when you pull them from Redis - they become plain objects missing methods like _getType(). Here’s what worked: add a validation step before feeding history to your prompt template. Make sure each message is properly instantiated as HumanMessage or AIMessage, not raw JSON. Wrap your conversation chain call in try-catch and log what’s actually coming out of Redis - you’ll see the class methods are missing. Converting them back to proper message instances fixed it completely for me.

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