Bird
0
0

Given this code snippet, what will print(memory.load_memory_variables({})) output?

medium📝 Predict Output Q4 of 15
LangChain - Conversational RAG
Given this code snippet, what will print(memory.load_memory_variables({})) output?
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(return_messages=True)
memory.chat_memory.add_user_message('Hello')
memory.chat_memory.add_ai_message('Hi there!')
A{"history": "Human: Hello\nAI: Hi there!"}
BAn error because add_user_message is not a valid method
C{"history": []}
D{"history": ["Human: Hello", "AI: Hi there!"]}
Step-by-Step Solution
Solution:
  1. Step 1: Understand return_messages=True effect

    With return_messages enabled, load_memory_variables returns a dict with 'history' key holding a list of messages.
  2. Step 2: Check how messages are added

    add_user_message and add_ai_message add messages as strings prefixed with 'Human:' and 'AI:'.
  3. Final Answer:

    {"history": ["Human: Hello", "AI: Hi there!"]} -> Option D
  4. Quick Check:

    Return messages list format = {"history": ["Human: Hello", "AI: Hi there!"]} [OK]
Quick Trick: return_messages=True returns list of messages in history [OK]
Common Mistakes:
  • Expecting a single string instead of list
  • Thinking methods add_user_message do not exist
  • Assuming empty history

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes