LangChain Tutorial (Python): building conditional routing for beginners

By Cyprian AaronsUpdated 2026-04-21
langchainbuilding-conditional-routing-for-beginnerspython

This tutorial shows you how to build a simple conditional router in LangChain with Python. You’ll use one input, classify it into a route, and send it to the right chain so your app can answer different kinds of questions with different prompts.

What You'll Need

  • Python 3.10+
  • langchain
  • langchain-openai
  • An OpenAI API key set as OPENAI_API_KEY
  • Basic familiarity with LangChain PromptTemplate, RunnableLambda, and RunnableBranch
  • A terminal and a virtual environment

Install the packages:

pip install langchain langchain-openai

Step-by-Step

  1. Start by creating two specialized chains. One will handle math questions, and the other will handle general questions. The point is not to make them smart by themselves, but to give each route a clear job.
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

math_prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a careful math tutor. Show short, correct steps."),
    ("human", "{question}")
])

general_prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant. Answer clearly and concisely."),
    ("human", "{question}")
])

math_chain = math_prompt | llm
general_chain = general_prompt | llm
  1. Next, create a routing function. This function looks at the user question and decides whether it should go to the math chain or the general chain.
from langchain_core.runnables import RunnableLambda

def route_question(inputs: dict) -> str:
    question = inputs["question"].lower()
    math_keywords = ["calculate", "sum", "multiply", "divide", "equation", "percent", "+", "-", "*", "/"]
    if any(keyword in question for keyword in math_keywords):
        return "math"
    return "general"

router = RunnableLambda(route_question)
  1. Now wire the router into a conditional branch. RunnableBranch lets you define conditions and send the request to the right chain without writing if/else logic around every call.
from langchain_core.runnables import RunnableBranch

conditional_chain = RunnableBranch(
    (lambda x: route_question(x) == "math", math_chain),
    general_chain,
)
  1. Add a small wrapper so both routes return plain text. This makes testing easier and keeps your app output consistent.
from langchain_core.output_parsers import StrOutputParser

math_chain_text = math_chain | StrOutputParser()
general_chain_text = general_chain | StrOutputParser()

conditional_chain = RunnableBranch(
    (lambda x: route_question(x) == "math", math_chain_text),
    general_chain_text,
)
  1. Finally, run a few test inputs. Use one math question and one non-math question so you can confirm the routing logic is doing its job.
tests = [
    {"question": "What is 18 * 7?"},
    {"question": "Write a polite email asking for a meeting next week."},
]

for test in tests:
    result = conditional_chain.invoke(test)
    print(f"Q: {test['question']}")
    print(f"A: {result}\n")

Testing It

Run the script and check that arithmetic-style prompts go through the math prompt while normal requests go through the general prompt. If both answers look similar, your routing function is probably too naive or your keywords are too broad.

A better test is to print which branch was selected before invoking the chain. In production, you’d usually log that decision so you can trace bad routing decisions later.

Try edge cases like "What is 20 percent of 150?", "Explain division", and "Summarize this paragraph." If routing fails on those inputs, expand your keyword list or move from keyword matching to an LLM-based classifier.

Next Steps

  • Replace keyword routing with an LLM classifier that returns structured labels like billing, support, or sales.
  • Add more branches using RunnableBranch for tools like document search, calculator logic, or policy lookup.
  • Store routing decisions in logs so you can measure misroutes and tune prompts over time.

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides