Home / File/ router.py — langchain Source File

router.py — langchain Source File

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

File python LangChainCore Runnables 7 imports 1 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  54bde527_a6b3_f948_1582_9551c5e53513["router.py"]
  2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"]
  54bde527_a6b3_f948_1582_9551c5e53513 --> 2bf6d401_816d_d011_3b05_a6114f55ff58
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  54bde527_a6b3_f948_1582_9551c5e53513 --> feec1ec4_6917_867b_d228_b134d0ff8099
  dd5e7909_a646_84f1_497b_cae69735550e["pydantic"]
  54bde527_a6b3_f948_1582_9551c5e53513 --> dd5e7909_a646_84f1_497b_cae69735550e
  f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"]
  54bde527_a6b3_f948_1582_9551c5e53513 --> f85fae70_1011_eaec_151c_4083140ae9e5
  15b6f2ac_8a3b_5719_0e4d_4652995195ed["langchain_core.runnables.base"]
  54bde527_a6b3_f948_1582_9551c5e53513 --> 15b6f2ac_8a3b_5719_0e4d_4652995195ed
  a8ec7563_2814_99b3_c6da_61c599efc542["langchain_core.runnables.config"]
  54bde527_a6b3_f948_1582_9551c5e53513 --> a8ec7563_2814_99b3_c6da_61c599efc542
  b4920617_3c91_2457_3f8c_84ba49f64322["langchain_core.runnables.utils"]
  54bde527_a6b3_f948_1582_9551c5e53513 --> b4920617_3c91_2457_3f8c_84ba49f64322
  style 54bde527_a6b3_f948_1582_9551c5e53513 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""`Runnable` that routes to a set of `Runnable` objects."""

from __future__ import annotations

from collections.abc import Mapping
from typing import (
    TYPE_CHECKING,
    Any,
    cast,
)

from pydantic import ConfigDict
from typing_extensions import TypedDict, override

from langchain_core.runnables.base import (
    Runnable,
    RunnableSerializable,
    coerce_to_runnable,
)
from langchain_core.runnables.config import (
    RunnableConfig,
    get_config_list,
    get_executor_for_config,
)
from langchain_core.runnables.utils import (
    ConfigurableFieldSpec,
    Input,
    Output,
    gather_with_concurrency,
    get_unique_config_specs,
)

if TYPE_CHECKING:
    from collections.abc import AsyncIterator, Callable, Iterator


class RouterInput(TypedDict):
    """Router input."""

    key: str
    """The key to route on."""
    input: Any
    """The input to pass to the selected `Runnable`."""


class RouterRunnable(RunnableSerializable[RouterInput, Output]):
    """`Runnable` that routes to a set of `Runnable` based on `Input['key']`.

    Returns the output of the selected Runnable.

    Example:
        ```python
        from langchain_core.runnables.router import RouterRunnable
        from langchain_core.runnables import RunnableLambda

        add = RunnableLambda(func=lambda x: x + 1)
        square = RunnableLambda(func=lambda x: x**2)

        router = RouterRunnable(runnables={"add": add, "square": square})
        router.invoke({"key": "square", "input": 3})
// ... (180 more lines)

Domain

Subdomains

Functions

Dependencies

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

Frequently Asked Questions

What does router.py do?
router.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, Runnables subdomain.
What functions are defined in router.py?
router.py defines 1 function(s): collections.
What does router.py depend on?
router.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 router.py in the architecture?
router.py is located at libs/core/langchain_core/runnables/router.py (domain: LangChainCore, subdomain: Runnables, 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