Bird
Raised Fist0
LangChainframework~10 mins

Chat history management in LangChain - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a chat history object using LangChain.

LangChain
from langchain.memory import [1]

chat_history = [1]()
Drag options to blanks, or click blank then click option'
AChatMemory
BConversationBufferMemory
CConversationSummaryMemory
DMemoryBuffer
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class that summarizes instead of buffering the chat.
Using a class name that does not exist in LangChain memory module.
2fill in blank
medium

Complete the code to add chat history memory to a LangChain chat model.

LangChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory()
chat = ChatOpenAI(memory=[1])
Drag options to blanks, or click blank then click option'
Amemory
Bhistory
Cchat_memory
Dbuffer
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that does not exist in the constructor.
Passing the wrong variable name.
3fill in blank
hard

Fix the error in the code to retrieve the chat history as a string.

LangChain
history_str = memory.[1]
Drag options to blanks, or click blank then click option'
Abuffer
Bload_memory()
Cget_history()
Dchat_memory
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call a method that does not exist.
Using an incorrect attribute name.
4fill in blank
hard

Fill both blanks to create a chat memory that summarizes conversation instead of buffering.

LangChain
from langchain.memory import [1]

memory = [2](llm=llm)
Drag options to blanks, or click blank then click option'
AConversationSummaryMemory
BConversationBufferMemory
CChatMemory
DSummaryMemory
Attempts:
3 left
💡 Hint
Common Mistakes
Using the buffer memory class instead of summary memory.
Using a class name that does not exist.
5fill in blank
hard

Fill all three blanks to create a chat memory with a custom key and return messages enabled.

LangChain
memory = ConversationBufferMemory(
    memory_key=[1],
    return_messages=[2],
    output_key=[3]
)
Drag options to blanks, or click blank then click option'
A"chat_history"
BTrue
C"output"
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using booleans as strings or vice versa.
Using incorrect key names.

Practice

(1/5)
1. What is the main purpose of ConversationBufferMemory in Langchain chat applications?
easy
A. To store and manage past chat messages for context
B. To generate new chat messages automatically
C. To connect to external databases
D. To format chat messages for display

Solution

  1. Step 1: Understand the role of chat history

    Chat history helps keep track of previous messages to maintain context in conversations.
  2. Step 2: Identify what ConversationBufferMemory does

    This class stores and retrieves past messages so the chat app remembers what was said.
  3. Final Answer:

    To store and manage past chat messages for context -> Option A
  4. Quick Check:

    Chat history storage = A [OK]
Hint: Remember: ConversationBufferMemory saves past messages [OK]
Common Mistakes:
  • Thinking it generates new messages
  • Confusing it with database connectors
  • Assuming it formats messages for UI
2. Which of the following is the correct way to initialize a ConversationBufferMemory instance in Langchain?
easy
A. memory = ConversationBufferMemory.create()
B. memory = ConversationBufferMemory()
C. memory = ConversationBufferMemory.new()
D. memory = ConversationBufferMemory.load()

Solution

  1. Step 1: Recall the standard initialization pattern

    In Langchain, classes are usually instantiated by calling the class name with parentheses.
  2. Step 2: Check the options for correct syntax

    Only ConversationBufferMemory() follows this pattern; others are invalid or do not exist.
  3. Final Answer:

    memory = ConversationBufferMemory() -> Option B
  4. Quick Check:

    Standard class instantiation = D [OK]
Hint: Use parentheses to create instances: ClassName() [OK]
Common Mistakes:
  • Using .load(), .new(), or .create() which are not valid here
  • Forgetting parentheses when creating an instance
  • Confusing with static methods
3. Given this code snippet, what will print(memory.load_memory_variables({})) output?
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.chat_memory.add_user_message('Hello')
memory.chat_memory.add_ai_message('Hi there!')
medium
A. {'history': 'Human: Hello\nAI: Hi there!\n'}
B. {'history': 'Hi there!\nHello\n'}
C. {'history': ''}
D. Error: method not found

Solution

  1. Step 1: Understand how messages are added

    User and AI messages are added in order: 'Hello' then 'Hi there!'.
  2. Step 2: Check how load_memory_variables returns history

    It returns a dictionary with key 'history' and a string showing messages prefixed by 'Human:' and 'AI:' in order.
  3. Final Answer:

    {'history': 'Human: Hello\nAI: Hi there!\n'} -> Option A
  4. Quick Check:

    History shows messages in order with labels = C [OK]
Hint: History shows messages in order with 'Human:' and 'AI:' labels [OK]
Common Mistakes:
  • Mixing message order
  • Expecting empty history
  • Assuming method does not exist
4. What is wrong with this code snippet that tries to add chat history?
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.add_user_message('Hello')
medium
A. You must import a different class for adding messages
B. You need to call memory.save() before adding messages
C. The message text must be a list, not a string
D. ConversationBufferMemory has no method add_user_message directly

Solution

  1. Step 1: Check method availability on ConversationBufferMemory

    The method add_user_message is not directly on ConversationBufferMemory but on its chat_memory attribute.
  2. Step 2: Identify correct usage

    Correct usage is memory.chat_memory.add_user_message(), not memory.add_user_message().
  3. Final Answer:

    ConversationBufferMemory has no method add_user_message directly -> Option D
  4. Quick Check:

    Use chat_memory to add messages = B [OK]
Hint: Add messages via memory.chat_memory, not memory directly [OK]
Common Mistakes:
  • Calling add_user_message on memory instead of chat_memory
  • Thinking message must be a list
  • Assuming save() is required before adding
5. You want to build a chat app that remembers only the last 3 messages. Which approach using ConversationBufferMemory is best to achieve this?
hard
A. Use ConversationBufferMemory with a custom buffer size of 3
B. Use ConversationBufferMemory without changes; it stores all messages
C. Manually delete older messages from chat_memory after each addition
D. Create a new ConversationBufferMemory instance for every message

Solution

  1. Step 1: Understand ConversationBufferMemory limitations

    ConversationBufferMemory stores all past messages without a built-in parameter to limit the number of messages (unlike ConversationBufferWindowMemory).
  2. Step 2: Identify the best approach among options

    Manually deleting older messages from memory.chat_memory.messages after each addition (e.g., memory.chat_memory.messages = memory.chat_memory.messages[-3:]) keeps only the last 3.
  3. Final Answer:

    Manually delete older messages from chat_memory after each addition -> Option C
  4. Quick Check:

    Manual management for custom limits = B [OK]
Hint: Manually prune memory.chat_memory.messages to last 3 after adds [OK]
Common Mistakes:
  • ConversationBufferMemory lacks message count limit parameter (use WindowMemory for that)
  • Not limiting buffer and storing unlimited history
  • Recreating instance loses all prior context
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard