Home / Class/ RunnableBinding Class — langchain Architecture

RunnableBinding Class — langchain Architecture

Architecture documentation for the RunnableBinding class in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  7c4bbf04_1945_a08e_31ca_a0cbf44c8755["RunnableBinding"]
  5f3c1f1c_6f8a_e293_7cb5_97c21b4bf214["base.py"]
  7c4bbf04_1945_a08e_31ca_a0cbf44c8755 -->|defined in| 5f3c1f1c_6f8a_e293_7cb5_97c21b4bf214
  0c8cd7ca_d160_bbff_afb4_48f8645422d8["bind()"]
  7c4bbf04_1945_a08e_31ca_a0cbf44c8755 -->|method| 0c8cd7ca_d160_bbff_afb4_48f8645422d8
  dfbf4743_c4b6_baff_73dd_528f924664a0["with_config()"]
  7c4bbf04_1945_a08e_31ca_a0cbf44c8755 -->|method| dfbf4743_c4b6_baff_73dd_528f924664a0
  e6341caf_f852_4660_4c70_2814e280a4af["with_listeners()"]
  7c4bbf04_1945_a08e_31ca_a0cbf44c8755 -->|method| e6341caf_f852_4660_4c70_2814e280a4af
  bf16a1c6_7087_8dcc_6ccf_5fced2b56509["with_types()"]
  7c4bbf04_1945_a08e_31ca_a0cbf44c8755 -->|method| bf16a1c6_7087_8dcc_6ccf_5fced2b56509
  480efcd3_b105_862e_62a7_8ffbbdc24e87["with_retry()"]
  7c4bbf04_1945_a08e_31ca_a0cbf44c8755 -->|method| 480efcd3_b105_862e_62a7_8ffbbdc24e87
  9a1cf7e5_6ab1_e00d_4689_e0d92f3ef9a7["__getattr__()"]
  7c4bbf04_1945_a08e_31ca_a0cbf44c8755 -->|method| 9a1cf7e5_6ab1_e00d_4689_e0d92f3ef9a7

Relationship Graph

Source Code

libs/core/langchain_core/runnables/base.py lines 5932–6137

class RunnableBinding(RunnableBindingBase[Input, Output]):  # type: ignore[no-redef]
    """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.

        ```python
        # 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):

        ```python
        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`
        ```
    """

    @override
    def bind(self, **kwargs: Any) -> Runnable[Input, Output]:
        """Bind additional kwargs to a `Runnable`, returning a new `Runnable`.

        Args:
            **kwargs: The kwargs to bind to the `Runnable`.

        Returns:
            A new `Runnable` with the same type and config as the original,
            but with the additional kwargs bound.

        """
        return self.__class__(
            bound=self.bound,
            config=self.config,
            config_factories=self.config_factories,
            kwargs={**self.kwargs, **kwargs},
            custom_input_type=self.custom_input_type,
            custom_output_type=self.custom_output_type,
        )

    @override
    def with_config(
        self,
        config: RunnableConfig | None = None,
        # Sadly Unpack is not well supported by mypy so this will have to be untyped
        **kwargs: Any,
    ) -> Runnable[Input, Output]:
        return self.__class__(

Frequently Asked Questions

What is the RunnableBinding class?
RunnableBinding is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is RunnableBinding defined?
RunnableBinding is defined in libs/core/langchain_core/runnables/base.py at line 5932.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free