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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
