LLMRouterChain Class — langchain Architecture
Architecture documentation for the LLMRouterChain class in llm_router.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 6583c2eb_e73e_04a5_2dfe_d3e26b9ec1eb["LLMRouterChain"] 8c933306_16e9_7f72_a392_80e52d7a7371["RouterChain"] 6583c2eb_e73e_04a5_2dfe_d3e26b9ec1eb -->|extends| 8c933306_16e9_7f72_a392_80e52d7a7371 12b08e15_dda7_34a6_0911_10fbdd2f2a7b["llm_router.py"] 6583c2eb_e73e_04a5_2dfe_d3e26b9ec1eb -->|defined in| 12b08e15_dda7_34a6_0911_10fbdd2f2a7b 56745521_2936_9cc1_c805_d317353974fe["_validate_prompt()"] 6583c2eb_e73e_04a5_2dfe_d3e26b9ec1eb -->|method| 56745521_2936_9cc1_c805_d317353974fe eba221c9_aa38_e298_cf9a_bcbc4aed2278["input_keys()"] 6583c2eb_e73e_04a5_2dfe_d3e26b9ec1eb -->|method| eba221c9_aa38_e298_cf9a_bcbc4aed2278 39ce415b_b0c9_e015_5e2b_1408f4edea5e["_validate_outputs()"] 6583c2eb_e73e_04a5_2dfe_d3e26b9ec1eb -->|method| 39ce415b_b0c9_e015_5e2b_1408f4edea5e 87ccce0a_1b7e_e8ff_7889_acaf7978975c["_call()"] 6583c2eb_e73e_04a5_2dfe_d3e26b9ec1eb -->|method| 87ccce0a_1b7e_e8ff_7889_acaf7978975c f3be5ec5_70d6_5fa7_102d_c7ef16f74255["_acall()"] 6583c2eb_e73e_04a5_2dfe_d3e26b9ec1eb -->|method| f3be5ec5_70d6_5fa7_102d_c7ef16f74255 7e98a1df_1041_92f3_e31b_2dfd178eb89d["from_llm()"] 6583c2eb_e73e_04a5_2dfe_d3e26b9ec1eb -->|method| 7e98a1df_1041_92f3_e31b_2dfd178eb89d
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/router/llm_router.py lines 33–164
class LLMRouterChain(RouterChain):
"""A router chain that uses an LLM chain to perform routing.
This class is deprecated. See below for a replacement, which offers several
benefits, including streaming and batch support.
Below is an example implementation:
```python
from operator import itemgetter
from typing import Literal
from typing_extensions import TypedDict
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o-mini")
prompt_1 = ChatPromptTemplate.from_messages(
[
("system", "You are an expert on animals."),
("human", "{query}"),
]
)
prompt_2 = ChatPromptTemplate.from_messages(
[
("system", "You are an expert on vegetables."),
("human", "{query}"),
]
)
chain_1 = prompt_1 | model | StrOutputParser()
chain_2 = prompt_2 | model | StrOutputParser()
route_system = "Route the user's query to either the animal "
"or vegetable expert."
route_prompt = ChatPromptTemplate.from_messages(
[
("system", route_system),
("human", "{query}"),
]
)
class RouteQuery(TypedDict):
\"\"\"Route query to destination.\"\"\"
destination: Literal["animal", "vegetable"]
route_chain = (
route_prompt
| model.with_structured_output(RouteQuery)
| itemgetter("destination")
)
chain = {
"destination": route_chain, # "animal" or "vegetable"
"query": lambda x: x["query"], # pass through input query
} | RunnableLambda(
# if animal, chain_1. otherwise, chain_2.
lambda x: chain_1 if x["destination"] == "animal" else chain_2,
)
chain.invoke({"query": "what color are carrots"})
```
"""
llm_chain: LLMChain
"""LLM chain used to perform routing"""
@model_validator(mode="after")
def _validate_prompt(self) -> Self:
prompt = self.llm_chain.prompt
if prompt.output_parser is None:
msg = (
"LLMRouterChain requires base llm_chain prompt to have an output"
" parser that converts LLM text output to a dictionary with keys"
" 'destination' and 'next_inputs'. Received a prompt with no output"
Extends
Source
Frequently Asked Questions
What is the LLMRouterChain class?
LLMRouterChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/router/llm_router.py.
Where is LLMRouterChain defined?
LLMRouterChain is defined in libs/langchain/langchain_classic/chains/router/llm_router.py at line 33.
What does LLMRouterChain extend?
LLMRouterChain extends RouterChain.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free