By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A checkpointer keeps state longer than a deploy keeps a commit. The support graph's v2 schema renames order_ref to order_id, adds a retry_budget field and bumps schema_version to 2. In the checkpoint store, a thread is paused at the approval node with v1 state; other threads will replay from last week's snapshots. In this lab you write a pure migration, replay an old checkpoint, reject state a newer graph wrote, and decide what a graph-version rollback may and may not do.
There are two entry points into old state and they behave differently. A new run starts at START, so a migration node at the front of the graph sees the state the caller passed. A paused thread resumes at its pending node, not at START, so a migration node never runs before the resume. That asymmetry is the central fact of this lab: paused threads must be migrated before they are resumed, and the migration must be a pure function keyed by the version, not a side effect of one node's placement.
A migration takes one checkpoint's values and returns a new dict; it must be pure (no writes, no clocks, no network), idempotent (running it twice equals running it once) and keyed by the version it starts from. The dispatcher is the part that refuses to guess: state written by a newer graph than this code understands raises instead of being read with missing fields. The migration functions stay in the repository forever — they are part of the data contract, not release scaffolding.
from typing import Any
STATE_SCHEMA_VERSION = 2
def migrate_v1_to_v2(state: dict[str, Any]) -> dict[str, Any]:
"""Pure, idempotent upgrade of one checkpoint's channel values."""
if state.get("schema_version", 1) >= STATE_SCHEMA_VERSION:
return dict(state)
upgraded = dict(state)
upgraded["schema_version"] = STATE_SCHEMA_VERSION
upgraded.setdefault("retry_budget", 3)
upgraded["order_id"] = upgraded.pop("order_ref")
return upgraded
def migrate(state: dict[str, Any]) -> dict[str, Any]:
"""Dispatch by version; refuse state written by a newer graph."""
version = state.get("schema_version", 1)
if version > STATE_SCHEMA_VERSION:
raise ValueError(
f"checkpoint is schema v{version}; "
f"this graph reads v{STATE_SCHEMA_VERSION}"
)
if version == 1:
return migrate_v1_to_v2(state)
return dict(state)
old_checkpoint = {
"schema_version": 1,
"messages": [],
"order_ref": "A17",
"pending_approval": True,
}
migrated = migrate(old_checkpoint)
print(migrated["schema_version"])
print(migrated["order_id"])
print(migrated["retry_budget"])
print("order_ref" in migrated)The old field is gone, the new name carries the value, the missing optional field got its default and the version moved last. Note that the rename cannot be modelled as an additive change: old code reading new state would see both fields, and new code reading old state needs the migration before it reads anything. That asymmetry is why the dispatcher tests both directions.
Replaying from a stored checkpoint is how you prove the migration on real data without touching the production thread. Read the snapshot, branch if the version is old, apply the migration to the fork's state with update_state, then resume the fork. When nodes write to external services, replay against stand-ins; checkpoints are a record of state, not a sandbox around the vendor API.
from langgraph.types import Command
from schema_migration import STATE_SCHEMA_VERSION, migrate
from support_graph import build_graph
graph = build_graph()
config = {"configurable": {"thread_id": "thread-c91"}}
snapshot = graph.get_state(config)
# The v2 graph keeps the legacy order_ref channel while paused threads drain,
# so the old snapshot is still readable here.
print("pending:", snapshot.next)
print("stored version:", snapshot.values.get("schema_version", 1))
if snapshot.values.get("schema_version", 1) < STATE_SCHEMA_VERSION:
graph.update_state(config, migrate(dict(snapshot.values)))
resumed = graph.invoke(Command(resume="approved"), config)
print("resumed order:", resumed["order_id"])The order matters: read the version and the pending node first, migrate only what is old, then resume. If the migration were a node at START, this paused thread would skip it entirely and the pending node would read v1 state with v2 code — the most common checkpoint-migration bug.
A migration floor is safer than a best guess. If the stored version is newer than the code supports, the only correct behaviour is a clear error that names both versions: guessing the shape of future state corrupts data, and silently dropping unknown keys loses the information the next deploy needs. Tests should cover the upgrade, the idempotent second pass, the pass-through of current state and the rejection of a newer version.
import pytest
from schema_migration import (
STATE_SCHEMA_VERSION,
migrate,
migrate_v1_to_v2,
)
def test_v1_checkpoint_is_upgraded():
upgraded = migrate({"schema_version": 1, "order_ref": "A17"})
assert upgraded["schema_version"] == 2
assert upgraded["order_id"] == "A17"
assert "order_ref" not in upgraded
def test_migration_is_idempotent():
once = migrate_v1_to_v2({"schema_version": 1, "order_ref": "A17"})
assert migrate(once) == once
def test_newer_state_is_rejected_instead_of_guessed():
with pytest.raises(ValueError, match="reads v2"):
migrate({"schema_version": 3, "order_id": "A17"})
def test_current_checkpoints_pass_through_unchanged():
state = {"schema_version": STATE_SCHEMA_VERSION, "order_id": "A17"}
assert migrate(state) == stateA rollback is a code deployment, not a data deletion. Redeploying the previous graph version works only if the previous code can read the state written since — and after a rename or a type change it usually cannot. The plan is therefore decided before the deploy: drain or complete paused threads, keep readers tolerant of both shapes for a compatibility window, record a graph_version in state, and snapshot the checkpoint store before the release. Keep the original thread, its traces and committed old-snapshot fixtures; those are the evidence a rollback or a replay depends on.
| Change | Forward migration | Rollback safety |
|---|---|---|
| Add an optional field | Set a default in one helper; readers use .get | Safe: old readers ignore the new key |
| Rename a field | Copy the value to the new key, keep both while paused threads drain | Unsafe without dual readers; migrate before rollback |
| Change a field's type or meaning | Validate and convert per version; reject unknown shapes | Unsafe; old code would misinterpret new values |
| Change a reducer | Re-derive the channel or migrate it explicitly | Unsafe for pending writes; drain the affected threads |
schema_version from the first deploy and every schema change ships with a pure, idempotent migration function.Key takeaways
Checkpoints outlive code, so a state-schema change is a data migration: version the state, write a pure idempotent migration per upgrade, and keep those functions in the repository.
A paused thread resumes at its pending node, not at START; migrate paused checkpoints before they resume or the resume reads the old shape.
Replay is evidence, not rollback: fork the thread, migrate the fork, replay against stand-ins and keep the original thread and traces.
Reject state from a newer graph version with a clear error naming both versions instead of guessing at the future shape.
A graph-version rollback redeploys code and keeps checkpoints, traces and fixtures; it needs dual readers or drained threads when the schema changed.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.