Heads up... You’re accessing parts of this content for free, with some sections shown as
text.
Heads up... You’re accessing parts of this content for free, with some sections shown as
text.Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
Unlock now
State Demo
Open the empty state-demo.ipynb notebook in the Starter folder. You’ll make a graph that adds toppings to a pizza. First, define your State
class:
from typing import TypedDict
class PizzaState(TypedDict):
toppings: list[str]
quantity: int
def add_cheese(state):
quantity = state["quantity"] + 1
return {"toppings": ["cheese"], "quantity": quantity}
from langgraph.graph import StateGraph
graph = StateGraph(PizzaState)
graph.add_node("cheese", add_cheese)
graph.set_entry_point("cheese")
graph.set_finish_point("cheese")
app = graph.compile()
initial_state = {"toppings": [], "quantity": 0}
app.invoke(initial_state)
def add_meat(state):
quantity = state["quantity"] + 1
return {"toppings": ["meat"], "quantity": quantity}
graph.add_node("meat", add_meat)
graph.add_edge("cheese", "meat")
graph.set_finish_point("meat")
from typing import TypedDict, Annotated
from operator import add
class PizzaState(TypedDict):
toppings: Annotated[list[str], add]
quantity: int