Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 32 ยท Chains and LCEL

RunnableParallel and RunnablePassthrough

Run steps side by side with RunnableParallel, carry input forward with RunnablePassthrough and assign, reshape dictionaries and shape chain inputs with runnable stand-ins.

A straight pipe carries one value from step to step. Real chains need more: run two things at once and combine the results, keep the original question while also computing something from it, or reshape a dictionary so that it matches a prompt's variables. LCEL handles these with three small tools: RunnableParallel, RunnablePassthrough and dictionary shaping.

RunnableParallel: fan out, then gather

RunnableParallel takes a dictionary of Runnables, gives the same input to each, runs them concurrently, and returns a dictionary of their outputs:

Python
from langchain_core.runnables import RunnableParallel

both = RunnableParallel(
    joke=joke_prompt | model | StrOutputParser(),
    poem=poem_prompt | model | StrOutputParser(),
)
both.invoke({"topic": "bears"})
# {'joke': '...', 'poem': '...'}

Two model calls happen at the same time, so the total time is about the slower of the two, not the sum. A plain dict placed in a pipe is coerced into a RunnableParallel, which gives the compact form you see in most examples:

Python
chain = {"joke": joke_chain, "poem": poem_chain} | combine_prompt | model

The output dictionary becomes the next step's input, so the keys must match the next prompt's variables.

flowchart LR
    I["input"] --> A["branch A"]
    I --> B["branch B"]
    A --> D["{a: ..., b: ...}"]
    B --> D
    D --> N["next step"]

Every branch receives the same input; results are collected under their keys.

Fan-out in plain Python (runnable)

This stand-in uses threads to run named branches at once and returns a dict, just like the real class. The sleeps imitate model latency, and the timing line shows the speed-up.

Python Stand-in: RunnableParallel with threads

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

RunnablePassthrough: keep the input

Sometimes the original input must reach a later step unchanged, while another branch computes something from it. The retrieval pattern is the classic case: the question goes to a retriever and straight into the prompt.

Python
from langchain_core.runnables import RunnablePassthrough

chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | model
    | StrOutputParser()
)
chain.invoke("What is our refund policy?")

The input string is sent to two branches. retriever turns it into documents, and RunnablePassthrough() returns it as is. The prompt then receives {"context": ..., "question": ...}. You will use this shape again in Building a RAG Chain.

RunnablePassthrough.assign: add keys to a dictionary

When the input is already a dictionary, .assign() keeps all its keys and adds computed ones. This is the cleanest way to enrich the data as it flows down a chain:

Python
chain = (
    RunnablePassthrough.assign(word_count=lambda d: len(d["text"].split()))
    | RunnablePassthrough.assign(
        label=lambda d: "long" if d["word_count"] > 5 else "short")
)
chain.invoke({"text": "Short note"})
# {'text': 'Short note', 'word_count': 2, 'label': 'short'}

Each assign sees everything added by the previous ones.

Python Stand-in: passthrough and assign

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reshaping with itemgetter and lambdas

Prompts need specific variable names. When a dictionary has the wrong keys, pick and rename with operator.itemgetter or lambdas:

Python
from operator import itemgetter

chain = (
    {
        "question": itemgetter("q"),
        "language": lambda d: d.get("lang", "English"),
    }
    | prompt
    | model
)
chain.invoke({"q": "What is DNS?"})

Each dictionary value is a Runnable that receives the whole input; itemgetter("q") extracts one key. This idiom connects steps whose shapes do not match without writing a named function for every adapter.

Python Stand-in: adapting dict shapes with itemgetter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Practical guidance

  • Use parallel for independent work. If branch B needs branch A's result, use two steps in sequence, not parallel.
  • Mind rate limits. Ten parallel branches means ten simultaneous API calls.
  • All branches get the same input. To give different inputs, adapt first with lambdas or itemgetter.
  • If one branch fails, the whole parallel step fails unless you add a fallback (see Retries, Fallbacks and Config).
  • Parallel does not mean separate processes: it uses threads (or asyncio for ainvoke), which is perfect for network-bound LLM calls.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does RunnableParallel return?

    Every branch receives the same input and the outputs are collected under their keys.
  2. 2. In {'context': retriever, 'question': RunnablePassthrough()} | prompt, what does RunnablePassthrough do?

    It returns its input as is, so the question reaches the prompt alongside the retrieved context.
  3. 3. What does RunnablePassthrough.assign(total=...) do to a dict input?

    assign merges new computed keys into the existing dictionary.
  4. 4. Two LLM calls are independent. How do you run them at the same time?

    Parallel runs branches concurrently, so total time is roughly that of the slowest branch.

Summary

  • RunnableParallel (or a dict in a pipe) runs branches concurrently on the same input and returns a dict.
  • RunnablePassthrough forwards the input unchanged; .assign() adds computed keys to a dict.
  • itemgetter and lambdas reshape dictionaries so keys match prompt variables.
  • Use parallelism for independent work and watch provider rate limits.

Next: Branching, Routing and RunnableLambda