Home / Class/ RunnableSerializable Class — langchain Architecture

RunnableSerializable Class — langchain Architecture

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

Entity Profile

Dependency Diagram

graph TD
  cf4a5dd2_0ab2_70a5_ef48_4a834231c413["RunnableSerializable"]
  f3658565_d05c_7f49_b7f8_622b7ef34f33["Serializable"]
  cf4a5dd2_0ab2_70a5_ef48_4a834231c413 -->|extends| f3658565_d05c_7f49_b7f8_622b7ef34f33
  5f3c1f1c_6f8a_e293_7cb5_97c21b4bf214["base.py"]
  cf4a5dd2_0ab2_70a5_ef48_4a834231c413 -->|defined in| 5f3c1f1c_6f8a_e293_7cb5_97c21b4bf214
  c3eeeb7f_ae27_dacf_b915_58de5a99a8a0["to_json()"]
  cf4a5dd2_0ab2_70a5_ef48_4a834231c413 -->|method| c3eeeb7f_ae27_dacf_b915_58de5a99a8a0
  790ea45e_3d1f_0375_a787_9ed49583e8e4["configurable_fields()"]
  cf4a5dd2_0ab2_70a5_ef48_4a834231c413 -->|method| 790ea45e_3d1f_0375_a787_9ed49583e8e4
  616e140c_fddf_2de6_006c_392d928a6001["configurable_alternatives()"]
  cf4a5dd2_0ab2_70a5_ef48_4a834231c413 -->|method| 616e140c_fddf_2de6_006c_392d928a6001

Relationship Graph

Source Code

libs/core/langchain_core/runnables/base.py lines 2586–2730

class RunnableSerializable(Serializable, Runnable[Input, Output]):
    """Runnable that can be serialized to JSON."""

    name: str | None = None
    """The name of the `Runnable`.

    Used for debugging and tracing.
    """

    model_config = ConfigDict(
        # Suppress warnings from pydantic protected namespaces
        # (e.g., `model_`)
        protected_namespaces=(),
    )

    @override
    def to_json(self) -> SerializedConstructor | SerializedNotImplemented:
        """Serialize the `Runnable` to JSON.

        Returns:
            A JSON-serializable representation of the `Runnable`.

        """
        dumped = super().to_json()
        with contextlib.suppress(Exception):
            dumped["name"] = self.get_name()
        return dumped

    def configurable_fields(
        self, **kwargs: AnyConfigurableField
    ) -> RunnableSerializable[Input, Output]:
        """Configure particular `Runnable` fields at runtime.

        Args:
            **kwargs: A dictionary of `ConfigurableField` instances to configure.

        Raises:
            ValueError: If a configuration key is not found in the `Runnable`.

        Returns:
            A new `Runnable` with the fields configured.

        !!! example

            ```python
            from langchain_core.runnables import ConfigurableField
            from langchain_openai import ChatOpenAI

            model = ChatOpenAI(max_tokens=20).configurable_fields(
                max_tokens=ConfigurableField(
                    id="output_token_number",
                    name="Max tokens in the output",
                    description="The maximum number of tokens in the output",
                )
            )

            # max_tokens = 20
            print(
                "max_tokens_20: ", model.invoke("tell me something about chess").content
            )

            # max_tokens = 200
            print(
                "max_tokens_200: ",
                model.with_config(configurable={"output_token_number": 200})
                .invoke("tell me something about chess")
                .content,
            )
            ```
        """
        # Import locally to prevent circular import
        from langchain_core.runnables.configurable import (  # noqa: PLC0415
            RunnableConfigurableFields,
        )

        model_fields = type(self).model_fields
        for key in kwargs:
            if key not in model_fields:
                msg = (
                    f"Configuration key {key} not found in {self}: "
                    f"available keys are {model_fields.keys()}"

Extends

Frequently Asked Questions

What is the RunnableSerializable class?
RunnableSerializable is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is RunnableSerializable defined?
RunnableSerializable is defined in libs/core/langchain_core/runnables/base.py at line 2586.
What does RunnableSerializable extend?
RunnableSerializable extends Serializable.

Analyze Your Own Codebase

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

Try Supermodel Free