RunnablePassthrough and RunnableLambda help you run simple tasks easily without extra setup.
RunnablePassthrough and RunnableLambda in LangChain
from langchain.schema.runnable import RunnablePassthrough, RunnableLambda # Create a passthrough runnable passthrough = RunnablePassthrough() # Create a lambda runnable with a function lambda_runnable = RunnableLambda(lambda x: x * 2) # Run them result1 = passthrough.invoke('hello') result2 = lambda_runnable.invoke(5)
RunnablePassthrough returns the input as output without changes.
RunnableLambda runs a function you give it on the input.
passthrough = RunnablePassthrough()
output = passthrough.invoke('test')lambda_runnable = RunnableLambda(lambda x: x + 10) output = lambda_runnable.invoke(7)
lambda_runnable = RunnableLambda(lambda s: s.upper()) output = lambda_runnable.invoke('hello')
This program shows how RunnablePassthrough returns the input unchanged, and RunnableLambda runs a function that doubles the input number.
from langchain.schema.runnable import RunnablePassthrough, RunnableLambda # Create a passthrough runnable passthrough = RunnablePassthrough() # Create a lambda runnable that doubles numbers lambda_runnable = RunnableLambda(lambda x: x * 2) # Use passthrough result1 = passthrough.invoke('keep this text') print(f'Passthrough output: {result1}') # Use lambda runnable result2 = lambda_runnable.invoke(10) print(f'Lambda output: {result2}')
RunnablePassthrough is useful when you want to keep data unchanged in a chain.
RunnableLambda lets you quickly wrap any small function to run inside LangChain.
Both help keep your code simple and modular.
RunnablePassthrough returns input as output without changes.
RunnableLambda runs a small function you provide on the input.
Use them to build simple, reusable pieces in your LangChain workflows.
Practice
RunnablePassthrough do with the input it receives?Solution
Step 1: Understand RunnablePassthrough behavior
RunnablePassthrough is designed to return whatever input it receives without modifying it.Step 2: Compare options with behavior
Only Returns the input exactly as it is without any changes matches this behavior exactly; others describe transformations or errors which RunnablePassthrough does not do.Final Answer:
Returns the input exactly as it is without any changes -> Option CQuick Check:
RunnablePassthrough returns input unchanged = A [OK]
- Thinking RunnablePassthrough modifies input
- Confusing RunnablePassthrough with RunnableLambda
- Assuming it throws errors on certain inputs
RunnableLambda that doubles a number input?Solution
Step 1: Identify the doubling function
Doubling means multiplying the input by 2, so the function should belambda x: x * 2.Step 2: Match with options
RunnableLambda(lambda x: x * 2) matches the doubling function exactly; others perform addition, division, or subtraction.Final Answer:
RunnableLambda(lambda x: x * 2) -> Option BQuick Check:
Doubling function uses multiplication by 2 = D [OK]
- Using addition instead of multiplication
- Confusing division or subtraction for doubling
- Incorrect lambda syntax
passthrough = RunnablePassthrough()
lambda_runner = RunnableLambda(lambda x: x.upper())
result = lambda_runner.invoke(passthrough.invoke('hello'))
print(result)Solution
Step 1: Trace RunnablePassthrough output
Callingpassthrough.invoke('hello')returns 'hello' unchanged.Step 2: Apply RunnableLambda function
The lambda converts input to uppercase, so'hello'.upper()returns 'HELLO'.Final Answer:
'HELLO' -> Option AQuick Check:
Passthrough returns input, lambda uppercases it = C [OK]
- Expecting passthrough to modify input
- Confusing case conversion result
- Assuming runtime error on invoke
lambda_runner = RunnableLambda(lambda x: x + 1)
result = lambda_runner.invoke('5')
print(result)Solution
Step 1: Analyze lambda operation on input
The lambda tries to add 1 to input '5', which is a string, causing a type mismatch.Step 2: Identify error type
Adding integer 1 to string '5' raises a TypeError in Python.Final Answer:
TypeError because string '5' cannot be added to integer 1 -> Option AQuick Check:
Adding int to string causes TypeError = B [OK]
- Assuming implicit string to int conversion
- Thinking output is string concatenation
- Confusing error types
RunnablePassthrough and RunnableLambda is correct?Solution
Step 1: Pass list unchanged with RunnablePassthrough
RunnablePassthrough returns the list as is, so it fits the first step.Step 2: Double each number with RunnableLambda
RunnableLambda withlambda x: [i*2 for i in x]correctly doubles each element in the list.Final Answer:
Use RunnablePassthrough to pass the list, then RunnableLambda with lambda x: [i*2 for i in x] -> Option DQuick Check:
Passthrough passes list, lambda doubles elements = A [OK]
- Trying to double list with passthrough
- Using lambda that multiplies list object, not elements
- Reversing order of passthrough and lambda
Ready to prove it?
Pass the test and this pattern moves unknown
