Challenge - 5 Problems
Sequential Chain 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 SequentialChain?
Consider this LangChain SequentialChain code that processes an input through two steps. What will be the final output printed?
LangChain
from langchain.chains import SequentialChain, LLMChain from langchain.llms import OpenAI from langchain.prompts import PromptTemplate llm = OpenAI(temperature=0) chain = SequentialChain( chains=[ LLMChain(llm=llm, prompt=PromptTemplate(template="Translate to French: {text}", input_variables=["text"])), LLMChain(llm=llm, prompt=PromptTemplate(template="Translate to English: {text}", input_variables=["text"])) ], input_variables=["text"], output_variables=["final_text"], verbose=False ) result = chain.run({"text": "Hello"}) print(result)
Attempts:
2 left
💡 Hint
Check how output variables are named and passed between chains in SequentialChain.
✗ Incorrect
The SequentialChain expects the output variable of the first chain to be the input variable of the second chain. Here, the output variable names are not aligned, causing a KeyError or mismatch error.
❓ state_output
intermediate2:00remaining
What is the final output of this SequentialChain with memory?
Given this SequentialChain with a memory buffer, what will be the final output stored in memory after running the chain?
LangChain
from langchain.chains import SequentialChain from langchain.llms import OpenAI from langchain.memory import ConversationBufferMemory from langchain.prompts import PromptTemplate from langchain.chains import LLMChain llm = OpenAI(temperature=0) memory = ConversationBufferMemory(memory_key="chat_history") chain1 = LLMChain(llm=llm, prompt=PromptTemplate(template="Say hello to {name}", input_variables=["name"])) chain2 = LLMChain(llm=llm, prompt=PromptTemplate(template="Say goodbye to {name}", input_variables=["name"])) seq_chain = SequentialChain( chains=[chain1, chain2], input_variables=["name"], memory=memory, verbose=False ) seq_chain.run({"name": "Alice"}) final_memory = memory.load_memory_variables({}) print(final_memory["chat_history"])
Attempts:
2 left
💡 Hint
ConversationBufferMemory only saves if inputs contain 'input' (or input_key) and outputs contain 'output'.
✗ Incorrect
The SequentialChain runs successfully, but after execution, memory.save_context is called with inputs={'name': 'Alice'} (lacks 'input' key) and outputs={'output': final result}. ConversationBufferMemory skips saving due to missing keys, leaving chat_history empty.
📝 Syntax
advanced2:00remaining
Which option correctly creates a SequentialChain with two LLMChains?
Select the code snippet that correctly initializes a SequentialChain with two LLMChains that use different prompts and share input/output variables properly.
Attempts:
2 left
💡 Hint
Check that the output variable of the first chain matches the input variable of the second chain.
✗ Incorrect
Option A correctly chains the output variable 'answer' from the first chain as input to the second chain, allowing sequential processing.
🔧 Debug
advanced2:00remaining
Why does this SequentialChain raise a KeyError?
This SequentialChain code raises a KeyError when run. What is the cause?
LangChain
from langchain.chains import SequentialChain from langchain.llms import OpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain llm = OpenAI(temperature=0) chain1 = LLMChain(llm=llm, prompt=PromptTemplate(template="Answer: {input_text}", input_variables=["input_text"])) chain2 = LLMChain(llm=llm, prompt=PromptTemplate(template="Summary: {answer}", input_variables=["answer"])) seq_chain = SequentialChain( chains=[chain1, chain2], input_variables=["input_text"], output_variables=["summary"], verbose=False ) result = seq_chain.run({"input_text": "Explain AI"}) print(result)
Attempts:
2 left
💡 Hint
Check the variable names passed between chains and their outputs.
✗ Incorrect
The first chain outputs a variable named 'text' by default, but the second chain expects 'answer' as input. This mismatch causes a KeyError when the second chain tries to access 'answer'.
🧠 Conceptual
expert2:00remaining
What is the main advantage of using SequentialChain in LangChain?
Select the best explanation for why developers use SequentialChain in LangChain workflows.
Attempts:
2 left
💡 Hint
Think about how outputs from one step can be inputs to the next in a chain.
✗ Incorrect
SequentialChain is designed to run multiple chains in order, passing outputs as inputs to the next, which helps build complex workflows with multiple reasoning steps.