AI Hints
Home›LangGraph Tutorial›Inspecting and Editing State
LangGraph · Lesson 43 of 120

Reading State

Get_state.

⏱ 2 min read▶ Video at 45:18🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Once state is saved, you can look at it, any time, without running the graph. Get state returns a snapshot of a thread. Its values, and what would run next.

Reading State
From the video: Reading State
  • Look without running
  • get_state
  • Values and next

A snapshot of a thread

We run a small order pipeline on a thread, using a helper file. Then get state returns a snapshot. Values holds the saved state. Next holds the nodes waiting to run, which is empty, because the run finished. Run it. We see the saved order, and an empty next.

get_state.pypython
from order_graph import graph

config = {"configurable": {"thread_id": "order-1"}}
graph.invoke({"title": "dune", "quantity": 2}, config)

snapshot = graph.get_state(config)
print("values:", snapshot.values)
print("next:", snapshot.next)
Output
values: {'title': 'Dune', 'quantity': 2, 'total': 19.98, 'receipt': '2 x Dune = £19.98'}
next: ()
Also uses order_graph.py
from typing import TypedDict
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import StateGraph, START, END

PRICES = {"Dune": 9.99, "Circe": 10.99}

class Order(TypedDict):
    title: str
    quantity: int
    total: float
    receipt: str

def clean(s):
    return {"title": s["title"].strip().title()}
def price(s):
    return {"total": round(PRICES[s["title"]] * s["quantity"], 2)}
def receipt(s):
    return {"receipt": f"{s['quantity']} x {s['title']} = £{s['total']}"}

b = StateGraph(Order)
b.add_sequence([clean, price, receipt])
b.add_edge(START, "clean"); b.add_edge("receipt", END)
graph = b.compile(checkpointer=InMemorySaver())

Uses for get_state

Get state is handy in apps, and when debugging. Show a customer their saved basket. Check where a paused graph is waiting. Or load a conversation's messages to display them, when a customer returns.

keyvalue
valuesthe saved state
nextwhat runs next
Paused graphssee where
Appsshow saved data

Summary

Get state returns a snapshot of a thread, with its values and its next nodes. Next, changing saved state.

Key takeaways
  • graph.get_state(config)
  • snapshot.values
  • snapshot.next
Next up: Editing State — update_state