Discover how tiny helpers can save you from writing endless boring code!
Why RunnablePassthrough and RunnableLambda in LangChain? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to quickly test or pass data through a process without writing extra code or creating full functions every time.
You try to manually write separate functions or classes for each small step, even if they just return the input or run a simple operation.
This manual approach wastes time and clutters your code with repetitive boilerplate.
It also makes your workflow harder to read and maintain because you have many tiny functions doing very little.
RunnablePassthrough and RunnableLambda let you create simple, reusable steps easily.
RunnablePassthrough just passes data through unchanged, while RunnableLambda lets you define quick inline functions without extra setup.
def process(data): return data result = process(input_data)
passthrough = RunnablePassthrough() result = passthrough.invoke(input_data)
They enable fast, clean chaining of operations in your workflows without extra code clutter.
When building a data pipeline, you can insert a RunnablePassthrough to debug or log data without changing the flow.
Or use RunnableLambda to quickly apply a small transformation inline.
Manual small-step functions add clutter and slow development.
RunnablePassthrough and RunnableLambda simplify creating quick, reusable steps.
They make workflows cleaner, easier to read, and faster to build.
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
