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 chat model with the
# additional kwarg `stop=['-']` when running it.
from langchain_openai 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):
from langchain_core.runnables import RunnableBinding
runnable_binding = RunnableBinding(
bound=model,
kwargs={"stop": ["-"]}, # <-- Note the additional kwargs
)
runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot`RunnableBinding(
self,
*,
bound: Runnable[Input, Output],
kwargs: Mapping[str, Any] | None = None,
config: RunnableConfig | None = None,
config_factories: list[Callable[[RunnableConfig], RunnableConfig]] | None = None,
custom_input_type: type[Input] | BaseModel | None = None,
custom_output_type: type[Output] | BaseModel | None = None,
**other_kwargs: Any = {}
)Bind additional kwargs to a Runnable, returning a new Runnable.
Bind lifecycle listeners to a Runnable, returning a new Runnable.
The Run object contains information about the run, including its id,
type, input, output, error, start_time, end_time, and
any tags or metadata added to the run.
The underlying Runnable that this Runnable delegates to.
kwargs to pass to the underlying Runnable when running.
The config to bind to the underlying Runnable.
The config factories to bind to the underlying Runnable.
Override the input type of the underlying Runnable with a custom type.
Override the output type of the underlying Runnable with a custom type.
The name of the Runnable. Used for debugging and tracing.
Input type.
Output Type.
The type of input this Runnable accepts specified as a Pydantic model.
Output schema.
List configurable fields for this Runnable.
Get the name of the Runnable.
Get a Pydantic model that can be used to validate input to the Runnable.
Get a JSON schema that represents the input to the Runnable.
Get a Pydantic model that can be used to validate output to the Runnable.
Get a JSON schema that represents the output of the Runnable.
The type of config this Runnable accepts specified as a Pydantic model.
Get a JSON schema that represents the config of the Runnable.
Return a graph representation of this Runnable.
Return a list of prompts used by this Runnable.
Pipe Runnable objects.
Pick keys from the output dict of this Runnable.
Assigns new fields to the dict output of this Runnable.
Transform a single input into an output.
Transform a single input into an output.
Default implementation runs invoke in parallel using a thread pool executor.
Run invoke in parallel on a list of inputs.
Default implementation runs ainvoke in parallel using asyncio.gather.
Run ainvoke in parallel on a list of inputs.
Default implementation of stream, which calls invoke.
Default implementation of astream, which calls ainvoke.
Stream all output from a Runnable, as reported to the callback system.
Generate a stream of events.
Transform inputs to outputs.
Transform inputs to outputs.
Bind async lifecycle listeners to a Runnable.
Return a new Runnable that maps a list of inputs to a list of outputs.
Add fallbacks to a Runnable, returning a new Runnable.
Create a BaseTool from a Runnable.