Learn/LangChain Mastery/Part 3 · 2. LCEL & Prompt Architecture
Lesson 13 of 68
SyllabusLangChain Mastery · Part 3

RunnableParallel & Branching

Running multiple steps at once. The key to fast, complex reasoning.

Course outline & contents
LangChain Mastery
Your First Chatbot: Talking to an LLMLCEL: Why LangChain Is a Composition EngineThe Runnable Protocol (invoke, batch, stream)Pipe Operator & Data FlowRunnableLambda & Custom LogicRunnableParallel & BranchingPromptTemplate vs ChatPromptTemplateSystem, Human, AI MessagesPrompt Architecture & ReusabilityStructured Output with JSON ModePydanticOutputParserOutputFixingParserStreaming Tokens & Async Chains

Standard chains are linear. A -> B -> C. But humans don't think like that. When you hear a statement, you might simultaneously:

  1. Check if it's true.
  2. Think of a counter-argument.
  3. Evaluate the tone.

To build smart AI, we need Branching. We need to run multiple chains at once and merge the results.

RunnableParallel allows you to split the input into multiple paths. Each path runs in parallel.

parallel_intro.py
from langchain_core.runnables import RunnableParallel

# Define two branches
joke_branch = prompt_joke | model | parser
poem_branch = prompt_poem | model | parser

# Combine them
map_chain = RunnableParallel(
  joke=joke_branch,
  poem=poem_branch
)

# Run
result = map_chain.invoke({"topic": "bears"})

# Output (Both generated simultaneously):
# {
#   "joke": "Why did the bear...",
#   "poem": "In forests deep..."
# }

The most common use of RunnableParallel is in RAG (Retrieval Augmented Generation). You need to do two things with the user's question:

  1. Pass it to the Prompt template.
  2. Use it to search the Retriever for documents.
rag_setup.py
setup_and_retrieval = RunnableParallel(
  {"context": retriever, "question": RunnablePassthrough()}
)
# "context" key gets documents from retriever
# "question" key gets the raw input passes through

Sometimes you want an "IF" statement. "If the user asks math, use the math chain. If they ask history, use the history chain."

branching.py
from langchain_core.runnables import RunnableBranch

branch = RunnableBranch(
  (lambda x: "math" in x["topic"].lower(), math_chain),
  (lambda x: "history" in x["topic"].lower(), history_chain),
  default_chain # Fallback
)

Linearlity is simplistic. Parallelism is powerful. Use RunnableParallel to fetch data from multiple sources or generate multiple perspectives at once.

Key Intuition: "More intelligence ≠ more tokens. It’s more structure."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonRunnableLambda & Custom LogicNext lesson →PromptTemplate vs ChatPromptTemplate