Implementation:Langchain ai Langgraph BaseChannel
| Knowledge Sources | |
|---|---|
| Domains | Channels, Core |
| Last Updated | 2026-02-11 16:00 GMT |
Overview
The abstract base class for all LangGraph channels, defining the core interface for state storage, updates, checkpointing, and lifecycle management in the Pregel execution engine.
Description
The BaseChannel class is the foundational abstraction in LangGraph's channel system. It is parameterized by three generic type variables: `Value` (the type returned by `get()`), `Update` (the type accepted by `update()`), and `Checkpoint` (the type produced by `checkpoint()`). All concrete channel implementations, including `LastValue`, `AnyValue`, `BinOp`, `Topic`, `EphemeralValue`, and `NamedBarrierValue`, inherit from this class.
The abstract methods define the complete lifecycle of a channel: `get()` retrieves the current value (raising `EmptyChannelError` if unset), `update()` applies a sequence of updates produced by nodes during a step, `from_checkpoint()` reconstructs the channel from serialized state, and the abstract properties `ValueType` and `UpdateType` expose type metadata for runtime introspection. The non-abstract `checkpoint()` method provides a default implementation that delegates to `get()` and returns `MISSING` on `EmptyChannelError`, while subclasses can override this for custom checkpoint formats.
The class also provides lifecycle hooks: `consume()` notifies the channel that a subscribed task has read its value (used by barrier channels to reset), and `finish()` notifies the channel that the Pregel run is completing (used by `NamedBarrierValueAfterFinish` to gate final availability). Both hooks default to no-ops returning `False`. The `copy()` method provides a convenient way to clone a channel by round-tripping through `checkpoint()` and `from_checkpoint()`, though subclasses typically override this with more efficient implementations.
Usage
Use `BaseChannel` as the base class when implementing a custom channel type for LangGraph. All channels used in a `StateGraph` or `MessageGraph` must conform to this interface. The Pregel execution engine calls these methods in a specific order during each step: `update()` to apply node outputs, `get()` to read values for downstream nodes, `consume()` after subscriber execution, and `checkpoint()` at step boundaries for persistence.
Code Reference
Source Location
- Repository: Langchain_ai_Langgraph
- File: libs/langgraph/langgraph/channels/base.py
Signature
class BaseChannel(Generic[Value, Update, Checkpoint], ABC):
__slots__ = ("key", "typ")
def __init__(self, typ: Any, key: str = "") -> None: ...
@property
@abstractmethod
def ValueType(self) -> Any: ...
@property
@abstractmethod
def UpdateType(self) -> Any: ...
def copy(self) -> Self: ...
def checkpoint(self) -> Checkpoint | Any: ...
@abstractmethod
def from_checkpoint(self, checkpoint: Checkpoint | Any) -> Self: ...
@abstractmethod
def get(self) -> Value: ...
def is_available(self) -> bool: ...
@abstractmethod
def update(self, values: Sequence[Update]) -> bool: ...
def consume(self) -> bool: ...
def finish(self) -> bool: ...
Import
from langgraph.channels.base import BaseChannel
I/O Contract
Constructor Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| typ | `Any` | Yes | The type descriptor for the channel's value |
| key | `str` | No | Channel key name (default: `""`) |
Abstract Methods (must be implemented)
| Method | Input | Output | Description |
|---|---|---|---|
| `ValueType` | (property) | `Any` | Returns the type of value stored |
| `UpdateType` | (property) | `Any` | Returns the type of update accepted |
| `from_checkpoint` | Any` | `Self` | Creates a new channel from checkpoint data |
| `get` | (none) | `Value` | Returns current value; raises `EmptyChannelError` if empty |
| `update` | `values: Sequence[Update]` | `bool` | Applies updates; returns `True` if state changed |
Concrete Methods (may be overridden)
| Method | Default Behavior | Description |
|---|---|---|
| `copy` | Delegates to `checkpoint()` / `from_checkpoint()` | Returns a clone of the channel |
| `checkpoint` | Calls `get()`, returns `MISSING` on error | Serializable snapshot of state |
| `is_available` | Calls `get()`, catches `EmptyChannelError` | `True` if channel has a value |
| `consume` | Returns `False` | Hook for subscriber notification |
| `finish` | Returns `False` | Hook for run completion notification |
Usage Examples
from langgraph.channels.base import BaseChannel, Value
from langgraph.errors import EmptyChannelError
from collections.abc import Sequence
from typing import Generic, Any
from typing_extensions import Self
class CounterChannel(Generic[Value], BaseChannel[int, int, int]):
"""A custom channel that sums all updates."""
def __init__(self, typ: Any = int, key: str = "") -> None:
super().__init__(typ, key)
self._total = 0
self._has_value = False
@property
def ValueType(self) -> type:
return int
@property
def UpdateType(self) -> type:
return int
def from_checkpoint(self, checkpoint: int) -> Self:
new = self.__class__(self.typ, self.key)
if checkpoint is not None:
new._total = checkpoint
new._has_value = True
return new
def get(self) -> int:
if not self._has_value:
raise EmptyChannelError()
return self._total
def update(self, values: Sequence[int]) -> bool:
if not values:
return False
self._total += sum(values)
self._has_value = True
return True
# Use the custom channel
ch = CounterChannel()
ch.update([1, 2, 3])
print(ch.get()) # 6
ch.update([4])
print(ch.get()) # 10