MCPcopy
hub / github.com/langchain-ai/langchain / RunnableLambda

Class RunnableLambda

libs/core/langchain_core/runnables/base.py:3572–4218  ·  view source on GitHub ↗

RunnableLambda converts a python callable into a Runnable. Wrapping a callable in a RunnableLambda makes the callable usable within either a sync or async context. RunnableLambda can be composed as any other Runnable and provides seamless integration with LangChain tracing. `R

Source from the content-addressed store, hash-verified

3570
3571
3572class RunnableLambda(Runnable[Input, Output]):
3573 """RunnableLambda converts a python callable into a Runnable.
3574
3575 Wrapping a callable in a RunnableLambda makes the callable usable
3576 within either a sync or async context.
3577
3578 RunnableLambda can be composed as any other Runnable and provides
3579 seamless integration with LangChain tracing.
3580
3581 `RunnableLambda` is best suited for code that does not need to support
3582 streaming. If you need to support streaming (i.e., be able to operate
3583 on chunks of inputs and yield chunks of outputs), use `RunnableGenerator`
3584 instead.
3585
3586 Examples:
3587
3588 .. code-block:: python
3589
3590 # This is a RunnableLambda
3591 from langchain_core.runnables import RunnableLambda
3592
3593 def add_one(x: int) -> int:
3594 return x + 1
3595
3596 runnable = RunnableLambda(add_one)
3597
3598 runnable.invoke(1) # returns 2
3599 runnable.batch([1, 2, 3]) # returns [2, 3, 4]
3600
3601 # Async is supported by default by delegating to the sync implementation
3602 await runnable.ainvoke(1) # returns 2
3603 await runnable.abatch([1, 2, 3]) # returns [2, 3, 4]
3604
3605
3606 # Alternatively, can provide both synd and sync implementations
3607 async def add_one_async(x: int) -> int:
3608 return x + 1
3609
3610 runnable = RunnableLambda(add_one, afunc=add_one_async)
3611 runnable.invoke(1) # Uses add_one
3612 await runnable.ainvoke(1) # Uses add_one_async
3613 """
3614
3615 def __init__(
3616 self,
3617 func: Union[
3618 Union[
3619 Callable[[Input], Output],
3620 Callable[[Input], Iterator[Output]],
3621 Callable[[Input, RunnableConfig], Output],
3622 Callable[[Input, CallbackManagerForChainRun], Output],
3623 Callable[[Input, CallbackManagerForChainRun, RunnableConfig], Output],
3624 ],
3625 Union[
3626 Callable[[Input], Awaitable[Output]],
3627 Callable[[Input], AsyncIterator[Output]],
3628 Callable[[Input, RunnableConfig], Awaitable[Output]],
3629 Callable[[Input, AsyncCallbackManagerForChainRun], Awaitable[Output]],

Callers 15

llm_with_toolsFunction · 0.90
multi_modal_rag_chainFunction · 0.90
chain.pyFile · 0.90
multi_modal_rag_chainFunction · 0.90
multi_modal_rag_chainFunction · 0.90
web.pyFile · 0.90
chain.pyFile · 0.90
chain.pyFile · 0.90
chain.pyFile · 0.90
multi_modal_rag_chainFunction · 0.90
chain.pyFile · 0.90
proposal_chain.pyFile · 0.90

Calls

no outgoing calls