langchain_core.runnables.base.RunnableBinding¶
class langchain_core.runnables.base.RunnableBinding[source]¶
Bases: RunnableBindingBase[Input, Output]
Wrap a runnable with additional functionality.
A RunnableBinding can be thought of as a “runnable decorator” that
preserves the essential features of Runnable; i.e., batching, streaming,
and async support, while adding additional functionality.
Any class that inherits from Runnable can be bound to a RunnableBinding.
Runnables expose a standard set of methods for creating RunnableBindings
or sub-classes of RunnableBindings (e.g., RunnableRetry,
RunnableWithFallbacks) that add additional functionality.
These methods include:
- bind: Bind kwargs to pass to the underlying runnable when running it.
- with_config: Bind config to pass to the underlying runnable when running it.
- with_listeners:  Bind lifecycle listeners to the underlying runnable.
- with_types: Override the input and output types of the underlying runnable.
- with_retry: Bind a retry policy to the underlying runnable.
- with_fallbacks: Bind a fallback policy to the underlying runnable.
Example:
bind: Bind kwargs to pass to the underlying runnable when running it.
# Create a runnable binding that invokes the ChatModel with the
# additional kwarg `stop=['-']` when running it.
from langchain.chat_models import ChatOpenAI
model = ChatOpenAI()
model.invoke('Say "Parrot-MAGIC"', stop=['-']) # Should return `Parrot`
# Using it the easy way via `bind` method which returns a new
# RunnableBinding
runnable_binding = model.bind(stop=['-'])
runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot`
Can also be done by instantiating a RunnableBinding directly (not recommended):