LCEL & ChainsLesson 2

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

explain_chain produces {explanation}, which tip_prompt uses alongside the original {tag}.
Same | 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

Same pipe syntax from LCEL — just more steps wired together.

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}

invoke input{"tag": "a"}
RunnablePassthrough.assign(…){"tag": "a", "explanation": "The <a> tag…"}
tip_prompt | model | parser"Use descriptive link text…"
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 — input tag plus explanation, output the tip string.

The demo script

sequential_chains_demo.py runs the chain for a and img.

sequential_chains_demo.py
"""sequential_chains_demo.py — two LLM steps"""
… imports, model, parser …
explain_chain = explain_prompt | model | parser
chain = RunnablePassthrough.assign(
explanation=explain_chain
) | tip_prompt | model | parser
tip = chain.invoke({"tag": tag})

Download the code

sequential_chains_demo.py

explain_chain → tip_chain

Download .py
Save into your langchain-course folder. Needs venv, .env, and packages from Project Setup.

Run it

Activate the venv from Project Setup, then:

python sequential_chains_demo.py
PowerShell — (.venv) active
(.venv) PS C:\projects\langchain-course> python sequential_chains_demo.py
Tag: <a>
Tip: Use clear link text so readers know where the link goes.
Tag: <img>
Tip: Always add an alt attribute so screen readers can describe the image.
One invoke — 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.