AI / LangGraph LangChain Interview questions
How do you implement conditional edges in LangGraph?
Conditional edges implement branching logic in LangGraph. A router function takes the current state and returns a string key. That key is looked up in a mapping dict to determine which node to execute next.
from langgraph.graph import StateGraph, START, END def should_continue(state: AgentState) -> str: """Decide whether to call a tool or end.""" last_message = state["messages"][-1] if last_message.tool_calls: # LLM wants to call a tool return "call_tool" return "end" # LLM produced a final answer graph.add_conditional_edges( "agent", # source node should_continue, # router function { "call_tool": "tool_executor", # route to tool executor "end": END, # or finish } )
The router function can return any string; the mapping dict translates those strings to actual node names or END. If all possible return values are listed in the mapping, you can omit the mapping dict and the router function can return node names directly. Conditional edges are the mechanism that creates cycles in a LangGraph — the agent node routes back to the tool executor, which routes back to the agent, until the agent routes to END.
More Related questions...