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.
Jump into concepts and practice - no test required
passthrough = RunnablePassthrough() lambda_run = RunnableLambda(lambda x: x * 2) output1 = passthrough.invoke(5) output2 = lambda_run.invoke(5)
| Step | Runnable Type | Input | Action | Output |
|---|---|---|---|---|
| 1 | RunnablePassthrough | 5 | Pass input as-is | 5 |
| 2 | RunnableLambda | 5 | Apply lambda x*2 | 10 |
| 3 | RunnablePassthrough | Hello | Pass input as-is | Hello |
| 4 | RunnableLambda | Hello | Apply lambda x*2 (string repeat) | HelloHello |
| 5 | End | - | - | - |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|---|
| passthrough | RunnablePassthrough instance | Same instance | Same instance | Same instance | Same instance | Same instance |
| lambda_run | RunnableLambda instance with lambda x*2 | Same instance | Same instance | Same instance | Same instance | Same instance |
| output1 | None | 5 | 5 | 5 | 5 | 5 |
| output2 | None | None | 10 | 10 | 10 | 10 |
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.
RunnablePassthrough do with the input it receives?RunnableLambda that doubles a number input?lambda x: x * 2.passthrough = RunnablePassthrough()
lambda_runner = RunnableLambda(lambda x: x.upper())
result = lambda_runner.invoke(passthrough.invoke('hello'))
print(result)passthrough.invoke('hello') returns 'hello' unchanged.'hello'.upper() returns 'HELLO'.lambda_runner = RunnableLambda(lambda x: x + 1)
result = lambda_runner.invoke('5')
print(result)RunnablePassthrough and RunnableLambda is correct?lambda x: [i*2 for i in x] correctly doubles each element in the list.