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.
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_modelfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserfrom langchain_core.runnables import RunnableParallelmodel = 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
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.
Runnable parallel runs several runnables on the same input at once, and returns a dictionary. Next, passing data through unchanged, alongside new results.