LangChain Mastery

Course Content

LangChain Mastery

7 sections · 109 lessons

How do you implement a chain with dynamic routing in LangChain?


A small-model router with a closed label setRouter:Literal of 5 labelsnew_project — project pricesresale — resale listingsrental — rental listingshome_loan —loan partner flowother — hand to a human
A Literal type means the router cannot invent a sixth label, and the other route catches the 4% nobody planned for.

What you need to know

Python
from typing import Literalfrom pydantic import BaseModelfrom langchain_core.runnables import RunnableLambda, RunnablePassthroughclass Route(BaseModel):    topic: Literal["billing", "technical", "other"]router = router_prompt | small_llm.with_structured_output(Route)def pick_chain(x: dict):    return {"billing": billing_chain,            "technical": tech_chain}.get(x["route"].topic, general_chain)routed = RunnablePassthrough.assign(route=router) | RunnableLambda(pick_chain)routed.invoke({"question": "I was charged twice for March"})
  • The router uses a small, fast model; classification is an easy task.
  • Literal limits the output to three labels, so the model cannot return "Billing issue" or "refunds".
  • pick_chain returns a Runnable, and LangChain invokes it with the same input. The .get(..., general_chain) default catches anything unexpected.

The RunnableBranch form

Python
from langchain_core.runnables import RunnableBranchbranch = RunnableBranch(    (lambda x: x["route"].topic == "billing", billing_chain),    (lambda x: x["route"].topic == "technical", tech_chain),    general_chain,                          # default, required)routed = RunnablePassthrough.assign(route=router) | branch

Conditions are checked in order; the first true one wins. It is more verbose than a function but shows the routes clearly in one place.

Other ways to route

MethodWhen
LLM classifier (above)Topics need understanding of the text
Embedding similarity to example questionsMany routes, low latency, no extra LLM call
Plain rules (keywords, user plan)The signal is simple and exact
An agent choosing toolsThe path depends on results, not just the question

A real-life example

A real-estate firm's WhatsApp assistant receives enquiries for new flats, resale, rentals and home loans. One general prompt handled all of them and often quoted new-project prices to people asking about rent. The team added a router with five labels (new_project, resale, rental, home_loan, other), each routed to its own prompt and listings retriever. The router runs on a small model and adds about 250 milliseconds. On 600 labelled messages, routing accuracy was 94%; most errors were rental versus resale, so the team added examples of both to the router prompt. The other route sends the chat to a human agent, which catches roughly 4% of messages.

Follow-up questions to expect

  • "What happens if the router is wrong?" — The wrong chain answers; reduce this with a default route, a confidence threshold that sends unsure cases to a general chain, and regular checks of logged routes.
  • "RunnableBranch or a function?" — Both work; a function returning a Runnable is shorter and easier to read with many routes.
  • "Router or agent?" — A router decides once at the start; an agent decides repeatedly as it goes. Use a router when one decision is enough.