Running multiple steps at once. The key to fast, complex reasoning.
Standard chains are linear. A -> B -> C.
But humans don't think like that. When you hear a statement, you might simultaneously:
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.
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:
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."
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."