Pregel class manages the runtime behavior for LangGraph applications. It combines actors (nodes) and channels into a single application following the Pregel algorithm (Bulk Synchronous Parallel model).
Defined in: langgraph/pregel/main.py:325
Overview
Prege l is the low-level execution engine that powers LangGraph. Most users will interact with Pregel indirectly through StateGraph or MessageGraph, which compile down to Pregel under the hood. For advanced use cases only. If you’re not sure whether you need to use Pregel directly, you probably don’t—use the Graph API instead.Execution Model
Prege l organizes execution into multiple steps, each consisting of three phases:- Plan: Determine which actors to execute in this step
- Execution: Execute all selected actors in parallel until completion, failure, or timeout
- Update: Update the channels with values written by the actors
Constructor
Parameters
dict[str, PregelNode | NodeBuilder]
required
Dictionary mapping node names to
PregelNode or NodeBuilder instances.dict[str, BaseChannel | ManagedValueSpec] | None
required
Dictionary mapping channel names to channel instances. Channels are used for communication between nodes.
str | Sequence[str]
required
Channel name(s) to use as input to the graph.
str | Sequence[str]
required
Channel name(s) to use as output from the graph.
StreamMode
default:"'values'"
Mode to stream output. Options:
'values', 'updates', 'checkpoints', 'tasks', 'debug', 'messages', 'custom'.str | Sequence[str] | None
default:"None"
Channels to stream. Defaults to all channels not in reserved channels.
bool
default:"False"
Whether to force emitting stream events eagerly. Automatically enabled for
'messages' and 'custom' stream modes.All | Sequence[str]
default:"()"
Node names to interrupt after. Use
'*' to interrupt after all nodes.All | Sequence[str]
default:"()"
Node names to interrupt before. Use
'*' to interrupt before all nodes.float | None
default:"None"
Maximum time to wait for a step to complete, in seconds.
bool | None
default:"None"
Whether to print debug information during execution.
Checkpointer
default:"None"
Checkpointer used to save and load graph state.
BaseStore | None
default:"None"
Memory store to use for SharedValues.
BaseCache | None
default:"None"
Cache to use for storing node results.
RetryPolicy | Sequence[RetryPolicy]
default:"()"
Retry policies to use when running tasks. Empty set disables retries.
CachePolicy | None
default:"None"
Cache policy to use for all nodes. Can be overridden by individual nodes.
type[ContextT] | None
default:"None"
Schema for the context object that will be passed to the workflow.
str
default:"'LangGraph'"
Name of the graph.
bool
default:"True"
Whether to automatically validate the graph structure on initialization.
Core Methods
invoke
Parameters
InputT
required
The input to the graph.
RunnableConfig | None
default:"None"
Configuration for the run, including thread_id for checkpointing.
StreamMode | list[StreamMode] | None
default:"None"
Override the graph’s default stream mode for this invocation.
Sequence[str] | None
default:"None"
Specific output keys to return.
All | Sequence[str] | None
default:"None"
Override interrupt_before_nodes for this invocation.
All | Sequence[str] | None
default:"None"
Override interrupt_after_nodes for this invocation.
bool | None
default:"None"
Override the debug flag for this invocation.
Returns
OutputT
The final output from the graph.
Usage Example
stream
Parameters
InputT
required
The input to the graph.
RunnableConfig | None
default:"None"
Configuration for the run.
StreamMode | list[StreamMode] | None
default:"None"
Override the graph’s default stream mode. Can specify multiple modes as a list.
bool
default:"False"
Whether to stream subgraph execution as well.
Returns
Iterator[Any]
Iterator yielding outputs based on the stream mode(s).
Usage Example
ainvoke
Parameters
Same asinvoke().
Returns
OutputT
The final output from the graph.
astream
Parameters
Same asstream().
Returns
AsyncIterator[Any]
Async iterator yielding outputs based on the stream mode(s).
get_graph
Parameters
RunnableConfig | None
default:"None"
Configuration for the graph.
int | bool
default:"False"
Whether to include subgraph details. If an integer, specifies the depth level.
Returns
Graph
A Graph object that can be visualized.
Usage Example
NodeBuilder
NodeBuilder provides a fluent API for building Pregel nodes.
Defined in: langgraph/pregel/main.py:161
Methods
subscribe_to
subscribe_only
read_from
do
write_to
meta
add_retry_policies
add_cache_policy
build
Usage Example
Channels
Channels are used to communicate between actors (nodes). LangGraph provides several built-in channel types:Basic Channels
- LastValue: Stores the last value sent to the channel
- Topic: A configurable PubSub Topic for sending multiple values
Advanced Channels
- Context: Exposes the value of a context manager
- BinaryOperatorAggregate: Stores a persistent value updated by applying a binary operator
Usage Example
Complete Example
See Also
- StateGraph - High-level graph builder (recommended)
- Checkpointing - State persistence concepts
- Types - Core type definitions