Performance: RunnablePassthrough and RunnableLambda
This concept affects how quickly and efficiently small tasks or functions run within a LangChain workflow, impacting interaction responsiveness and overall execution speed.
Jump into concepts and practice - no test required
from langchain.schema.runnables import RunnablePassthrough passthrough = RunnablePassthrough() result = passthrough.invoke(data)
class CustomRunnable: def invoke(self, input): return input custom = CustomRunnable() result = custom.invoke(data)
| Pattern | Function Calls | Call Stack Depth | Execution Overhead | Verdict |
|---|---|---|---|---|
| Custom class passthrough | 2+ | Increased | Higher due to extra layers | [X] Bad |
| RunnablePassthrough | 1 | Minimal | Minimal overhead | [OK] Good |
| Wrapped function in class | 3+ | Increased | Higher due to wrapping | [X] Bad |
| RunnableLambda with inline function | 1 | Minimal | Minimal overhead | [OK] Good |
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.