RouterRunnable Class — langchain Architecture
Architecture documentation for the RouterRunnable class in router.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1["RouterRunnable"] 08837a8a_ba43_28b7_1ac5_6c54611f8c1b["router.py"] 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1 -->|defined in| 08837a8a_ba43_28b7_1ac5_6c54611f8c1b 8ea48064_4166_bd18_0b00_72876f9b05cc["config_specs()"] 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1 -->|method| 8ea48064_4166_bd18_0b00_72876f9b05cc c6cd3a35_a873_3b09_8b39_a54f64a1312a["__init__()"] 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1 -->|method| c6cd3a35_a873_3b09_8b39_a54f64a1312a 6575104f_b1fe_3825_91bd_a8a3d75b6981["is_lc_serializable()"] 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1 -->|method| 6575104f_b1fe_3825_91bd_a8a3d75b6981 2be850b7_cf53_0eed_7a29_826f3c28e214["get_lc_namespace()"] 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1 -->|method| 2be850b7_cf53_0eed_7a29_826f3c28e214 8ef1341d_4d2d_1e85_ef8b_7b1a46cbfe15["invoke()"] 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1 -->|method| 8ef1341d_4d2d_1e85_ef8b_7b1a46cbfe15 66770e3a_8e4a_3cb6_25bf_9dd72f2a0655["ainvoke()"] 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1 -->|method| 66770e3a_8e4a_3cb6_25bf_9dd72f2a0655 267f4457_b3c5_103f_c5cc_b9218a5cc0a2["batch()"] 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1 -->|method| 267f4457_b3c5_103f_c5cc_b9218a5cc0a2 a371e741_891e_9391_fba5_54e26c40140c["abatch()"] 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1 -->|method| a371e741_891e_9391_fba5_54e26c40140c 7fe334ab_f637_a81d_83f7_0308940061e7["stream()"] 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1 -->|method| 7fe334ab_f637_a81d_83f7_0308940061e7 085bf329_19e3_3b21_96e8_c51dae65adbb["astream()"] 2ba4f1aa_5a12_3f0e_0437_22697ec3b0e1 -->|method| 085bf329_19e3_3b21_96e8_c51dae65adbb
Relationship Graph
Source Code
libs/core/langchain_core/runnables/router.py lines 46–239
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})
```
"""
runnables: Mapping[str, Runnable[Any, Output]]
@property
@override
def config_specs(self) -> list[ConfigurableFieldSpec]:
return get_unique_config_specs(
spec for step in self.runnables.values() for spec in step.config_specs
)
def __init__(
self,
runnables: Mapping[str, Runnable[Any, Output] | Callable[[Any], Output]],
) -> None:
"""Create a `RouterRunnable`.
Args:
runnables: A mapping of keys to `Runnable` objects.
"""
super().__init__(
runnables={key: coerce_to_runnable(r) for key, r in runnables.items()}
)
model_config = ConfigDict(
arbitrary_types_allowed=True,
)
@classmethod
@override
def is_lc_serializable(cls) -> bool:
"""Return `True` as this class is serializable."""
return True
@classmethod
@override
def get_lc_namespace(cls) -> list[str]:
"""Get the namespace of the LangChain object.
Returns:
`["langchain", "schema", "runnable"]`
"""
return ["langchain", "schema", "runnable"]
@override
def invoke(
self, input: RouterInput, config: RunnableConfig | None = None, **kwargs: Any
) -> Output:
key = input["key"]
actual_input = input["input"]
if key not in self.runnables:
msg = f"No runnable associated with key '{key}'"
raise ValueError(msg)
runnable = self.runnables[key]
return runnable.invoke(actual_input, config)
@override
async def ainvoke(
self,
input: RouterInput,
config: RunnableConfig | None = None,
**kwargs: Any | None,
) -> Output:
key = input["key"]
Defined In
Source
Frequently Asked Questions
What is the RouterRunnable class?
RouterRunnable is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/router.py.
Where is RouterRunnable defined?
RouterRunnable is defined in libs/core/langchain_core/runnables/router.py at line 46.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free