"""
Let’s see what checkpoints are saved when a simple graph is invoked as follows:
让我们看看在调用简单图时保存了哪些检查点,具体如下:
"""
from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.memory import InMemorySaver
from langchain_core.runnables import RunnableConfig
from typing import Annotated
from typing_extensions import TypedDict
from operator import add
class State(TypedDict):
foo: str
bar: Annotated[list[str], add]
def node_a(state: State):
return {"foo": "a", "bar": ["a"]}
def node_b(state: State):
return {"foo": "b", "bar": ["b"]}
workflow = StateGraph(State)
workflow.add_node("node_a", node_a)
workflow.add_node("node_b", node_b)
# 连线逻辑:START -> A -> B -> END
workflow.add_edge(START, "node_a")
workflow.add_edge("node_a", "node_b")
workflow.add_edge("node_b", END)
checkpointer = InMemorySaver()
graph = workflow.compile(checkpointer=checkpointer)
config: RunnableConfig = {"configurable": {"thread_id": "1"}}
res = graph.invoke({"foo": "", "bar":[]}, config)
print(res)
# get the latest state snapshot
config = {"configurable": {"thread_id": "1"}}
st_01 = graph.get_state(config)
print(st_01)
# get a state snapshot for a specific checkpoint_id
config = {"configurable": {"thread_id": "1", "checkpoint_id": "1ef663ba-28fe-6528-8002-5a559208592c"}}
st_02 = graph.get_state(config)
print(st_02)
print(f"------------------------------------")
st_03 = graph.get_state_history({"configurable": {"thread_id": "1"}})
for i in st_03:
print(i)
{'foo': 'b', 'bar': ['a', 'b']}
StateSnapshot(values={'foo': 'b', 'bar': ['a', 'b']}, next=(), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f0f7b6c-b09f-63aa-8002-d006d3693fd0'}}, metadata={'source': 'loop', 'step': 2, 'parents': {}}, created_at='2026-01-22T17:21:31.102094+00:00', parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f0f7b6c-b09e-6bda-8001-ea168d3ee082'}}, tasks=(), interrupts=())
StateSnapshot(values={}, next=(), config={'configurable': {'thread_id': '1', 'checkpoint_id': '1ef663ba-28fe-6528-8002-5a559208592c'}}, metadata=None, created_at=None, parent_config=None, tasks=(), interrupts=())
------------------------------------
StateSnapshot(values={'foo': 'b', 'bar': ['a', 'b']}, next=(), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f0f7b6c-b09f-63aa-8002-d006d3693fd0'}}, metadata={'source': 'loop', 'step': 2, 'parents': {}}, created_at='2026-01-22T17:21:31.102094+00:00', parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f0f7b6c-b09e-6bda-8001-ea168d3ee082'}}, tasks=(), interrupts=())
StateSnapshot(values={'foo': 'a', 'bar': ['a']}, next=('node_b',), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f0f7b6c-b09e-6bda-8001-ea168d3ee082'}}, metadata={'source': 'loop', 'step': 1, 'parents': {}}, created_at='2026-01-22T17:21:31.101893+00:00', parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f0f7b6c-b09e-60cc-8000-08cb7c6d0ea6'}}, tasks=(PregelTask(id='e6d414a3-d5d8-56f5-747b-62e49aa74558', name='node_b', path=('__pregel_pull', 'node_b'), error=None, interrupts=(), state=None, result={'foo': 'b', 'bar': ['b']}),), interrupts=())
StateSnapshot(values={'foo': '', 'bar': []}, next=('node_a',), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f0f7b6c-b09e-60cc-8000-08cb7c6d0ea6'}}, metadata={'source': 'loop', 'step': 0, 'parents': {}}, created_at='2026-01-22T17:21:31.101610+00:00', parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f0f7b6c-b09d-669a-bfff-991ccdebeb74'}}, tasks=(PregelTask(id='1d229589-e137-b394-c906-ed424a352fa9', name='node_a', path=('__pregel_pull', 'node_a'), error=None, interrupts=(), state=None, result={'foo': 'a', 'bar': ['a']}),), interrupts=())
StateSnapshot(values={'bar': []}, next=('__start__',), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1f0f7b6c-b09d-669a-bfff-991ccdebeb74'}}, metadata={'source': 'input', 'step': -1, 'parents': {}}, created_at='2026-01-22T17:21:31.101349+00:00', parent_config=None, tasks=(PregelTask(id='ce0bbaf9-2760-67c9-f7c5-128b2c019ecc', name='__start__', path=('__pregel_pull', '__start__'), error=None, interrupts=(), state=None, result={'foo': '', 'bar': []}),), interrupts=())
(langchain_py312)