Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/LangChain Mastery/Part 3 · 2. LCEL & Prompt Architecture
Lesson 13 of 68

RunnableParallel & Branching

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

Derpx0610 Jan · 1 min read
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
Contents
1. The Single-Thread Limit2. RunnableParallel (The "Map" Step)3. Real World Use: RAG Setup4. Conditional Branching (RunnableBranch)5. Summary
0% read · 1 min leftBack to top ↑
Course outline & contents0%
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
Contents
1. The Single-Thread Limit2. RunnableParallel (The "Map" Step)3. Real World Use: RAG Setup4. Conditional Branching (RunnableBranch)5. Summary

1. The Single-Thread Limit

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.

2. RunnableParallel (The "Map" Step)

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..."
# }

3. Real World Use: RAG Setup

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

4. Conditional Branching (RunnableBranch)

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
)

5. Summary

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