Home / Class/ RunnableBranch Class — langchain Architecture

RunnableBranch Class — langchain Architecture

Architecture documentation for the RunnableBranch class in branch.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  8c770be5_e4c6_a7e0_4227_c2085f2c1314["RunnableBranch"]
  051881b9_3e20_4600_ec05_faa44ea54a4d["branch.py"]
  8c770be5_e4c6_a7e0_4227_c2085f2c1314 -->|defined in| 051881b9_3e20_4600_ec05_faa44ea54a4d
  d857a65b_857b_e924_118a_bc4f5364cfaa["__init__()"]
  8c770be5_e4c6_a7e0_4227_c2085f2c1314 -->|method| d857a65b_857b_e924_118a_bc4f5364cfaa
  e5f124f3_f4fa_8697_5e99_69fd719ba5e9["is_lc_serializable()"]
  8c770be5_e4c6_a7e0_4227_c2085f2c1314 -->|method| e5f124f3_f4fa_8697_5e99_69fd719ba5e9
  a4e07842_df81_477e_3e4f_70e0a9010725["get_lc_namespace()"]
  8c770be5_e4c6_a7e0_4227_c2085f2c1314 -->|method| a4e07842_df81_477e_3e4f_70e0a9010725
  0279c3b1_26ba_49d7_9e5e_72d76bf77816["get_input_schema()"]
  8c770be5_e4c6_a7e0_4227_c2085f2c1314 -->|method| 0279c3b1_26ba_49d7_9e5e_72d76bf77816
  fbdb94c1_32d3_7c09_e62c_575de0bd0205["config_specs()"]
  8c770be5_e4c6_a7e0_4227_c2085f2c1314 -->|method| fbdb94c1_32d3_7c09_e62c_575de0bd0205
  9f958f06_98f9_3ba8_4239_f2c7fb815242["invoke()"]
  8c770be5_e4c6_a7e0_4227_c2085f2c1314 -->|method| 9f958f06_98f9_3ba8_4239_f2c7fb815242
  8fe0517c_6208_0ec9_8513_64b9f8430708["ainvoke()"]
  8c770be5_e4c6_a7e0_4227_c2085f2c1314 -->|method| 8fe0517c_6208_0ec9_8513_64b9f8430708
  b69050e0_7f2d_540b_c705_85c3001d53a4["stream()"]
  8c770be5_e4c6_a7e0_4227_c2085f2c1314 -->|method| b69050e0_7f2d_540b_c705_85c3001d53a4
  e7b52750_c385_20a6_da26_fddbf6b4f003["astream()"]
  8c770be5_e4c6_a7e0_4227_c2085f2c1314 -->|method| e7b52750_c385_20a6_da26_fddbf6b4f003

Relationship Graph

Source Code

libs/core/langchain_core/runnables/branch.py lines 42–461

class RunnableBranch(RunnableSerializable[Input, Output]):
    """`Runnable` that selects which branch to run based on a condition.

    The `Runnable` is initialized with a list of `(condition, Runnable)` pairs and
    a default branch.

    When operating on an input, the first condition that evaluates to True is
    selected, and the corresponding `Runnable` is run on the input.

    If no condition evaluates to `True`, the default branch is run on the input.

    Examples:
        ```python
        from langchain_core.runnables import RunnableBranch

        branch = RunnableBranch(
            (lambda x: isinstance(x, str), lambda x: x.upper()),
            (lambda x: isinstance(x, int), lambda x: x + 1),
            (lambda x: isinstance(x, float), lambda x: x * 2),
            lambda x: "goodbye",
        )

        branch.invoke("hello")  # "HELLO"
        branch.invoke(None)  # "goodbye"
        ```
    """

    branches: Sequence[tuple[Runnable[Input, bool], Runnable[Input, Output]]]
    """A list of `(condition, Runnable)` pairs."""
    default: Runnable[Input, Output]
    """A `Runnable` to run if no condition is met."""

    def __init__(
        self,
        *branches: tuple[
            Runnable[Input, bool]
            | Callable[[Input], bool]
            | Callable[[Input], Awaitable[bool]],
            RunnableLike,
        ]
        | RunnableLike,
    ) -> None:
        """A `Runnable` that runs one of two branches based on a condition.

        Args:
            *branches: A list of `(condition, Runnable)` pairs.
                Defaults a `Runnable` to run if no condition is met.

        Raises:
            ValueError: If the number of branches is less than `2`.
            TypeError: If the default branch is not `Runnable`, `Callable` or `Mapping`.
            TypeError: If a branch is not a `tuple` or `list`.
            ValueError: If a branch is not of length `2`.
        """
        if len(branches) < _MIN_BRANCHES:
            msg = "RunnableBranch requires at least two branches"
            raise ValueError(msg)

        default = branches[-1]

        if not isinstance(
            default,
            (Runnable, Callable, Mapping),  # type: ignore[arg-type]
        ):
            msg = "RunnableBranch default must be Runnable, callable or mapping."
            raise TypeError(msg)

        default_ = cast(
            "Runnable[Input, Output]", coerce_to_runnable(cast("RunnableLike", default))
        )

        branches_ = []

        for branch in branches[:-1]:
            if not isinstance(branch, (tuple, list)):
                msg = (
                    f"RunnableBranch branches must be "
                    f"tuples or lists, not {type(branch)}"
                )
                raise TypeError(msg)

Frequently Asked Questions

What is the RunnableBranch class?
RunnableBranch is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/branch.py.
Where is RunnableBranch defined?
RunnableBranch is defined in libs/core/langchain_core/runnables/branch.py at line 42.

Analyze Your Own Codebase

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

Try Supermodel Free