0
0
LangChainframework~10 mins

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

Choose your learning style9 modes available
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.