Saturday, April 4, 2026

RunablePassthrough in LangChain With Examples

RunablePassthrough in LangChain is a simple runnable that returns its input unchanged. It is used in the scenario where you want to preserve the original input alongside other computed values.

For example, if you have a RAG pipeline like this-

chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | model
    | StrOutputParser()
)

Here you have to send question to the vector store as well as to the prompt later in the pipeline. Using "question": RunnablePassthrough() ensures the original question is retained for the prompt, whereas without it the "question" key would not be available during prompt construction.

LangChain RunnablePassthrough Example

Let’s say you are creating a RAG-based Customer Support Bot that retrieves documentation from the vector store, enriches the prompt with that context, and passes the original question along then you can use RunnablePassthrough, to preserve the question, to be used later in the pipeline.

Here are few snippets of the code (full vector store is not implemented here, just the relevant part to keep focus on RunnablePassthrough). Pinecone vector store is used for indexing and storing vector embeddings.

from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema import StrOutputParser
from langchain_pinecone import PineconeVectorStore
from pinecone import Pinecone
from langchain.schema.runnable import RunnablePassthrough, RunnableLambda

embeddings = OpenAIEmbeddings()
doc_search = PineconeVectorStore.from_existing_index(index, embeddings)
retriever = doc_search.as_retriever(search_kwargs={"k": 2})

# Define the Prompt Template
template = "Answer the question based only on the following context:
{context}

Question: {question}
"
prompt = ChatPromptTemplate.from_template(template)
model = ChatOpenAI()

# Build the Chain
# Use RunnableLambda to ensure the retriever gets the 'question' key
chain = (
    {
        "context": RunnableLambda(lambda x: retriever.invoke(x["question"])),
        "question": RunnablePassthrough()
    }
    | prompt
    | model
    | StrOutputParser()
)

# Run the chain
response = chain.invoke({"question": "When is the report due for environment policy?"})
print(response)

RunnablePassthrough.assign in LangChain

RunablePassthrough.assign lets you add extra static or computed fields to the passthrough output.

For example, suppose you want to add some metadata like timestamp to the prompt which is then passed to the LLM as extra context for guiding the model’s response.

# Define the Prompt Template 
template = """ Answer the question based only on the following context:
{context}

Question: {question}

Metadata: {timestamp}

Use the metadata to guide your response. For example, consider the timestamp when deciding if the information is current or relevant.
"""

In chain you can pass this timestamp information using RunnablePassthrough().assign

# 3. Build the Chain
chain = (
    {
        "context": RunnableLambda(lambda x: retriever.invoke(x["question"])),
        # Preserve the question AND add metadata with .assign
        "question": RunnablePassthrough().assign(
            timestamp=lambda x: datetime.now().isoformat()
        )
    }
    | prompt
    | model
    | StrOutputParser()
)

That's all for this topic RunablePassthrough in LangChain With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. RunnableParallel in LangChain With Examples
  2. RunnableLambda in LangChain With Examples
  3. RunnableBranch in LangChain With Examples
  4. Chain Using LangChain Expression Language With Examples
  5. LangChain PromptTemplate + Streamlit - Code Generator Example

You may also like-

  1. String in Java Tutorial
  2. Array in Java
  3. Count Number of Words in a String Java Program
  4. Ternary Operator in Java With Examples
  5. Java Multithreading Interview Questions And Answers
  6. Java Exception Handling Tutorial
  7. ConcurrentHashMap in Java With Examples
  8. TreeMap in Java With Examples

No comments:

Post a Comment