Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 32 ยท Chains and LCEL

Branching, Routing and RunnableLambda

Route a request to different chains with RunnableLambda and RunnableBranch, classify the input first, and build a runnable stand-in router with fallbacks.

Not every request deserves the same treatment. A support bot answers billing questions with one prompt and technical questions with another; a code assistant uses a bigger model for hard problems and a small one for simple ones. Routing means choosing the next step based on the input. In LCEL there are two ways: RunnableBranch, which tests conditions in order, and a RunnableLambda that simply returns the Runnable to run. This lesson covers both, and the classify-then-route pattern behind most routers.

RunnableLambda: any function as a step

A RunnableLambda wraps an ordinary function so it can sit in a chain. If the function returns a Runnable, LangChain runs that Runnable with the same input. That single rule gives you dynamic routing with normal Python if statements:

Python
from langchain_core.runnables import RunnableLambda

def route(info: dict):
    if info["topic"] == "billing":
        return billing_chain
    if info["topic"] == "technical":
        return technical_chain
    return general_chain

full_chain = (
    {"topic": classifier_chain, "question": lambda x: x["question"]}
    | RunnableLambda(route)
)
full_chain.invoke({"question": "Why was I charged twice?"})

The recommended style today is exactly this: a plain function with ordinary control flow. It is easier to read, test and debug than a table of conditions.

RunnableBranch: conditions as data

RunnableBranch takes (condition, runnable) pairs and a default. It tests the conditions in order and runs the first match:

Python
from langchain_core.runnables import RunnableBranch

branch = RunnableBranch(
    (lambda x: x["topic"] == "billing", billing_chain),
    (lambda x: x["topic"] == "technical", technical_chain),
    general_chain,                      # the default, no condition
)

It behaves like an if / elif / else. Use it when you want the routing rules visible as one object. For anything more than three or four cases, a function is usually clearer.

flowchart TB
    Q["User question"] --> C["Classifier<br/>cheap model, temperature 0"]
    C -->|"billing"| B["Billing chain"]
    C -->|"technical"| T["Technical chain"]
    C -->|"other"| G["General chain"]
    B --> O["Answer"]
    T --> O
    G --> O

A classifier picks the path; each path is a complete chain of its own.

The classifier

Routing quality depends on the classifier step. Keep it small and strict: a short prompt, a cheap model, temperature 0, and ideally structured output so the label is one of a fixed set.

Python
from typing import Literal
from pydantic import BaseModel

class Route(BaseModel):
    topic: Literal["billing", "technical", "general"]

classifier = (
    ChatPromptTemplate.from_messages([
        ("system", "Pick the best topic for the customer message."),
        ("human", "{question}"),
    ])
    | small_model.with_structured_output(Route)
    | (lambda r: r.topic)
)

Because the output is constrained to three strings, the routing function never sees a surprise label. Always keep a default route for the unexpected.

A router in plain Python (runnable)

This stand-in has a rule-based classifier (so it runs offline) and three handler "chains". The structure is the same as the real one: classify, then pick a chain through a function.

Python Stand-in: classify, then route

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The dictionary lookup with .get(..., default) is the whole idea of a default branch.

RunnableBranch as conditions (runnable)

Here is the same decision written as ordered (condition, handler) pairs with a default, the shape of RunnableBranch:

Python Stand-in: ordered conditions with a default

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Order matters: the first matching condition wins, exactly like elif.

Other routing ideas

  • Model routing. Send easy prompts to a small, cheap model and hard ones to a large model. The classifier can be a heuristic (prompt length, keywords) rather than an LLM.
  • Semantic routing. Embed the question and compare with example questions per route; pick the nearest. It is cheap and fast, and uses the same ideas as the embeddings lesson.
  • Tool use. Letting the model itself choose among tools is the generalization of routing; that is what agents do.
  • Guardrails. A first branch can refuse or sanitize unsafe requests before they reach an expensive chain.

Testing routers

A router is a decision function, so test it like one: write a table of example questions with the expected route and run it whenever you change the classifier. Misrouting is quiet because every route still produces fluent text. A routing test set catches it.

Python Stand-in: a tiny routing test table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens when a RunnableLambda's function returns a Runnable?

    This is the simplest way to route dynamically: a Python function chooses which chain to run.
  2. 2. How does RunnableBranch choose what to run?

    It behaves like if, elif and else.
  3. 3. Which classifier setup is best for routing?

    Routing needs consistent, constrained labels; structured output with a Literal type provides that.
  4. 4. Why keep a default route?

    Without a default, anything the classifier does not anticipate would crash or misroute.

Summary

  • Routing picks the next chain based on the input; classify first, then route.
  • A RunnableLambda returning a Runnable is the simplest dynamic router.
  • RunnableBranch expresses ordered conditions plus a default.
  • Use small, strict, structured classifiers and always include a default route.
  • Test routers with a table of labeled examples.

Next: Retries, Fallbacks and Config