Home / File/ branch.py — langchain Source File

branch.py — langchain Source File

Architecture documentation for branch.py, a python file in the langchain codebase. 7 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  9b18dfd5_b059_e4a8_2a9f_8ec998542125["branch.py"]
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  9b18dfd5_b059_e4a8_2a9f_8ec998542125 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  9b18dfd5_b059_e4a8_2a9f_8ec998542125 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  9b18dfd5_b059_e4a8_2a9f_8ec998542125 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  9b18dfd5_b059_e4a8_2a9f_8ec998542125 --> 91721f45_4909_e489_8c1f_084f8bd87145
  c764ccae_0d75_abec_7c23_6d5d1949a7ba["langchain_core.runnables.base"]
  9b18dfd5_b059_e4a8_2a9f_8ec998542125 --> c764ccae_0d75_abec_7c23_6d5d1949a7ba
  2971f9da_6393_a3e3_610e_ace3d35ee978["langchain_core.runnables.config"]
  9b18dfd5_b059_e4a8_2a9f_8ec998542125 --> 2971f9da_6393_a3e3_610e_ace3d35ee978
  81c04601_d095_a27d_4af1_55e771bb2b6b["langchain_core.runnables.utils"]
  9b18dfd5_b059_e4a8_2a9f_8ec998542125 --> 81c04601_d095_a27d_4af1_55e771bb2b6b
  style 9b18dfd5_b059_e4a8_2a9f_8ec998542125 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Runnable that selects which branch to run based on a condition."""

from collections.abc import (
    AsyncIterator,
    Awaitable,
    Callable,
    Iterator,
    Mapping,
    Sequence,
)
from typing import (
    Any,
    cast,
)

from pydantic import BaseModel, ConfigDict
from typing_extensions import override

from langchain_core.runnables.base import (
    Runnable,
    RunnableLike,
    RunnableSerializable,
    coerce_to_runnable,
)
from langchain_core.runnables.config import (
    RunnableConfig,
    ensure_config,
    get_async_callback_manager_for_config,
    get_callback_manager_for_config,
    patch_config,
)
from langchain_core.runnables.utils import (
    ConfigurableFieldSpec,
    Input,
    Output,
    get_unique_config_specs,
)

_MIN_BRANCHES = 2


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),
// ... (402 more lines)

Subdomains

Classes

Dependencies

  • collections.abc
  • langchain_core.runnables.base
  • langchain_core.runnables.config
  • langchain_core.runnables.utils
  • pydantic
  • typing
  • typing_extensions

Frequently Asked Questions

What does branch.py do?
branch.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What does branch.py depend on?
branch.py imports 7 module(s): collections.abc, langchain_core.runnables.base, langchain_core.runnables.config, langchain_core.runnables.utils, pydantic, typing, typing_extensions.
Where is branch.py in the architecture?
branch.py is located at libs/core/langchain_core/runnables/branch.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/core/langchain_core/runnables).

Analyze Your Own Codebase

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

Try Supermodel Free