Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Langchain ai Langgraph Serde Types

From Leeroopedia
Knowledge Sources
Domains Serialization, Types
Last Updated 2026-02-11 16:00 GMT

Overview

Defines the `ChannelProtocol` and `SendProtocol` protocol classes along with sentinel string constants used for special checkpoint channel names throughout LangGraph's serialization layer.

Description

The Serde Types module provides lightweight protocol definitions and constants that form the type contract between LangGraph's serialization layer and its channel/execution system. The `ChannelProtocol` is a generic Protocol parameterized by `Value`, `Update`, and `C` (checkpoint type) that mirrors the `BaseChannel` abstract class, declaring the essential channel interface methods: `checkpoint()`, `from_checkpoint()`, `update()`, `get()`, `consume()`, along with `ValueType` and `UpdateType` properties. This protocol enables the serialization layer to work with channel objects without importing the full channel module, breaking circular dependencies.

The `SendProtocol` is a `runtime_checkable` Protocol that defines the contract for message-send objects in the Pregel execution engine. It requires `node` (target node name) and `arg` (the argument to send) attributes along with `__hash__`, `__repr__`, and `__eq__` methods, allowing send objects to be used in sets and as dictionary keys.

The module also defines five sentinel string constants: `ERROR` (`"__error__"`), `SCHEDULED` (`"__scheduled__"`), `INTERRUPT` (`"__interrupt__"`), `RESUME` (`"__resume__"`), and `TASKS` (`"__pregel_tasks"`). These are used as special channel names in checkpoint data to represent error states, scheduled operations, human-in-the-loop interrupts, resume signals, and task metadata respectively. They are prefixed with double underscores to avoid collision with user-defined channel names.

Usage

Use these protocols and constants when implementing custom serializers, checkpoint savers, or channel types that need to interact with LangGraph's internal channel and execution data. The sentinel constants should be referenced by name rather than by their string values to maintain forward compatibility if the values change.

Code Reference

Source Location

Signature

ERROR = "__error__"
SCHEDULED = "__scheduled__"
INTERRUPT = "__interrupt__"
RESUME = "__resume__"
TASKS = "__pregel_tasks"

class ChannelProtocol(Protocol[Value, Update, C]):
    @property
    def ValueType(self) -> Any: ...
    @property
    def UpdateType(self) -> Any: ...
    def checkpoint(self) -> C | None: ...
    def from_checkpoint(self, checkpoint: C | None) -> Self: ...
    def update(self, values: Sequence[Update]) -> bool: ...
    def get(self) -> Value: ...
    def consume(self) -> bool: ...

@runtime_checkable
class SendProtocol(Protocol):
    node: str
    arg: Any
    def __hash__(self) -> int: ...
    def __repr__(self) -> str: ...
    def __eq__(self, value: object) -> bool: ...

Import

from langgraph.checkpoint.serde.types import ChannelProtocol, SendProtocol
from langgraph.checkpoint.serde.types import ERROR, SCHEDULED, INTERRUPT, RESUME, TASKS

I/O Contract

Sentinel Constants

Constant Value Purpose
`ERROR` `"__error__"` Marks error state channels in checkpoint data
`SCHEDULED` `"__scheduled__"` Marks scheduled operation channels
`INTERRUPT` `"__interrupt__"` Marks human-in-the-loop interrupt channels
`RESUME` `"__resume__"` Marks resume signal channels
`TASKS` `"__pregel_tasks"` Marks task metadata channels

ChannelProtocol Methods

Method Input Output Description
`ValueType` (property) `Any` The type of value stored in the channel
`UpdateType` (property) `Any` The type of update accepted by the channel
`checkpoint` (none) None` Serializable snapshot of current state
`from_checkpoint` None` `Self` Restore channel from a checkpoint
`update` `values: Sequence[Update]` `bool` Apply updates; returns True if state changed
`get` (none) `Value` Get the current value
`consume` (none) `bool` Notify that a subscriber consumed the value

SendProtocol Attributes

Attribute Type Description
`node` `str` Target node name for the send operation
`arg` `Any` The argument/payload to send to the target node

Usage Examples

from langgraph.checkpoint.serde.types import (
    ChannelProtocol,
    SendProtocol,
    ERROR,
    INTERRUPT,
    TASKS,
)

# Use sentinel constants for special channel names
checkpoint_data = {
    "messages": [...],
    ERROR: None,
    INTERRUPT: [],
    TASKS: {},
}

# Check if an object implements SendProtocol
class MySend:
    def __init__(self, node: str, arg):
        self.node = node
        self.arg = arg
    def __hash__(self):
        return hash((self.node, str(self.arg)))
    def __repr__(self):
        return f"MySend({self.node}, {self.arg})"
    def __eq__(self, other):
        return isinstance(other, MySend) and self.node == other.node

send = MySend("agent", {"query": "hello"})
assert isinstance(send, SendProtocol)  # Runtime check passes

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment