From bf2147bd96fc661320261a58320fd2eddd1ddc3e Mon Sep 17 00:00:00 2001 From: Sydney Runkle Date: Wed, 29 Apr 2026 09:28:13 -0400 Subject: [PATCH] fix(_checkpoint): remove assert, use cast for DeltaChannel narrowing _needs_replay already gates on isinstance(spec, DeltaChannel), so the assert/isinstance checks inside the replay branch were unreachable. Replace with cast(DeltaChannel, spec) for zero-cost type narrowing that survives -O and avoids any runtime check. Co-Authored-By: Claude Sonnet 4.6 (1M context) --- libs/langgraph/langgraph/pregel/_checkpoint.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/libs/langgraph/langgraph/pregel/_checkpoint.py b/libs/langgraph/langgraph/pregel/_checkpoint.py index b58dddfa2..41493f999 100644 --- a/libs/langgraph/langgraph/pregel/_checkpoint.py +++ b/libs/langgraph/langgraph/pregel/_checkpoint.py @@ -2,7 +2,7 @@ from __future__ import annotations from collections.abc import Callable, Mapping from datetime import datetime, timezone -from typing import Any +from typing import Any, cast from langchain_core.runnables import RunnableConfig from langgraph.checkpoint.base import DELTA_SENTINEL, BaseCheckpointSaver, Checkpoint @@ -126,12 +126,9 @@ def channels_from_checkpoint( ch: BaseChannel stored = checkpoint["channel_values"].get(k, MISSING) if _needs_replay(spec, stored) and saver is not None and config is not None: - if not isinstance(spec, DeltaChannel): - raise TypeError( - f"Expected DeltaChannel for channel requiring replay, got {type(spec)}" - ) + delta_spec = cast(DeltaChannel, spec) history = saver._get_channel_writes_history(config, k) - replay_ch = spec.from_checkpoint(history.seed) + replay_ch = delta_spec.from_checkpoint(history.seed) replay_ch.replay_writes(history.writes) ch = replay_ch else: @@ -161,12 +158,9 @@ async def achannels_from_checkpoint( ch: BaseChannel stored = checkpoint["channel_values"].get(k, MISSING) if _needs_replay(spec, stored) and saver is not None and config is not None: - if not isinstance(spec, DeltaChannel): - raise TypeError( - f"Expected DeltaChannel for channel requiring replay, got {type(spec)}" - ) + delta_spec = cast(DeltaChannel, spec) history = await saver._aget_channel_writes_history(config, k) - replay_ch = spec.from_checkpoint(history.seed) + replay_ch = delta_spec.from_checkpoint(history.seed) replay_ch.replay_writes(history.writes) ch = replay_ch else: