LCEL & ChainsLesson 6

RunnableParallel

Sequential chains run steps in a line — step 2 waits for step 1. RunnableParallel runs two branches on the same input and returns a dict: result['purpose'], result['example']. Both branches take {tag} and answer different questions about the HTML tag.

Split and merge

Same {"tag": "a"} goes to purpose_chain and example_chain. When both finish, invoke returns {"purpose": "…", "example": "…"}.

One input splits into parallel branches, then merges:

input: {"tag": "a"}
↓ split

branch 1

purpose_chain

branch 2

example_chain
↓ merge
{"purpose": "…", "example": "…"}
Same input to both branches — one dict from invoke.

Compared to sequential chains

Simple Sequential Chains when step 2 needs step 1's string. RunnableParallel when both branches only need the original input.

Sequential (one after another)

chain = pick_chain
    | explain_prompt
    | model | parser
# step 2 waits for step 1

Use when step 2 needs step 1 output

RunnableParallel

chain = RunnableParallel(
    purpose=purpose_chain,
    example=example_chain,
)
# both run on same input

Use when branches are independent

Use parallel when neither branch needs the other's output.

The dict result

Each keyword in RunnableParallel(purpose=…, example=…) becomes a key in the output. Read values with result["purpose"].

One invoke, one dict back

result = chain.invoke({"tag": "a"})

result["purpose"]
# "The <a> tag creates a hyperlink…"

result["example"]
# '<a href="/home">Home</a>'
Key names come from the arguments you pass to RunnableParallel.

A plain dict works the same way — {"purpose": purpose_chain, "example": example_chain}.

What each branch does

  • purpose_chain — input {tag}, output one-sentence purpose.
  • example_chain — same input, output one line of HTML. System message blocks DOCTYPE, fences, and full pages.

The demo script

runnable_parallel_demo.py runs the chain for a and img.

runnable_parallel_demo.py
"""runnable_parallel_demo.py"""
purpose_chain = purpose_prompt | model | parser
example_chain = example_prompt | model | parser
chain = RunnableParallel(purpose=…, example=…)
result = chain.invoke({"tag": tag})

Download the code

runnable_parallel_demo.py

purpose + example in parallel

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 runnable_parallel_demo.py
PowerShell — (.venv) active
(.venv) PS C:\projects\langchain-course> python runnable_parallel_demo.py
Tag: <a>
purpose: The <a> tag creates a hyperlink to another page.
example: <a href="/home">Home</a>
Tag: <img>
purpose: The <img> tag embeds an image in the page.
example: <img src="logo.png" alt="Logo">
example should be one line like <a href="/home">Home</a> — not a full HTML page. The system prompt enforces that.

When to pick parallel

  • Both branches need the same input but not each other's output — purpose + example here.
  • You want several answers in one dict from a single invoke.
  • Official reference: Parallel runnables.

What's Next

Next: RunnablePassthrough — keep input keys while adding new ones.