liveBook · Manning

RunnableParallel

As explained in MongoDB 8.0 in Action, Third Edition

RunnableParallel is a LangChain runnable that evaluates multiple input mappings in parallel and returns their combined results.

RunnableParallel is a LangChain runnable that evaluates multiple input mappings in parallel and returns their combined results. In a retrieval-augmented chatbot, it prepares the question and context for later processing. section 14.5.4 section 14.5.2

A RAG chain uses RunnableParallel to coordinate related inputs:

  • Maps the user question and context as parallel inputs. section 14.5.4
  • Works alongside a retriever that finds relevant documents from an Atlas Vector Search index. section 14.5.4
  • Combines retrieved context with the question before ChatPromptTemplate formats the prompt for ChatOpenAI. section 14.5.4
  • Uses RunnablePassthrough to carry the question or retrieved context forward without modification. section 14.5.4 section 14.5.2

Inspecting a MongoDB RAG chain

Ask “What indexes does MongoDB support?” in the LangServe playground. The intermediate steps show RunnableParallel preparing the question and context, retrieval locating relevant documents, prompt construction combining the results, and the model producing the answer. section 14.5.4

import os
from langchain_openai import ChatOpenAI
from langchain_community.document_loaders import PyPDFLoader
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel
from langchain_core.runnables import (
    RunnableLambda,
    RunnableParallel,
    RunnablePassthrough,
)
from langchain_text_splitters import RecursiveCharacterTextSplitter
from pymongo import MongoClient

# Set DB
if os.environ.get("MONGO_URI", None) is None:
    raise Exception("Missing `MONGO_URI` environment variable.")
MONGO_URI = os.environ["MONGO_URI"]

DB_NAME = "langchain"
COLLECTION_NAME = "mongodb"
ATLAS_VECTOR_SEARCH_INDEX_NAME = "default"

client = MongoClient(MONGO_URI)
db = client[DB_NAME]
MONGODB_COLLECTION = db[COLLECTION_NAME]

# Read from MongoDB Atlas Vector Search
vectorstore = MongoDBAtlasVectorSearch.from_connection_string(
    MONGO_URI,
    DB_NAME + "." + COLLECTION_NAME,
    OpenAIEmbeddings(disallowed_special=()),
    index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,
)
retriever = vectorstore.as_retriever(search_kwargs={"k": 1})

# RAG prompt
template = """Answer the question based only on the following context:
{context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)

# RAG
model = ChatOpenAI()
chain = (
   RunnableParallel({"context": retriever,
➥"question": RunnablePassthrough()})
    | prompt
    | model
    | StrOutputParser()
)


# Add typing for input
class Question(BaseModel):
    __root__: str


chain = chain.with_types(input_type=Question)


def _ingest(url: str) -> dict:
    loader = PyPDFLoader(url)
    data = loader.load()

    # Split docs
    text_splitter = RecursiveCharacterTextSplitter
➥(chunk_size=500, chunk_overlap=0)
    docs = text_splitter.split_documents(data)

    # Insert the documents in MongoDB Atlas Vector Search
    _ = MongoDBAtlasVectorSearch.from_documents(
        documents=docs,
        embedding=OpenAIEmbeddings(disallowed_special=()),
        collection=MONGODB_COLLECTION,
        index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,
    )
    return {}

ingest = RunnableLambda(_ingest)

Inspect the chain definition alongside RunnableLambda, RunnablePassthrough, the retriever, prompt template, model, and output parser. section 14.5.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains input mapping and pipeline placement through intermediate steps, without detailing concurrency implementation internals.
Examples
Demonstrates MongoDB index retrieval through a LangServe playground query and visible intermediate chain outputs.
What you can do after
Operate a LangServe RAG chain and inspect how parallel inputs feed retrieval, prompt construction, and model generation.
How it connects
Connects RunnablePassthrough, retrievers, embeddings, Atlas Vector Search, ChatPromptTemplate, ChatOpenAI, and output parsing.

Also explained in

Compare how 2 books explain RunnableParallel →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.