Bird
Raised Fist0
LangChainframework~10 mins

Sequential chains in LangChain - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of a SequentialChain in Langchain?
easy
A. To create a single chain that never passes outputs
B. To run multiple chains all at the same time independently
C. To run multiple chains one after another, passing outputs as inputs
D. To stop chains from running automatically

Solution

  1. Step 1: Understand SequentialChain behavior

    A SequentialChain runs chains in order, passing output from one to the next.
  2. 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.
  3. Final Answer:

    To run multiple chains one after another, passing outputs as inputs -> Option C
  4. Quick 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
A. SequentialChain([chain1, chain2])
B. SequentialChain(chains=[chain1, chain2], input_variables=["input"], output_variables=["output"])
C. SequentialChain(chain1, chain2)
D. SequentialChain(chains=chain1 + chain2)

Solution

  1. Step 1: Recall SequentialChain constructor

    It requires a list of chains and lists of input and output variable names.
  2. 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.
  3. Final Answer:

    SequentialChain(chains=[chain1, chain2], input_variables=["input"], output_variables=["output"]) -> Option B
  4. Quick 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
A. Error: missing input for chain2
B. 'hello world'
C. 'start'
D. {'final': 'hello world'}

Solution

  1. Step 1: Understand chain outputs and inputs

    chain1 outputs {'intermediate': 'hello'}, chain2 uses 'intermediate' input and outputs {'final': 'hello world'}.
  2. Step 2: SequentialChain runs chain1 then chain2, passing outputs

    Final result is {'final': 'hello world'}, printed as "{'final': 'hello world'}".
  3. Final Answer:

    {'final': 'hello world'} -> Option D
  4. Quick 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
A. Missing output_variables parameter causes an error
B. Chains list should be a tuple, not a list
C. Input dictionary keys do not match input_variables
D. run() method requires no arguments

Solution

  1. Step 1: Check required parameters for SequentialChain

    Both input_variables and output_variables are required parameters.
  2. Step 2: Identify missing parameter

    The code misses output_variables, so it will raise an error.
  3. Final Answer:

    Missing output_variables parameter causes an error -> Option A
  4. Quick 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
A. Create two chains: keyword_extractor outputs 'keywords'; summary_chain takes 'keywords' as input; combine with SequentialChain passing these variables
B. Create one chain that does both extraction and summary in one step
C. Run keyword_extractor and summary_chain separately without chaining outputs
D. Use SequentialChain but ignore passing variables between chains

Solution

  1. Step 1: Identify the workflow steps

    First extract keywords, then summarize them, so output of first is input of second.
  2. Step 2: Use SequentialChain with proper variable passing

    Set keyword_extractor to output 'keywords', summary_chain to input 'keywords', then chain them sequentially.
  3. Final Answer:

    Create two chains: keyword_extractor outputs 'keywords'; summary_chain takes 'keywords' as input; combine with SequentialChain passing these variables -> Option A
  4. Quick 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