Lesson 11 / 31
Routing and Branching
Send each input down the right path.
Decide, then act
Real apps need different handling for different inputs: billing questions to one prompt, technical questions to another, small talk to a cheap model. RunnableBranch takes a list of (condition, runnable) pairs and a default; the first condition that is true selects the path. Conditions can be plain Python or the result of an earlier classifier step (often a small, cheap LLM call that returns a label). For more complex flows with loops and shared state, use LangGraph. Always include a default branch and test unusual inputs.
Routing by keyword, run
I ran this offline in a Python virtual environment with langchain-core 1.6.6, langchain-text-splitters 1.1.2 and llama-index-core 0.14.25. No API key or network call is needed because a fake model or a toy embedding stands in for the real one. The first matching condition wins; anything unmatched goes to the default route.
from langchain_core.runnables import RunnableBranch, RunnableLambda
route = RunnableBranch(
(lambda x: "refund" in x.lower(), RunnableLambda(lambda x: "billing team")),
(lambda x: "crash" in x.lower(), RunnableLambda(lambda x: "engineering")),
RunnableLambda(lambda x: "general support"),
)
for q in ["I want a refund", "App crash on start", "Where is your office?"]:
print(q, "->", route.invoke(q))
Output:
I want a refund -> billing team App crash on start -> engineering Where is your office? -> general support
Quick check: What should a RunnableBranch always have?
- A trained model
- Exactly one condition
- A GPU
- A default branch for inputs that match no condition
Answer
A default branch for inputs that match no condition — Without a default, unexpected inputs have nowhere to go.