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.
0
0
Sequential chains in LangChain
Introduction
When you want to process data step-by-step, like cleaning text then summarizing it.
When you need to combine different language models or tools in order.
When you want to build a workflow that depends on previous answers.
When you want to keep your code organized by separating tasks.
When you want to reuse small chains to build bigger ones.
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
This example runs chain1 then chain2 in order, passing the output of chain1 to chain2.
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"})
Here, three chains run one after another to answer a question step-by-step.
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)
OutputSuccess
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.