0
0
LangChainframework~20 mins

Sequential chains in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sequential Chain 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 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)
A"Hello"
BAn error because output variable names mismatch
C"Hello" (the original input)
D"Bonjour"
Attempts:
2 left
💡 Hint
Check how output variables are named and passed between chains in SequentialChain.
state_output
intermediate
2: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"])
AHello Alice\nGoodbye Alice
BSay hello to Alice\nSay goodbye to Alice
CHello Alice
DEmpty string because memory is not updated
Attempts:
2 left
💡 Hint
ConversationBufferMemory only saves if inputs contain 'input' (or input_key) and outputs contain 'output'.
📝 Syntax
advanced
2: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.
Achain = SequentialChain(chains=[LLMChain(llm=llm, prompt=PromptTemplate(template="Q: {q}", input_variables=["q"])), LLMChain(llm=llm, prompt=PromptTemplate(template="Q: {answer}", input_variables=["answer"]))], input_variables=["q"], output_variables=["final_answer"])
Bchain = SequentialChain(chains=[LLMChain(llm=llm, prompt=PromptTemplate(template="Q: {q1}", input_variables=["q1"])), LLMChain(llm=llm, prompt=PromptTemplate(template="Q: {q2}", input_variables=["q2"]))], input_variables=["q1"], output_variables=["answer"])
Cchain = SequentialChain(chains=[LLMChain(llm=llm, prompt=PromptTemplate(template="Q: {q}", input_variables=["q"])), LLMChain(llm=llm, prompt=PromptTemplate(template="Q: {q2}", input_variables=["q2"]))], input_variables=["q"], output_variables=["final_answer"])
Dchain = SequentialChain(chains=[LLMChain(llm=llm, prompt=PromptTemplate(template="Q: {q1}", input_variables=["q1"])), LLMChain(llm=llm, prompt=PromptTemplate(template="Q: {q1}", input_variables=["q1"]))], input_variables=["q1"], output_variables=["final_answer"])
Attempts:
2 left
💡 Hint
Check that the output variable of the first chain matches the input variable of the second chain.
🔧 Debug
advanced
2: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)
AThe second chain expects 'answer' input but the first chain outputs 'text', causing KeyError
BThe first chain's prompt template is missing a variable, causing KeyError
CThe output_variables list is empty, causing KeyError
DThe SequentialChain is missing the 'memory' parameter, causing KeyError
Attempts:
2 left
💡 Hint
Check the variable names passed between chains and their outputs.
🧠 Conceptual
expert
2:00remaining
What is the main advantage of using SequentialChain in LangChain?
Select the best explanation for why developers use SequentialChain in LangChain workflows.
AIt caches all LLM outputs to avoid repeated API calls.
BIt automatically parallelizes multiple LLM calls to improve performance.
CIt allows chaining multiple LLM calls where each step's output feeds the next, enabling complex multi-step reasoning.
DIt provides a graphical interface to design LLM workflows visually.
Attempts:
2 left
💡 Hint
Think about how outputs from one step can be inputs to the next in a chain.