AI Hints
Home›LangChain Tutorial›Runnables
LangChain · Lesson 21 of 120

RunnableParallel

Side by side.

⏱ 2 min read▶ Video at 21:49🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

For one book, Riverside Books wants a short summary, and a list of similar titles. These two requests don't depend on each other, so they can run at the same time.

RunnableParallel
From the video: RunnableParallel
  • A summary
  • Similar titles
  • At the same time

Two chains at once

Runnable parallel takes named runnables, and runs them all on the same input, at the same time. It returns a dictionary, with one key per branch. Run it. Both answers come back together, in one dictionary, and the total time is about the same as a single call.

parallel.pypython
from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableParallel

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
ask = lambda t: ChatPromptTemplate.from_template(t) | model | StrOutputParser()

both = RunnableParallel(
    summary=ask("Summarise {book} in 10 words."),
    similar=ask("Name 2 books similar to {book}, comma separated, titles only."),
)
for key, value in both.invoke({"book": "The Hobbit"}).items():
    print(f"{key}: {value}")
Output
summary: Homeward-bound hobbit finds magic ring, slays dragon, reclaims gold.
similar: The Lion, the Witch and the Wardrobe, The Fellowship of the Ring

A handy shortcut

Here's a shortcut. Inside a chain, a plain dictionary of runnables is automatically treated as a runnable parallel. So you can simply write a dictionary with summary and similar keys, and pipe it into the next step. You'll see this pattern everywhere, especially in retrieval chains.

keyvalue
RunnableParallelnamed branches
Same inputgoes to each
Outputa dict of results
Shortcuta plain dict in a chain

Summary

Runnable parallel runs several runnables on the same input at once, and returns a dictionary. Next, passing data through unchanged, alongside new results.

Key takeaways
  • Independent branches
  • Run at once
  • Dict of results
Next up: RunnablePassthrough — carrying data along