Skip to main content
The 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:
  1. Plan: Determine which actors to execute in this step
  2. Execution: Execute all selected actors in parallel until completion, failure, or timeout
  3. Update: Update the channels with values written by the actors
This process repeats until no actors are selected for execution or a maximum number of steps is reached.

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

Synchronously invoke the graph and return the final output.

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

Synchronously stream graph execution, yielding outputs as they become available.

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

Asynchronously invoke the graph and return the final output.

Parameters

Same as invoke().

Returns

OutputT
The final output from the graph.

astream

Asynchronously stream graph execution.

Parameters

Same as stream().

Returns

AsyncIterator[Any]
Async iterator yielding outputs based on the stream mode(s).

get_graph

Return a drawable representation of the computation 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

Add channels to subscribe to. Node will be invoked when any of these channels are updated.

subscribe_only

Subscribe to a single channel only.

read_from

Adds channels to read from without subscribing to them.

do

Adds the specified node/runnable to execute.

write_to

Add channel writes.

meta

Add tags or metadata to the node.

add_retry_policies

Adds retry policies to the node.

add_cache_policy

Adds cache policy to the node.

build

Builds and returns the PregelNode.

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