Challenge - 5 Problems
LangChain Chat History Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this LangChain chat history code?
Consider this code snippet using LangChain's
ConversationBufferMemory to store chat history. What will be the printed output?LangChain
from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory() memory.save_context({'input': 'Hello'}, {'output': 'Hi there!'}) memory.save_context({'input': 'How are you?'}, {'output': 'I am fine, thanks!'}) print(memory.load_memory_variables({})['history'])
Attempts:
2 left
💡 Hint
Look at how ConversationBufferMemory formats the history string with 'Human:' and 'AI:' prefixes.
✗ Incorrect
ConversationBufferMemory stores chat turns as a string with 'Human:' before inputs and 'AI:' before outputs, joined by newlines.
❓ state_output
intermediate2:00remaining
What is the value of the chat history after adding a new message?
Using LangChain's
ConversationBufferMemory, after saving these contexts, what is the value of memory.chat_memory.messages?LangChain
from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory() memory.save_context({'input': 'Hi'}, {'output': 'Hello!'}) memory.save_context({'input': 'What is your name?'}, {'output': 'I am LangChain.'}) messages = memory.chat_memory.messages
Attempts:
2 left
💡 Hint
Check the type of objects stored in
chat_memory.messages.✗ Incorrect
The
chat_memory.messages list contains message objects like HumanMessage and AIMessage with content strings.📝 Syntax
advanced2:00remaining
Which option correctly initializes a LangChain chat memory with a custom key?
You want to create a
ConversationBufferMemory that stores history under the key 'chat_history'. Which code snippet is correct?Attempts:
2 left
💡 Hint
Check the official parameter name for setting the memory key in ConversationBufferMemory.
✗ Incorrect
The correct parameter name is
memory_key. Other options use invalid parameter names causing errors.🔧 Debug
advanced2:00remaining
Why does this LangChain chat memory code raise an AttributeError?
Given this code, why does it raise
AttributeError: 'ConversationBufferMemory' object has no attribute 'messages'?LangChain
from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory() print(memory.messages)
Attempts:
2 left
💡 Hint
Check the structure of ConversationBufferMemory and where messages are stored.
✗ Incorrect
The
messages attribute is part of the chat_memory property, not directly on the memory object.🧠 Conceptual
expert3:00remaining
Which LangChain memory type best suits a chat app needing to remember only the last 3 exchanges?
You are building a chat app that should keep only the last 3 user and AI messages in memory to save space. Which LangChain memory class is best to use?
Attempts:
2 left
💡 Hint
Think about which memory type limits the number of recent messages stored.
✗ Incorrect
ConversationBufferWindowMemory keeps a sliding window of the last k messages, perfect for limiting history to last 3 exchanges.