AI / LangGraph LangChain Interview questions
What is StateGraph in LangGraph?
StateGraph is the main graph class in LangGraph. You instantiate it with a state type (a TypedDict class), add nodes and edges to it, then compile it into an executable app. The state type defines all the fields that are shared across nodes and how those fields are updated when a node returns a partial update.
from typing import TypedDict, Annotated
from langgraph.graph import StateGraph, START, END
import operator
# Define the shared state structure
class AgentState(TypedDict):
messages: Annotated[list, operator.add] # append-reducer
steps_taken: int
# Build the graph
graph_builder = StateGraph(AgentState)
def call_llm(state: AgentState) -> dict:
response = llm.invoke(state["messages"])
return {"messages": [response], "steps_taken": state["steps_taken"] + 1}
graph_builder.add_node("llm", call_llm)
graph_builder.add_edge(START, "llm")
graph_builder.add_edge("llm", END)
# Compile to executable
graph = graph_builder.compile()
State updates use reducers. The default reducer is last-write-wins (the node's returned value replaces the current value). Using Annotated[list, operator.add] means returned lists are appended to the existing list — the standard pattern for message history in chat agents.
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...
