Bird
0
0

What is the issue with this code snippet when trying to add a user message to chat history?

medium📝 Debug Q6 of 15
LangChain - Conversational RAG
What is the issue with this code snippet when trying to add a user message to chat history?
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.add_user_message('Hello')
AThe message should be added using memory.chat_memory.add_user_message
BConversationBufferMemory does not have an add_user_message method
CThe message must be added during initialization, not after
DThe message should be a dictionary, not a string
Step-by-Step Solution
Solution:
  1. Step 1: Understand method availability

    ConversationBufferMemory itself does not expose add_user_message directly; instead, its internal chat_memory attribute has this method.
  2. Step 2: Correct usage

    Use memory.chat_memory.add_user_message('Hello') to add a user message properly.
  3. Final Answer:

    The message should be added using memory.chat_memory.add_user_message -> Option A
  4. Quick Check:

    Use chat_memory attribute to add messages [OK]
Quick Trick: Add messages via chat_memory, not memory directly [OK]
Common Mistakes:
  • Calling add_user_message on ConversationBufferMemory directly
  • Passing wrong data types for messages
  • Trying to add messages before initializing memory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes