Bird
Raised Fist0
LangChainframework~10 mins

RunnablePassthrough and RunnableLambda in LangChain - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - RunnablePassthrough and RunnableLambda
Input Data
Pass input as-is
Output same as input
Next step or result
RunnablePassthrough sends input through unchanged. RunnableLambda applies a function to input and outputs the result.
Execution Sample
LangChain
passthrough = RunnablePassthrough()
lambda_run = RunnableLambda(lambda x: x * 2)

output1 = passthrough.invoke(5)
output2 = lambda_run.invoke(5)
Shows how RunnablePassthrough returns input unchanged and RunnableLambda doubles the input.
Execution Table
StepRunnable TypeInputActionOutput
1RunnablePassthrough5Pass input as-is5
2RunnableLambda5Apply lambda x*210
3RunnablePassthroughHelloPass input as-isHello
4RunnableLambdaHelloApply lambda x*2 (string repeat)HelloHello
5End---
💡 All inputs processed; RunnablePassthrough outputs input unchanged; RunnableLambda outputs function result.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
passthroughRunnablePassthrough instanceSame instanceSame instanceSame instanceSame instanceSame instance
lambda_runRunnableLambda instance with lambda x*2Same instanceSame instanceSame instanceSame instanceSame instance
output1None55555
output2NoneNone10101010
Key Moments - 3 Insights
Why does RunnablePassthrough output exactly what I input?
RunnablePassthrough is designed to return the input unchanged, as shown in execution_table rows 1 and 3.
How does RunnableLambda change the input?
RunnableLambda applies the function you give it to the input, like doubling a number or repeating a string, as seen in rows 2 and 4.
What happens if the lambda function is not compatible with the input?
An error would occur during the invoke call because the function cannot process the input type; this is not shown here but is important to handle.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of RunnableLambda when input is 5?
A5
B10
C25
DNone
💡 Hint
Check row 2 in the execution_table where RunnableLambda processes input 5.
At which step does RunnablePassthrough output the string 'Hello'?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at execution_table row 3 for RunnablePassthrough with input 'Hello'.
If the lambda function was changed to lambda x: x + 1, what would be the output for input 5 at step 2?
A5
B10
C6
DError
💡 Hint
Consider how lambda x: x + 1 changes input 5; see execution_table row 2 for original output.
Concept Snapshot
RunnablePassthrough: returns input unchanged.
RunnableLambda: applies a function to input and returns result.
Use invoke(input) to run.
RunnableLambda needs a function compatible with input type.
Useful for chaining operations in LangChain.
Full Transcript
RunnablePassthrough and RunnableLambda are simple tools in LangChain to process data. RunnablePassthrough just sends the input through without changing it. RunnableLambda takes a function you give it and applies that function to the input, then returns the result. For example, if you give RunnableLambda a function that doubles a number, and input 5, it returns 10. This helps you build chains of operations easily. The execution table shows step-by-step how inputs are handled and outputs produced. Remember, RunnableLambda needs a function that works with the input type or it will cause errors.

Practice

(1/5)
1. What does RunnablePassthrough do with the input it receives?
easy
A. Ignores the input and returns a fixed value
B. Transforms the input using a custom function
C. Returns the input exactly as it is without any changes
D. Throws an error if input is not a string

Solution

  1. Step 1: Understand RunnablePassthrough behavior

    RunnablePassthrough is designed to return whatever input it receives without modifying it.
  2. 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.
  3. Final Answer:

    Returns the input exactly as it is without any changes -> Option C
  4. Quick Check:

    RunnablePassthrough returns input unchanged = A [OK]
Hint: RunnablePassthrough just passes input through unchanged [OK]
Common Mistakes:
  • Thinking RunnablePassthrough modifies input
  • Confusing RunnablePassthrough with RunnableLambda
  • Assuming it throws errors on certain inputs
2. Which of the following is the correct way to create a RunnableLambda that doubles a number input?
easy
A. RunnableLambda(lambda x: x + 2)
B. RunnableLambda(lambda x: x * 2)
C. RunnableLambda(lambda x: x / 2)
D. RunnableLambda(lambda x: x - 2)

Solution

  1. Step 1: Identify the doubling function

    Doubling means multiplying the input by 2, so the function should be lambda x: x * 2.
  2. Step 2: Match with options

    RunnableLambda(lambda x: x * 2) matches the doubling function exactly; others perform addition, division, or subtraction.
  3. Final Answer:

    RunnableLambda(lambda x: x * 2) -> Option B
  4. Quick Check:

    Doubling function uses multiplication by 2 = D [OK]
Hint: Doubling means multiply input by 2 in lambda [OK]
Common Mistakes:
  • Using addition instead of multiplication
  • Confusing division or subtraction for doubling
  • Incorrect lambda syntax
3. What will be the output of this code?
passthrough = RunnablePassthrough()
lambda_runner = RunnableLambda(lambda x: x.upper())
result = lambda_runner.invoke(passthrough.invoke('hello'))
print(result)
medium
A. 'HELLO'
B. 'hello'
C. Error: RunnablePassthrough cannot be invoked
D. 'Hello'

Solution

  1. Step 1: Trace RunnablePassthrough output

    Calling passthrough.invoke('hello') returns 'hello' unchanged.
  2. Step 2: Apply RunnableLambda function

    The lambda converts input to uppercase, so 'hello'.upper() returns 'HELLO'.
  3. Final Answer:

    'HELLO' -> Option A
  4. Quick Check:

    Passthrough returns input, lambda uppercases it = C [OK]
Hint: Passthrough returns input, lambda transforms it [OK]
Common Mistakes:
  • Expecting passthrough to modify input
  • Confusing case conversion result
  • Assuming runtime error on invoke
4. Identify the error in this code snippet:
lambda_runner = RunnableLambda(lambda x: x + 1)
result = lambda_runner.invoke('5')
print(result)
medium
A. TypeError because string '5' cannot be added to integer 1
B. SyntaxError in lambda function
C. RunnableLambda cannot be invoked with strings
D. No error; output will be '51'

Solution

  1. Step 1: Analyze lambda operation on input

    The lambda tries to add 1 to input '5', which is a string, causing a type mismatch.
  2. Step 2: Identify error type

    Adding integer 1 to string '5' raises a TypeError in Python.
  3. Final Answer:

    TypeError because string '5' cannot be added to integer 1 -> Option A
  4. Quick Check:

    Adding int to string causes TypeError = B [OK]
Hint: Adding int to string causes TypeError in Python [OK]
Common Mistakes:
  • Assuming implicit string to int conversion
  • Thinking output is string concatenation
  • Confusing error types
5. You want to create a LangChain workflow that takes a list of numbers, passes it unchanged, then doubles each number. Which combination of RunnablePassthrough and RunnableLambda is correct?
hard
A. Use RunnablePassthrough twice, no lambda needed
B. Use RunnableLambda with lambda x: x, then RunnablePassthrough to double numbers
C. Use RunnableLambda with lambda x: x*2, then RunnablePassthrough to pass list
D. Use RunnablePassthrough to pass the list, then RunnableLambda with lambda x: [i*2 for i in x]

Solution

  1. Step 1: Pass list unchanged with RunnablePassthrough

    RunnablePassthrough returns the list as is, so it fits the first step.
  2. Step 2: Double each number with RunnableLambda

    RunnableLambda with lambda x: [i*2 for i in x] correctly doubles each element in the list.
  3. Final Answer:

    Use RunnablePassthrough to pass the list, then RunnableLambda with lambda x: [i*2 for i in x] -> Option D
  4. Quick Check:

    Passthrough passes list, lambda doubles elements = A [OK]
Hint: Passthrough passes input; lambda transforms list elements [OK]
Common Mistakes:
  • Trying to double list with passthrough
  • Using lambda that multiplies list object, not elements
  • Reversing order of passthrough and lambda