Bird
Raised Fist0
LangChainframework~20 mins

Question reformulation with history in LangChain - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
LangChain Question Reformulation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this LangChain question reformulation code?
Given the following LangChain code snippet that reformulates a follow-up question using chat history, what will be the output when the input question is "What is the capital?" and the chat history contains one previous question-answer pair?
LangChain
from langchain.chains import TransformChain
from langchain.prompts import PromptTemplate

prompt_template = """
Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.

Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:"""
prompt = PromptTemplate(input_variables=["chat_history", "question"], template=prompt_template)

chain = TransformChain(input_variables=["chat_history", "question"], output_variables=["rephrased_question"], transform=lambda inputs: {"rephrased_question": f"Standalone: {inputs['question']}"})

inputs = {"chat_history": "Q: Who is the president? A: Joe Biden.", "question": "What is the capital?"}
output = chain.run(inputs)
print(output)
A{"rephrased_question": "What is the capital?"}
B{"rephrased_question": "Standalone: What is the capital?"}
CSyntaxError due to missing colon in lambda
DKeyError because 'question' key is missing in inputs
Attempts:
2 left
💡 Hint
Look at how the lambda function formats the output dictionary using the input question.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in this LangChain prompt template?
Consider the following prompt template string for question reformulation. Which option contains a syntax error that will prevent the PromptTemplate from being created?
LangChain
prompt_template = """
Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.

Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:"""
Aprompt = PromptTemplate(input_variables=["chat_history", "question"], template=prompt_template)
Bprompt = PromptTemplate(input_variables=["chat_history", "question"], template=prompt_template + "\n")
Cprompt = PromptTemplate(input_variables=["chat_history", "question"], template=prompt_template[:-1])
Dprompt = PromptTemplate(input_variables=["chat_history", "question"], template=prompt_template.replace('{question}', '{follow_up}'))
Attempts:
2 left
💡 Hint
Check if the input variables match the placeholders in the template string.
❓ state_output
advanced
2:00remaining
What is the value of 'rephrased_question' after running this LangChain chain?
Given a LangChain TransformChain that reformulates a question by appending chat history, what is the value of 'rephrased_question' after running with the inputs below?
LangChain
from langchain.chains import TransformChain

chain = TransformChain(
    input_variables=["chat_history", "question"],
    output_variables=["rephrased_question"],
    transform=lambda inputs: {"rephrased_question": f"{inputs['chat_history']} Then ask: {inputs['question']}"}
)

inputs = {"chat_history": "Q: Who won the game? A: Team A.", "question": "What was the score?"}
output = chain.run(inputs)
rephrased = output["rephrased_question"]
A"Q: Who won the game? A: Team A. Then ask: What was the score?"
B"Then ask: What was the score? Q: Who won the game? A: Team A."
C"Q: Who won the game? A: Team A. What was the score?"
DKeyError because 'rephrased_question' is not in output
Attempts:
2 left
💡 Hint
Look at how the lambda formats the string using chat_history and question keys.
🔧 Debug
advanced
2:00remaining
Which option causes a runtime error when running this LangChain chain?
Given this LangChain TransformChain code, which option will cause a runtime error when calling chain.run with the provided inputs?
LangChain
from langchain.chains import TransformChain

chain = TransformChain(
    input_variables=["chat_history", "question"],
    output_variables=["rephrased_question"],
    transform=lambda inputs: {"rephrased_question": inputs["question"].upper()}
)

inputs = {"chat_history": "Previous Q&A", "question": "hello"}
Achain.run(inputs)
Bchain.run({"chat_history": "Previous Q&A", "question": None})
Cchain.run({"chat_history": "Previous Q&A"})
Dchain.run({"question": "hello"})
Attempts:
2 left
💡 Hint
Check which inputs dictionary is missing required keys.
🧠 Conceptual
expert
3:00remaining
How does LangChain's question reformulation with history improve conversational AI?
Why is it important to reformulate follow-up questions into standalone questions using chat history in LangChain conversational chains?
AIt allows the language model to understand the context without needing the entire conversation history, improving accuracy and relevance.
BIt reduces the number of tokens sent to the model by removing all previous conversation context.
CIt enables the model to generate questions instead of answers, improving engagement.
DIt stores the entire conversation history in a database for later retrieval.
Attempts:
2 left
💡 Hint
Think about how standalone questions help the model understand context better.

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