0
0
LangchainHow-ToBeginner ยท 3 min read

How to Use RunnablePassthrough in Langchain: Simple Guide

In Langchain, RunnablePassthrough is used to create a runnable that simply returns the input it receives without any changes. You use it by importing RunnablePassthrough and calling its invoke method with your input to get the same output.
๐Ÿ“

Syntax

The RunnablePassthrough class is a simple runnable that returns its input as output without any processing. You create an instance and call invoke(input) to get the same input back.

  • RunnablePassthrough(): Constructor to create the passthrough runnable.
  • invoke(input): Method to run the passthrough, returning the input as output.
python
from langchain.schema.runnable import RunnablePassthrough

passthrough = RunnablePassthrough()
output = passthrough.invoke("Hello, LangChain!")
๐Ÿ’ป

Example

This example shows how to create a RunnablePassthrough instance and use it to pass a string input through unchanged. It demonstrates that the output is exactly the same as the input.

python
from langchain.schema.runnable import RunnablePassthrough

# Create the passthrough runnable
passthrough = RunnablePassthrough()

# Input data
input_text = "This text passes through unchanged."

# Invoke passthrough
output_text = passthrough.invoke(input_text)

print("Output:", output_text)
Output
Output: This text passes through unchanged.
โš ๏ธ

Common Pitfalls

One common mistake is expecting RunnablePassthrough to modify or process the input. It does not change the input in any way.

Another pitfall is forgetting to call the invoke method and trying to use the runnable instance directly, which will not produce the expected output.

python
from langchain.schema.runnable import RunnablePassthrough

passthrough = RunnablePassthrough()

# Wrong: Using the instance directly
# output = passthrough  # This does not run the passthrough

# Right: Call invoke() method
output = passthrough.invoke("Test input")
print(output)
Output
Test input
๐Ÿ“Š

Quick Reference

MethodDescription
RunnablePassthrough()Creates a passthrough runnable instance
invoke(input)Returns the input unchanged
โœ…

Key Takeaways

RunnablePassthrough returns the input exactly as output without changes.
Always call the invoke() method to run the passthrough.
It is useful when you want to pass data through a chain without modification.
Do not expect any processing or transformation from RunnablePassthrough.
Use RunnablePassthrough for simple passthrough behavior in Langchain pipelines.