In this video, we dive deep into LangGraph Conditional Edges to build a high-performance Smart Routing Mechanism. Routing is a core concept in Agentic workflows. Instead of sending every request to the same node, we implement a "Router" that intelligently classifies user intent—like distinguishing between an HR query (leaves/holidays) and an Accounts query (salary/payments)—and directs the flow accordingly. What you will learn in this tutorial: Setting up LangGraph: Building a stateful graph with nodes and edges. Pydantic Structured Output: How to force an LLM (GPT-4o Mini) to return a strictly typed schema for reliable routing. Conditional Edges: Implementing logic to branch your workflow based on the LLM's decision. State Management: Passing information seamlessly across different modules. Technical Stack: Framework: LangGraph / LangChain LLM: GPT-4o Mini (OpenAI) Data Validation: Pydantic (BaseModel) Environment: VS Code & Python Timestamps: 0:00 - Introduction to Routing Scenarios 0:50 - Use Case: HR vs. Accounts Department 2:15 - Project Setup & API Configurations 3:45 - Defining the Pydantic Router Schema 5:30 - Creating Nodes (HR, Accounts, and Router Check) 8:10 - Implementing add_conditional_edges 11:00 - Testing the Smart Router with Real Queries 12:45 - Conclusion & GitHub Repository Info Resources: GitHub Repository: https://github.com/NajiAboo/langgraph-tutorials/blob/main/conditional_edge_router.ipynb If you found this helpful, please Like, Subscribe, and hit the notification bell for more deep dives into Agentic AI! Hashtags #LangGraph, #LangChain, #AgenticAI, #AIAgents, #PythonProgramming, #GenerativeAI, #OpenAI, #GPT4oMini, #Pydantic, #AIWorkflow, #MachineLearning, #SmartRouting, #MohamedNajiAboo
Full Transcript
Hi everyone, welcome back. So, we have explained the basics of LangGraph conditional edges. And in this video, we will be exploring more on the conditional edges. Now, think about a scenario. Whenever we are raising a request in our company, like applying for a leave or asking for a salary details, does it go to the same department? No, right? If it is related to leave or holidays, it goes to HR team. If it is about salary or payment, it goes to accounts department. So, basically, there is a decision happening in between that decides where your request should go. Your request should go. So, this is what what is exactly happening in the conditional edges. So, in this video, we will be implement this smart router mechanism using conditional edges. Okay. So, what we are going to achieve in this video or we are going to implement this, we will have a scenario in which somebody will ask some questions. Maybe the people will ask about the holidays or it will be ask about the salary. And we will have a router check node will be there. So, this router check will will have a a chat or LLM will be there. So, it will decide the type or the category of this question. So, based on this type or the category, it will decide it is HR or the accounts. So, based on that, it will go to the HR module and it will process the HR request or it will go to the accounts module. So, we will implement this use case using LangGraph conditional edges. So, let me open my Visual Studio Code. Okay. So, we can import some basic imports. So, first, we need to import the .env. I can import the load_dotenv. And let's do this. And this will be used to load the environment variables. So, basically, we have environment variable which contain the OpenAI key. Now, from the LangChain, from LangChain OpenAI, we can import the chat OpenAI. Now, let's define a model. Uh we we can have a chat. And the model will be the we will be using GPT 4o mini. 4o mini the model that we are going to use. Now, let's make a system prompt. Okay. So, what is our system prompt? I can say that you are a helpful router selector based on the context, you have to select the route as HR or accounts. accounts. Any HR related should go to HR or any accounts should go to accounts. And then, I can have some examples. Um How many leaves are available for me? And this will be the HR. I can say uh Give me my salary details. So, this will go to the accounts. I'm just giving some of the examples. Okay. And which is always good to update our prompt or modify prompt using what we call them uh ChatGPT or any other LLM tools which will give a more optimized prompt. Okay. So, this is very simple prompt I have given. Okay. No guardrails or nothing is there. Now, we can import uh typedic can be imported. As I mentioned, this will be uh we can use signal the functions. And from Pydantic, I can import the base model. And also, I can import the field. Okay. So, what we are going to do? So, we will we will have a LLM. Okay. So, LLM will be we will invoke the model and we will invoke in a structured model. Okay. So, for that, we need to have a Pydantic model. So, let me have a Pydantic model. I can say router uh service schema. And base model. Okay. So, we need three parameters. One is the router path, I can say. And router path is a literal which contains HR or accounts. Okay. So, let's say that this is a field. This is a field. I can give the description as very simple description. Uh the uh destination department. Okay. Um Now, okay, I can say confidence. This will be float. Here, the field, not file extension. The field, we can give the description. Can say confidence score between zero and one. The next is a description or we can say it as a reasoning also, but let put it as a description. The field, the description is I can say reasoning for the routing choice. Okay. So, what this means Okay, if you are saying the HR or it is saying about accounts, okay, why it is selected as HR or why it selects as a why it selects as a accounts. Okay. So, that kind of reasoning we will have in this description. Okay. Now, we can import the start and from start and end from the LangGraph. So, we can take LangGraph.graph, we can import uh StateGraph and we can import the start and we can import the end. Now, let's define a state. Okay. The state is the as I mentioned, state is the thing that um that will be shared among all the nodes. Okay. So, let's have a class state. Simply state, I can have a typedic. Um Here, we will have the input. The input is the one that is passed by the user. Then, I can have a router path. Router path is the one that is um decided by the LLM. Here, we have a HR. Or here, we have a accounts. Okay. Uh now, confidence. Confidence score, which is also from LLM. Now, description or the reason. Can have the details. Then, the final result will be in the result. Okay. So, we got our state is also ready. Now, let's build the nodes. So, first, we can build a HR node. HR node. Then, HR node, we have a input. The state. This is the input that is entered by the user. User. Okay. Here, I am saying print. Uh I can say maybe uh description is description. State dot description. Then, confidence. I can say state confidence. Okay. Now, let's return a dictionary. I can send it as a uh router path. I can say that result. So, path is router. Here, we can say that that you Your answer is from HR department. Okay. Um Okay, I'm just checking this. So, this is our HR module or HR node. So, what I'm doing I'm just taking the input. I'm not printing the input anymore. Okay, that is fine. And based on this, I am checking like I have a description is there. I'm printing the description and confidence score is printing. And finally, I'm sending a response said, "Okay, your um your answer is from HR department." Similar to this, I will have a accounts. I'm just copy and paste. Accounts will be there. And similar thing, I'm just changing the accounts. Okay. So, accounts is also ready. Now, I can create a check node. Okay. So, let's do a router check node. Here, I can do router check. Check, which is of state. What I need to do, I need to take a input. The state input. Okay. Now, I'm going to use a structured LLM. Structured LLM, I can use model.with_structured_llm. Uh structured output. Here, I can pass the structure. Okay. The structure will be the router service schema. Okay. So, what it what it means is uh so, LLM generate a non-structured unstructured response that will be converted to this format, the router service format. Okay. Now, let's have a response. So, response will be I can have a uh structured LLM.invoke. Okay. Now, I can pass First, I can pass uh system prompt. System. Which will be the system prompt. Now I can pass the user. Let's have the user. Or human. Here we can pass the input. Let me see, something is wrong here. We have the invoke. Uh, System prompt. Something is missing. It is showing some errors. So we have the Why it is showing something like this? This is closed. This is also closed. Let me see. It's also closed. Okay. So maybe we can execute it we see. System. Okay. It should be comma. Now we are good. Okay. Now let me return. What all things I need to return? I need to return the router path. So here the route path will be uh, response. dot router path. Now I need to have a description. And description will be uh, response. description. Okay. Now I need to have a confidence. It will be response. confidence. Okay. So first we will have a router will be there. So the router will check the input and it will pass the necessary details to the LLM model. And it will decide where it has to go. So this is our first node. Okay. So once we have the first node, okay, then this will go to the our conditional check node. Okay. So let me have the conditional check node also then it will be like more clear. Okay. So let me have a conditional check. State. And here we need to have a decorator. We need to we will have a HR. Or we will have a accounts. I'm taking router path. State. Router path. So if router path if router path equal to equal to HR, return HR. Okay. Otherwise, return accounts. Okay. Now we are ready with all our nodes. So first we will have a router check will be there. So the router check will be there. So the router check will get the input from the user and we will convert it into the structured LLM response and we will update our state with router path and everything. Then it will come to the conditional check and it will check based on our router path it will go to the HR department or it will go to the accounts node. Okay. Now the basic details are ready. Now we can start building our our graph. Okay. So for that we will have a builder. Uh, state graph. We need to pass pass our state. So state is what we are going to pass. Now builder. node. add node. I can add a check node. Uh, not check node. Here a router check. Router check. Here I am passing router check. Now I can add node. Another node is the HR node. Then pass HR node can be passed. Then we can pass add node. Let's pass accounts. Here we will have a accounts. Okay. Now the nodes is done. Now let's connect these nodes. Okay. We can do add edge. The first will do will be the start. From the start it will go to the router check. Okay. From the router check it is actually a conditional edge, right? So it will go to the router check. It will go to the conditional check. From that it will go to the HR. And from HR it will go to end. Not end key. End. Okay. Now we will have a builder. add edge. Here I will have a accounts. Then it will go to the end. So we are ready with the node. Now combine it. We can use the builder. combine. Builder. combine. This will combine. Okay. Let's visualize the graph. Okay. See. So this is our graph. So we are starting. We are giving some input and we will have a router check will be there. The based on the router check it will go to the accounts department or it will go to the HR department. Finally, it will go to end. Okay. So this is good. Now we can start. Now let me uh, restart this. Let's do a run all command. Okay. Now I can graph. invoke. Invoke. We can do a invoke. Uh, here here I am passing a message uh, as a input, right? Here I can pass input. I can say give me my salary details. So it must say this is from the HR department. You can see let's see how this works. So we can see this is our input and this is the router path. It is accounts and confidence and it says about description. The router is seeking information related to salary which falls under the accounts. The result is your answer is from the accounts department. So it is correctly identified and it is routed to the exact node. Now similar to this I can give another one. Uh, how many give me my sick leave details. So let's do this. So it is coming under HR and it is ac- correctly identified as a HR department. Okay. So in this way we can easily build a check a router check or router um, router check using the conditional edges. This is very important thing and this can be implemented or this will be applied to in our different use cases. Okay. So that is all about in this video. I will be uploading this into the my GitHub repo. You guys can download and play it out. So that's it. Please do like and subscribe. Have a nice day. Bye-bye.
Original Description
In this video, we dive deep into LangGraph Conditional Edges to build a high-performance Smart Routing Mechanism.
Routing is a core concept in Agentic workflows. Instead of sending every request to the same node, we implement a "Router" that intelligently classifies user intent—like distinguishing between an HR query (leaves/holidays) and an Accounts query (salary/payments)—and directs the flow accordingly.
What you will learn in this tutorial:
Setting up LangGraph: Building a stateful graph with nodes and edges.
Pydantic Structured Output: How to force an LLM (GPT-4o Mini) to return a strictly typed schema for reliable routing.
Conditional Edges: Implementing logic to branch your workflow based on the LLM's decision.
State Management: Passing information seamlessly across different modules.
Technical Stack:
Framework: LangGraph / LangChain
LLM: GPT-4o Mini (OpenAI)
Data Validation: Pydantic (BaseModel)
Environment: VS Code & Python
Timestamps:
0:00 - Introduction to Routing Scenarios
0:50 - Use Case: HR vs. Accounts Department
2:15 - Project Setup & API Configurations
3:45 - Defining the Pydantic Router Schema
5:30 - Creating Nodes (HR, Accounts, and Router Check)
8:10 - Implementing add_conditional_edges
11:00 - Testing the Smart Router with Real Queries
12:45 - Conclusion & GitHub Repository Info
Resources:
GitHub Repository: https://github.com/NajiAboo/langgraph-tutorials/blob/main/conditional_edge_router.ipynb
If you found this helpful, please Like, Subscribe, and hit the notification bell for more deep dives into Agentic AI!
Hashtags
#LangGraph, #LangChain, #AgenticAI, #AIAgents, #PythonProgramming, #GenerativeAI, #OpenAI, #GPT4oMini, #Pydantic, #AIWorkflow, #MachineLearning, #SmartRouting, #MohamedNajiAboo