Python / Python Modern Generative AI and Agents Interview Questions
What is LangGraph and how does it differ from LangChain's AgentExecutor for building agents?
LangGraph is a framework for building stateful, multi-step agents as directed graphs where each node is a function (LLM call, tool call, or logic) and edges define the flow of control. Unlike LangChain's AgentExecutor (a simple Thought-Action-Observation loop), LangGraph gives you explicit control over state transitions, conditional routing, cycles, parallelism, and human-in-the-loop checkpoints.
LangGraph excels at complex agent workflows: routers that choose different paths based on intent, agents that call multiple tools in parallel, agents that require human approval before taking irreversible actions, and systems where the same state graph runs across multiple user sessions (persistence via checkpointers).
# pip install langgraph
from langgraph.graph import StateGraph, END
from langgraph.prebuilt import ToolNode
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from typing import TypedDict, Annotated
import operator
# Define agent state
class AgentState(TypedDict):
messages: Annotated[list, operator.add] # appends each step
@tool
def search_web(query: str) -> str:
'''Search the web for current information.'''
return f'Search results for: {query}'
tools = [search_web]
model = ChatOpenAI(model='gpt-4o').bind_tools(tools)
def call_model(state: AgentState):
response = model.invoke(state['messages'])
return {'messages': [response]}
def should_continue(state: AgentState):
'''Route to tools or end based on whether LLM called a tool.'''
last = state['messages'][-1]
return 'tools' if last.tool_calls else END
# Build the graph
graph = StateGraph(AgentState)
graph.add_node('agent', call_model)
graph.add_node('tools', ToolNode(tools))
graph.set_entry_point('agent')
graph.add_conditional_edges('agent', should_continue)
graph.add_edge('tools', 'agent') # after tools, return to agent
app = graph.compile()
result = app.invoke({'messages': [{'role': 'user', 'content': 'What happened in AI news today?'}]})
print(result['messages'][-1].content)
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...
