0
0
LangChainframework~10 mins

Sequential chains in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sequential chains
Start: Input data
Chain 1: Process input
Output 1: Result from Chain 1
Chain 2: Use Output 1 as input
Output 2: Final result
End
Sequential chains take input, process it step-by-step through multiple chains, passing output from one to the next.
Execution Sample
LangChain
from langchain.chains import SequentialChain

chain1 = ... # first chain
chain2 = ... # second chain
seq_chain = SequentialChain(chains=[chain1, chain2], input_variables=["input"], output_variables=["output2"])
result = seq_chain.run("Hello")
This code runs two chains in order, passing the output of the first as input to the second, starting with 'Hello'.
Execution Table
StepActionInputChain OutputNext Input
1Start with input{"input": "Hello"}N/A{"input": "Hello"}
2Run Chain 1{"input": "Hello"}{"output1": "Hello processed"}{"output1": "Hello processed"}
3Run Chain 2{"output1": "Hello processed"}{"output2": "Final result"}N/A
4Return final outputN/A{"output2": "Final result"}N/A
💡 All chains executed sequentially; final output returned.
Variable Tracker
VariableStartAfter Chain 1After Chain 2Final
input"Hello""Hello""Hello""Hello"
output1N/A"Hello processed""Hello processed""Hello processed"
output2N/AN/A"Final result""Final result"
Key Moments - 2 Insights
Why does the output of Chain 1 become the input of Chain 2?
Because SequentialChain passes the output variables of one chain as input variables to the next, as shown in execution_table step 3.
What happens if the input variable names don't match between chains?
The chain will fail to pass data correctly, causing errors or missing inputs, since SequentialChain relies on matching variable names to connect chains.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the input to Chain 2 at step 3?
A{"input": "Hello"}
B{"input": "Hello processed"}
C{"output1": "Hello processed"}
D{"output2": "Final result"}
💡 Hint
Check the 'Next Input' column at step 2 and the 'Input' column at step 3.
At which step does the final output become available?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Look at the 'Chain Output' and 'Action' columns for when final output is returned.
If Chain 1 output variable was named differently and not passed correctly, what would happen?
AChain 2 would receive the correct input anyway.
BChain 2 would get no input and likely fail.
CThe final output would be from Chain 1 only.
DThe SequentialChain would skip Chain 2.
💡 Hint
Refer to key_moments about variable name matching and execution_table flow.
Concept Snapshot
Sequential chains run multiple chains one after another.
Output of one chain becomes input to the next.
Input variables must match for data flow.
Use SequentialChain with chains list and input/output variables.
Final output is from the last chain in sequence.
Full Transcript
Sequential chains in Langchain let you connect multiple chains so that the output of one becomes the input of the next. You start with an initial input, run the first chain, get its output, then feed that output as input to the second chain, and so on. This continues until all chains have run, and the final output is returned. It is important that the variable names used for outputs and inputs match between chains to ensure smooth data flow. The example code shows creating two chains and combining them with SequentialChain. The execution table traces each step: starting input, Chain 1 processing, Chain 2 processing, and final output. The variable tracker shows how variables change after each chain runs. Common confusions include why outputs become inputs and what happens if variable names don't match. The visual quiz tests understanding of input/output flow and error cases. Overall, sequential chains help build complex workflows by linking simple chains in order.