Bird
Raised Fist0
LangChainframework~10 mins

Question reformulation with history in LangChain - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Question reformulation with history
User asks new question
↓
Retrieve conversation history
↓
Combine history + new question
↓
Pass combined input to reformulation chain
↓
Generate reformulated question
↓
Return reformulated question for next processing
The system takes the new question and past conversation, combines them, then reformulates the question to include context.
Execution Sample
LangChain
from langchain.chains import ConversationChain
from langchain.llms import OpenAI

conversation = ConversationChain(llm=OpenAI())
response = conversation.predict(input="What is AI?")
This code uses LangChain's ConversationChain to reformulate a question considering conversation history.
Execution Table
StepActionInputOutputNotes
1User inputs new question"What is AI?"New question storedStart of interaction
2Retrieve conversation historyPrevious Q&A (empty initially)History retrievedNo prior history on first question
3Combine history + new question'' + 'What is AI?'Combined input: 'What is AI?'First question, so just the question
4Pass to reformulation chainCombined inputReformulated question: 'What is AI?'No change for first question
5Return reformulated questionReformulated questionOutput ready for next stepReady for answer generation
6User inputs follow-up question"How does it work?"New question storedSecond question in conversation
7Retrieve conversation historyPrevious Q&A: 'What is AI?'History retrievedHistory now includes first question
8Combine history + new question'What is AI?' + 'How does it work?'Combined input with contextContext added to new question
9Pass to reformulation chainCombined inputReformulated question: 'How does AI work?'Question reformulated with history
10Return reformulated questionReformulated questionOutput ready for next stepReady for answer generation
💡 Execution stops after reformulated question is returned for each user input.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
new_questionNone"What is AI?""How does it work?"NoneNoneNone
conversation_history''''"What is AI?""What is AI?""What is AI? + How does it work?""What is AI? + How does it work?"
combined_input''"What is AI?""What is AI? + How does it work?""What is AI?""What is AI? + How does it work?""What is AI? + How does it work?"
reformulated_question''"What is AI?""How does AI work?""What is AI?""How does AI work?""How does AI work?"
Key Moments - 3 Insights
Why does the first question not change after reformulation?
Because there is no prior conversation history, the reformulation chain returns the question as is, as shown in execution_table step 4.
How does the conversation history affect the reformulated question?
The history is combined with the new question before reformulation, adding context so the chain can produce a clearer question, as seen in steps 8 and 9.
What happens if the history is empty or missing?
The combined input is just the new question, so reformulation does not add context, resulting in the original question returned, as in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 9. What is the reformulated question?
A"How does it work?"
B"What is AI?"
C"How does AI work?"
D"What is AI? How does it work?"
💡 Hint
Check the 'Output' column at step 9 in the execution_table.
At which step does the system combine the conversation history with the new question for the second user input?
AStep 6
BStep 8
CStep 7
DStep 9
💡 Hint
Look for the step where 'Combine history + new question' happens for the second question.
If the conversation history was empty, how would the reformulated question at step 9 change?
AIt would be the same as the new question 'How does it work?'
BIt would be empty
CIt would remain 'How does AI work?'
DIt would combine both questions anyway
💡 Hint
Refer to the behavior at step 4 when history is empty.
Concept Snapshot
Question reformulation with history:
- Take new question + conversation history
- Combine them as input
- Pass to reformulation chain
- Get clearer question with context
- Use reformulated question for next steps
- First question returns as is (no history)
Full Transcript
This visual execution shows how LangChain reformulates a user's question by including conversation history. First, the user inputs a question. The system retrieves any past conversation history and combines it with the new question. This combined input is sent to the reformulation chain, which outputs a clearer, context-aware question. For the first question, with no history, the reformulated question is the same as the input. For follow-up questions, the history helps produce a better question. Variables like new_question, conversation_history, combined_input, and reformulated_question change step-by-step as shown. Key moments include understanding why the first question is unchanged and how history affects reformulation. The quiz tests understanding of these steps and outputs.

Practice

(1/5)
1. What is the main purpose of question reformulation with history in LangChain?
easy
A. To store user personal data securely
B. To speed up the chatbot response time
C. To translate questions into multiple languages
D. To help chatbots understand follow-up questions better

Solution

  1. Step 1: Understand the role of question reformulation

    It helps the chatbot by using previous conversation history to clarify follow-up questions.
  2. Step 2: Identify the main benefit

    This makes the chatbot understand user intent better and respond more naturally.
  3. Final Answer:

    To help chatbots understand follow-up questions better -> Option D
  4. Quick Check:

    Question reformulation = better follow-up understanding [OK]
Hint: Focus on improving chatbot understanding of follow-ups [OK]
Common Mistakes:
  • Confusing reformulation with speed optimization
  • Thinking it stores personal data
  • Assuming it translates languages
2. Which of the following is the correct way to create a ConversationChain in LangChain that uses question reformulation with history?
easy
A. chain = ConversationChain(llm=llm, memory=ConversationBufferMemory())
B. chain = ConversationChain(memory=llm, llm=ConversationBufferMemory())
C. chain = ConversationChain(llm=llm, history=ConversationBufferMemory())
D. chain = ConversationChain(buffer=llm, memory=ConversationBufferMemory())

Solution

  1. Step 1: Recall the correct parameter names

    ConversationChain expects an LLM instance as 'llm' and a memory object as 'memory'.
  2. Step 2: Match the correct syntax

    chain = ConversationChain(llm=llm, memory=ConversationBufferMemory()) correctly assigns 'llm=llm' and 'memory=ConversationBufferMemory()'. Others mix parameter names or use incorrect ones.
  3. Final Answer:

    chain = ConversationChain(llm=llm, memory=ConversationBufferMemory()) -> Option A
  4. Quick Check:

    Correct parameters = llm and memory [OK]
Hint: Remember 'llm' and 'memory' are required parameters [OK]
Common Mistakes:
  • Swapping 'llm' and 'memory' parameters
  • Using 'history' instead of 'memory'
  • Passing wrong argument names
3. Given the following code snippet, what will be the output after the second question?
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

llm = SomeLLM()
memory = ConversationBufferMemory()
chain = ConversationChain(llm=llm, memory=memory)

print(chain.run("What is AI?"))
print(chain.run("And how does it learn?"))
medium
A. The memory will reset after each question
B. The second answer will ignore the first question
C. The second answer will consider the first question's context
D. The code will raise a syntax error

Solution

  1. Step 1: Understand ConversationBufferMemory behavior

    This memory stores conversation history and passes it to the LLM for context.
  2. Step 2: Analyze the chain.run calls

    The second call to run uses the stored history, so the answer will reflect the first question's context.
  3. Final Answer:

    The second answer will consider the first question's context -> Option C
  4. Quick Check:

    Memory stores history = context-aware answers [OK]
Hint: ConversationChain with memory keeps context across calls [OK]
Common Mistakes:
  • Assuming memory resets automatically
  • Thinking each run is independent
  • Expecting syntax errors from correct code
4. Identify the error in this LangChain code snippet for question reformulation with history:
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

llm = SomeLLM()
chain = ConversationChain(llm=llm, memory=ConversationBufferMemory)

response = chain.run("Explain machine learning.")
print(response)
medium
A. The llm variable is not defined
B. ConversationBufferMemory is not instantiated with parentheses
C. The run method does not exist on ConversationChain
D. The print statement is missing parentheses

Solution

  1. Step 1: Check how ConversationBufferMemory is used

    It must be instantiated with parentheses to create an object, but here it's passed as a class.
  2. Step 2: Verify other parts

    llm is defined, run method exists, and print syntax is correct in Python 3.
  3. Final Answer:

    ConversationBufferMemory is not instantiated with parentheses -> Option B
  4. Quick Check:

    Instantiate classes with () [OK]
Hint: Always add () when creating memory instance [OK]
Common Mistakes:
  • Forgetting parentheses when instantiating classes
  • Assuming run method is missing
  • Confusing print syntax in Python 3
5. You want to improve a chatbot to handle multi-turn conversations with question reformulation using LangChain. Which approach best achieves this?
hard
A. Use ConversationChain with ConversationBufferMemory to keep history and reformulate questions
B. Use a simple LLM call without memory for faster responses
C. Store conversation history manually in a list and pass only the last question to the LLM
D. Use ConversationChain but reset memory after each question to avoid confusion

Solution

  1. Step 1: Understand multi-turn conversation needs

    Keeping conversation history helps the chatbot understand context and reformulate questions properly.
  2. Step 2: Evaluate options

    Use ConversationChain with ConversationBufferMemory to keep history and reformulate questions uses ConversationChain with memory, which automates history management and reformulation. Others either ignore history or reset it, losing context.
  3. Final Answer:

    Use ConversationChain with ConversationBufferMemory to keep history and reformulate questions -> Option A
  4. Quick Check:

    Memory + ConversationChain = best multi-turn handling [OK]
Hint: Use ConversationChain with memory for natural multi-turn chats [OK]
Common Mistakes:
  • Ignoring conversation history
  • Resetting memory too often
  • Manually managing history inefficiently
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