Sequential Chains
The LCEL lesson used one prompt. This lesson runs two in order: explain what an HTML tag does, then ask for a beginner tip using that explanation. You still call chain.invoke once — LangChain runs step 1, feeds the result into step 2, and returns the tip.
Two steps, one invoke
explain_chain runs first and produces explanation. The tip prompt reads that plus the original tag and returns the final answer.
Step 2 starts after step 1 finishes:
input
{ tag: "a" }
step 1
explain_chain
step 2
tip_chain
output
beginner tip
| syntax as LCEL — more steps in the pipe.Compared to LCEL
Step 2 needs both tag and explanation. RunnablePassthrough.assign runs explain_chain and adds its output as a new key without dropping tag.
Single chain (LCEL lesson)
chain = prompt | model | parser
answer = chain.invoke(
{"question": q}
)One prompt → one answer
Sequential chain
chain = (
RunnablePassthrough.assign(
explanation=explain_chain
)
| tip_prompt
| model
| parser
)
tip = chain.invoke({"tag": "a"})Two prompts → explain, then tip
How assign works
Input is {"tag": "a"}. After assign you have {"tag": "a", "explanation": "…"} — both keys ready for tip_prompt. Skip assign and step 2 loses tag (you get a KeyError).
How assign keeps {tag} and adds {explanation}
assign keeps the original input and adds new keys. Step 2 needs both tag and explanation.What each step does
explain_chain— input{"tag": "a"}, output a one-sentence description of the tag.tip_prompt | model | parser— inputtagplusexplanation, output the tip string.
The demo script
sequential_chains_demo.py runs the chain for a and img.
Download the code
sequential_chains_demo.py
explain_chain → tip_chain
langchain-course folder. Needs venv, .env, and packages from Project Setup.Run it
Activate the venv from Project Setup, then:
python sequential_chains_demo.pyinvoke — two model requests under the hood.Where this pattern shows up
- Explain or summarize first, then write something shorter from that text — same shape as tag → tip here.
- Any step where the second prompt needs both the original input and the first model's reply.
- Official reference: Sequence runnables.
What's Next
Next: Simple Sequential Chains — when step 2 only needs the string from step 1.