Sequential chains help you run multiple steps one after another, passing results from one step to the next. This makes complex tasks easier by breaking them into simple parts.
Sequential chains in LangChain
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
LangChain
from langchain.chains import SequentialChain chain = SequentialChain( chains=[chain1, chain2, ...], input_variables=["input1", "input2"], output_variables=["output1", "output2"] ) result = chain.run({"input1": value1, "input2": value2})
You create a SequentialChain by giving it a list of smaller chains.
Input variables are the starting data you provide, output variables are what you want back at the end.
Examples
LangChain
from langchain.chains import SequentialChain # Define two simple chains chain1 = SomeChain() chain2 = AnotherChain() # Create a sequential chain seq_chain = SequentialChain( chains=[chain1, chain2], input_variables=["text"], output_variables=["summary"] ) result = seq_chain.run({"text": "Hello world"})
LangChain
seq_chain = SequentialChain(
chains=[chainA, chainB, chainC],
input_variables=["question"],
output_variables=["answer"]
)
answer = seq_chain.run({"question": "What is AI?"})Sample Program
This program first summarizes the input text, then translates the summary to French using two chains connected sequentially.
LangChain
from langchain.llms import OpenAI from langchain.chains import LLMChain, SequentialChain from langchain.prompts import PromptTemplate # Create a prompt template for first chain template1 = "Summarize this text: {text}" prompt1 = PromptTemplate(input_variables=["text"], template=template1) # Create first chain llm = OpenAI(temperature=0) chain1 = LLMChain(llm=llm, prompt=prompt1, output_key="summary") # Create a prompt template for second chain template2 = "Translate this summary to French: {summary}" prompt2 = PromptTemplate(input_variables=["summary"], template=template2) # Create second chain chain2 = LLMChain(llm=llm, prompt=prompt2, output_key="french_translation") # Create sequential chain seq_chain = SequentialChain( chains=[chain1, chain2], input_variables=["text"], output_variables=["summary", "french_translation"] ) # Run the chain result = seq_chain.run({"text": "Langchain helps you build chains of language models."}) print(result)
Important Notes
Make sure each chain's output keys match the next chain's input variables.
Sequential chains keep your workflow clear and easy to debug.
You can nest sequential chains inside other chains for more complex flows.
Summary
Sequential chains run multiple chains one after another.
They pass outputs from one chain as inputs to the next.
This helps build clear, step-by-step workflows.
Practice
1. What is the main purpose of a
SequentialChain in Langchain?easy
Solution
Step 1: Understand SequentialChain behavior
A SequentialChain runs chains in order, passing output from one to the next.Step 2: Compare options to this behavior
Only To run multiple chains one after another, passing outputs as inputs describes this step-by-step passing of outputs between chains.Final Answer:
To run multiple chains one after another, passing outputs as inputs -> Option CQuick Check:
SequentialChain = run chains sequentially with output passing [OK]
Hint: Sequential means one after another with output passing [OK]
Common Mistakes:
- Thinking chains run in parallel
- Believing outputs are not passed
- Confusing SequentialChain with single chain
2. Which of the following is the correct way to create a
SequentialChain with two chains named chain1 and chain2?easy
Solution
Step 1: Recall SequentialChain constructor
It requires a list of chains and lists of input and output variable names.Step 2: Check each option's syntax
Only SequentialChain(chains=[chain1, chain2], input_variables=["input"], output_variables=["output"]) correctly uses named parameters with lists for chains and variables.Final Answer:
SequentialChain(chains=[chain1, chain2], input_variables=["input"], output_variables=["output"]) -> Option BQuick Check:
Correct constructor syntax = SequentialChain(chains=[chain1, chain2], input_variables=["input"], output_variables=["output"]) [OK]
Hint: Look for named parameters and list brackets [OK]
Common Mistakes:
- Passing chains without list brackets
- Missing input/output variable lists
- Using plus operator to combine chains
3. Given the following code snippet, what will be the final output printed?
from langchain.chains import SequentialChain
chain1 = SomeChain() # outputs {'intermediate': 'hello'}
chain2 = SomeChain() # expects input 'intermediate' and outputs {'final': 'hello world'}
seq_chain = SequentialChain(chains=[chain1, chain2], input_variables=['input'], output_variables=['final'])
result = seq_chain.run({'input': 'start'})
print(result)medium
Solution
Step 1: Understand chain outputs and inputs
chain1 outputs {'intermediate': 'hello'}, chain2 uses 'intermediate' input and outputs {'final': 'hello world'}.Step 2: SequentialChain runs chain1 then chain2, passing outputs
Final result is {'final': 'hello world'}, printed as "{'final': 'hello world'}".Final Answer:
{'final': 'hello world'} -> Option DQuick Check:
Output of SequentialChain = {'final': 'hello world'} [OK]
Hint: Final output is dict of output_variables [OK]
Common Mistakes:
- Expecting string instead of dict repr
- Confusing input and output keys
- Assuming error due to input passing
4. What is the error in this code snippet that tries to create a SequentialChain?
seq_chain = SequentialChain(chains=[chain1, chain2], input_variables=['input'])
result = seq_chain.run({'input': 'data'})medium
Solution
Step 1: Check required parameters for SequentialChain
Both input_variables and output_variables are required parameters.Step 2: Identify missing parameter
The code misses output_variables, so it will raise an error.Final Answer:
Missing output_variables parameter causes an error -> Option AQuick Check:
output_variables missing = error [OK]
Hint: Always provide input and output variable lists [OK]
Common Mistakes:
- Forgetting output_variables
- Using wrong data type for chains
- Passing arguments incorrectly to run()
5. You want to build a SequentialChain that first extracts keywords from text, then summarizes those keywords. Which approach correctly sets up this workflow?
hard
Solution
Step 1: Identify the workflow steps
First extract keywords, then summarize them, so output of first is input of second.Step 2: Use SequentialChain with proper variable passing
Set keyword_extractor to output 'keywords', summary_chain to input 'keywords', then chain them sequentially.Final Answer:
Create two chains: keyword_extractor outputs 'keywords'; summary_chain takes 'keywords' as input; combine with SequentialChain passing these variables -> Option AQuick Check:
SequentialChain passes outputs as inputs [OK]
Hint: Chain outputs must match next chain inputs [OK]
Common Mistakes:
- Trying to do both steps in one chain
- Not passing outputs to next chain
- Ignoring variable names in chaining
