How to Use SequentialChain in Langchain: Simple Guide
Use
SequentialChain in Langchain to run multiple chains one after another, where each chain's output feeds into the next chain's input automatically. You create individual chains, then combine them in a SequentialChain object, which manages the flow of data between them.Syntax
The SequentialChain class takes a list of chains and runs them in order. Each chain processes inputs and produces outputs that become inputs for the next chain.
Key parts:
chains: List of individual chains to run sequentially.input_variables: Variables required to start the first chain.output_variables: Variables to return after all chains finish.
python
from langchain.chains import SequentialChain # Create SequentialChain with chains list sequential_chain = SequentialChain( chains=[chain1, chain2], input_variables=["input1"], output_variables=["output2"] )
Example
This example shows two simple chains: one that doubles a number, and another that adds 10. The SequentialChain runs them in order, passing the output of the first as input to the second.
python
from langchain.llms import OpenAI from langchain.chains import LLMChain, SequentialChain from langchain.prompts import PromptTemplate # Simple prompt templates double_prompt = PromptTemplate( input_variables=["number"], template="Double the number {number}." ) add_prompt = PromptTemplate( input_variables=["doubled"], template="Add 10 to {doubled}." ) # Create chains llm = OpenAI(temperature=0) double_chain = LLMChain(llm=llm, prompt=double_prompt, output_key="doubled") add_chain = LLMChain(llm=llm, prompt=add_prompt, output_key="result") # Create SequentialChain seq_chain = SequentialChain( chains=[double_chain, add_chain], input_variables=["number"], output_variables=["result"], verbose=True ) # Run chain output = seq_chain.run({"number": "5"}) print(output)
Output
20
Common Pitfalls
- Not matching
output_keyof one chain with theinput_variablesof the next chain causes errors. - Forgetting to include all required
input_variableswhen creatingSequentialChain. - Using incompatible chains that do not produce or consume expected keys.
Always ensure the output keys from one chain match the input keys expected by the next.
python
from langchain.chains import SequentialChain # Wrong: output key 'doubled' not passed to next chain input 'value' seq_chain_wrong = SequentialChain( chains=[double_chain, add_chain], input_variables=["number"], output_variables=["result"] ) # Right: rename add_chain input to 'doubled' to match output key add_chain_corrected = LLMChain(llm=llm, prompt=add_prompt, output_key="result") seq_chain_right = SequentialChain( chains=[double_chain, add_chain_corrected], input_variables=["number"], output_variables=["result"] )
Quick Reference
| Parameter | Description |
|---|---|
| chains | List of chains to run in order |
| input_variables | List of input variable names to start the chain |
| output_variables | List of output variable names to return after all chains |
| verbose | If True, prints chain steps for debugging |
Key Takeaways
SequentialChain runs multiple chains in order, passing outputs as inputs automatically.
Ensure output keys of one chain match input variables of the next to avoid errors.
Define input_variables and output_variables clearly when creating SequentialChain.
Use verbose=True to see detailed chain execution for debugging.
SequentialChain helps build complex workflows by chaining simple chains together.