LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • Graphs
  • Functional API
  • Pregel
  • Checkpointing
  • Storage
  • Caching
  • Types
  • Runtime
  • Config
  • Errors
  • Constants
  • Channels
  • Agents
LangGraph CLI
LangGraph SDK
LangGraph Supervisor
LangGraph Swarm
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

OverviewGraphsFunctional APIPregelCheckpointingStorageCachingTypesRuntimeConfigErrorsConstantsChannelsAgents
LangGraph CLI
LangGraph SDK
LangGraph Supervisor
LangGraph Swarm
Language
Theme
Pythonlanggraphpregelmain
Module●Since v0.6

main

Attributes

attribute
CACHE_NS_WRITES
attribute
CONF
attribute
CONFIG_KEY_CACHE
attribute
CONFIG_KEY_CHECKPOINT_ID
attribute
CONFIG_KEY_CHECKPOINT_NS
attribute
CONFIG_KEY_CHECKPOINTER
attribute
CONFIG_KEY_DURABILITY
attribute
CONFIG_KEY_NODE_FINISHED
attribute
CONFIG_KEY_READ
attribute
CONFIG_KEY_RUNNER_SUBMIT
attribute
CONFIG_KEY_RUNTIME
attribute
CONFIG_KEY_SEND
attribute
CONFIG_KEY_STREAM
attribute
CONFIG_KEY_TASK_ID
attribute
CONFIG_KEY_THREAD_ID
attribute
ERROR
attribute
INPUT
attribute
INTERRUPT
attribute
NS_END
attribute
NS_SEP
attribute
NULL_TASK_ID
attribute
PUSH
attribute
TASKS
attribute
MISSING

Unset sentinel value.

attribute
END

The last (maybe virtual) node in graph-style Pregel.

attribute
ManagedValueSpec: type[ManagedValue]
attribute
DEFAULT_BOUND
attribute
StreamChunk: tuple[tuple[str, ...], str, Any]
attribute
DEFAULT_RUNTIME
attribute
All: Literal['*']

Special value to indicate that graph should interrupt on all nodes.

attribute
Durability: Literal['sync', 'async', 'exit']

Durability mode for the graph execution.

  • 'sync': Changes are persisted synchronously before the next step starts.
  • 'async': Changes are persisted asynchronously while the next step executes.
  • 'exit': Changes are persisted only when the graph exits.
attribute
StreamMode: Literal['values', 'updates', 'checkpoints', 'tasks', 'debug', 'messages', 'custom']

How the stream method should emit outputs.

  • "values": Emit all values in the state after each step, including interrupts. When used with functional API, values are emitted once at the end of the workflow.
  • "updates": Emit only the node or task names and updates returned by the nodes or tasks after each step. If multiple updates are made in the same step (e.g. multiple nodes are run) then those updates are emitted separately.
  • "custom": Emit custom data using from inside nodes or tasks using StreamWriter.
  • "messages": Emit LLM messages token-by-token together with metadata for any LLM invocations inside nodes or tasks.
  • "checkpoints": Emit an event when a checkpoint is created, in the same format as returned by get_state().
  • "tasks": Emit events when tasks start and finish, including their results and errors.
  • "debug": Emit "checkpoints" and "tasks" events for debugging purposes.
attribute
ContextT

Type variable used to represent graph run scoped context.

Defaults to None.

attribute
InputT

Type variable used to represent the input to a StateGraph.

Defaults to StateT.

attribute
OutputT

Type variable used to represent the output of a StateGraph.

Defaults to StateT.

attribute
StateT

Type variable used to represent the state in a graph.

Functions

function
ensure_config

Return a config with all keys, merging any provided configs.

function
merge_configs

Merge multiple configs into one.

function
patch_checkpoint_map
function
patch_config

Patch a config with new values.

function
patch_configurable
function
recast_checkpoint_ns

Remove task IDs from checkpoint namespace.

function
create_model

Create a pydantic model with the given field definitions.

function
coerce_to_runnable

Coerce a runnable-like object into a Runnable.

function
get_config
function
create_error_message
function
apply_writes

Apply writes from a set of tasks (usually the tasks from a Pregel step) to the checkpoint and channels, and return managed values writes to be applied externally.

function
local_read

Function injected under CONFIG_KEY_READ in task config, to read current state. Used by conditional edges to read a copy of the state with reflecting the writes from that node only.

function
prepare_next_tasks

Prepare the set of tasks that will make up the next Pregel step.

function
identifier

Return the module and name of an object.

function
channels_from_checkpoint

Get channels from a checkpoint.

function
copy_checkpoint
function
create_checkpoint

Create a checkpoint for the given channels.

function
empty_checkpoint
function
draw_graph

Get the graph for this Pregel instance.

function
map_input

Map input chunk to a sequence of pending writes in the form (channel, value).

function
read_channels
function
get_new_channel_versions

Get subset of current_versions that are newer than previous_versions.

function
validate_graph
function
validate_keys
function
get_bolded_text

Get bolded text.

function
get_colored_text

Get colored text.

function
tasks_w_writes

Apply writes / subgraph states to tasks to be returned in a StateSnapshot.

function
ensure_valid_checkpointer

Classes

class
AsyncQueue

Async unbounded FIFO queue with a wait() method.

Subclassed from asyncio.Queue, adding a wait() method.

class
SyncQueue

Unbounded FIFO queue with a wait() method. Adapted from pure Python implementation of queue.SimpleQueue.

class
RunnableSeq

Sequence of Runnable, where the output of each is the input of the next.

RunnableSeq is a simpler version of RunnableSequence that is internal to LangGraph.

class
DeprecatedKwargs

TypedDict to use for extra keyword arguments, enabling type checking warnings for deprecated arguments.

class
BaseChannel

Base class for all channels.

class
Topic

A configurable PubSub Topic.

class
ErrorCode
class
GraphRecursionError

Raised when the graph has exhausted the maximum number of steps.

This prevents infinite loops. To increase the maximum number of steps, run your graph with a config specifying a higher recursion_limit.

Troubleshooting guides:

  • GRAPH_RECURSION_LIMIT

Examples:

graph = builder.compile()
graph.invoke(
    {"messages": [("user", "Hello, world!")]},
    # The config is the second positional argument
    {"recursion_limit": 1000},
)
class
InvalidUpdateError

Raised when attempting to update a channel with an invalid set of updates.

Troubleshooting guides:

  • INVALID_CONCURRENT_GRAPH_UPDATE
  • INVALID_GRAPH_NODE_RETURN_VALUE
class
PregelTaskWrites

Simplest implementation of WritesProtocol, for usage with writes that don't originate from a runnable task, eg. graph input, update_state, etc.

class
AsyncPregelLoop
class
SyncPregelLoop
class
StreamMessagesHandler

A callback handler that implements stream_mode=messages.

Collects messages from: (1) chat model stream events; and (2) node outputs.

class
PregelNode

A node in a Pregel graph. This won't be invoked as a runnable by the graph itself, but instead acts as a container for the components necessary to make a PregelExecutableTask for a node.

class
RetryPolicy

Configuration for retrying nodes.

class
PregelRunner

Responsible for executing a set of Pregel tasks concurrently, committing their writes, yielding control to caller when there is output to emit, and interrupting other tasks if appropriate.

class
ChannelWrite

Implements the logic for sending writes to CONFIG_KEY_SEND. Can be used as a runnable or as a static method to call imperatively.

class
ChannelWriteEntry
class
PregelProtocol
class
StreamProtocol
class
Runtime

Convenience class that bundles run-scoped context and other runtime utilities.

This class is injected into graph nodes and middleware. It provides access to context, store, stream_writer, and previous.

Accessing config

Runtime does not include config. To access RunnableConfig, you can inject it directly by adding a config: RunnableConfig parameter to your node function (recommended), or use get_config() from langgraph.config.

Note

ToolRuntime (from langgraph.prebuilt) is a subclass that provides similar functionality but is designed specifically for tools. It shares context, store, and stream_writer with Runtime, and adds tool-specific attributes like config, state, and tool_call_id.

Example:

from typing import TypedDict
from langgraph.graph import StateGraph
from dataclasses import dataclass
from langgraph.runtime import Runtime
from langgraph.store.memory import InMemoryStore

@dataclass
class Context:  # (1)!
    user_id: str

class State(TypedDict, total=False):
    response: str

store = InMemoryStore()  # (2)!
store.put(("users",), "user_123", {"name": "Alice"})

def personalized_greeting(state: State, runtime: Runtime[Context]) -> State:
    '''Generate personalized greeting using runtime context and store.'''
    user_id = runtime.context.user_id  # (3)!
    name = "unknown_user"
    if runtime.store:
        if memory := runtime.store.get(("users",), user_id):
            name = memory.value["name"]

    response = f"Hello {name}! Nice to see you again."
    return {"response": response}

graph = (
    StateGraph(state_schema=State, context_schema=Context)
    .add_node("personalized_greeting", personalized_greeting)
    .set_entry_point("personalized_greeting")
    .set_finish_point("personalized_greeting")
    .compile(store=store)
)

result = graph.invoke({}, context=Context(user_id="user_123"))
print(result)
# > {'response': 'Hello Alice! Nice to see you again.'}
  1. Define a schema for the runtime context.
  2. Create a store to persist memories and other information.
  3. Use the runtime context to access the user_id.
class
CachePolicy

Configuration for caching nodes.

class
Command

One or more commands to update the graph's state and send messages to nodes.

class
Interrupt

Information about an interrupt that occurred in a node.

Changed in version v0.4.0
  • interrupt_id was introduced as a property
Changed in version v0.6.0

The following attributes have been removed:

  • ns
  • when
  • resumable
  • interrupt_id, deprecated in favor of id
class
Send

A message or packet to send to a specific node in the graph.

The Send class is used within a StateGraph's conditional edges to dynamically invoke a node with a custom state at the next step.

Importantly, the sent state can differ from the core graph's state, allowing for flexible and dynamic workflow management.

One such example is a "map-reduce" workflow where your graph invokes the same node multiple times in parallel with different states, before aggregating the results back into the main graph's state.

class
StateSnapshot

Snapshot of the state of the graph at the beginning of a step.

class
StateUpdate
class
LangGraphDeprecatedSinceV10

A specific LangGraphDeprecationWarning subclass defining functionality deprecated since LangGraph v1.0.0

class
NodeBuilder
class
Pregel

Pregel manages the runtime behavior for LangGraph applications.

Overview

Pregel combines actors and channels into a single application. Actors read data from channels and write data to channels. Pregel organizes the execution of the application into multiple steps, following the Pregel Algorithm/Bulk Synchronous Parallel model.

Each step consists of three phases:

  • Plan: Determine which actors to execute in this step. For example, in the first step, select the actors that subscribe to the special input channels; in subsequent steps, select the actors that subscribe to channels updated in the previous step.
  • Execution: Execute all selected actors in parallel, until all complete, or one fails, or a timeout is reached. During this phase, channel updates are invisible to actors until the next step.
  • Update: Update the channels with the values written by the actors in this step.

Repeat until no actors are selected for execution, or a maximum number of steps is reached.

Actors

An actor is a PregelNode. It subscribes to channels, reads data from them, and writes data to them. It can be thought of as an actor in the Pregel algorithm. PregelNodes implement LangChain's Runnable interface.

Channels

Channels are used to communicate between actors (PregelNodes). Each channel has a value type, an update type, and an update function – which takes a sequence of updates and modifies the stored value. Channels can be used to send data from one chain to another, or to send data from a chain to itself in a future step. LangGraph provides a number of built-in channels:

Basic channels: LastValue and Topic

  • LastValue: The default channel, stores the last value sent to the channel, useful for input and output values, or for sending data from one step to the next
  • Topic: A configurable PubSub Topic, useful for sending multiple values between actors, or for accumulating output. Can be configured to deduplicate values, and/or to accumulate values over the course of multiple steps.

Advanced channels: Context and BinaryOperatorAggregate

  • Context: exposes the value of a context manager, managing its lifecycle. Useful for accessing external resources that require setup and/or teardown. e.g. client = Context(httpx.Client)
  • BinaryOperatorAggregate: stores a persistent value, updated by applying a binary operator to the current value and each update sent to the channel, useful for computing aggregates over multiple steps. e.g. total = BinaryOperatorAggregate(int, operator.add)

Examples

Most users will interact with Pregel via a StateGraph (Graph API) or via an entrypoint (Functional API).

However, for advanced use cases, Pregel can be used directly. If you're not sure whether you need to use Pregel directly, then the answer is probably no

  • you should use the Graph API or Functional API instead. These are higher-level interfaces that will compile down to Pregel under the hood.

Here are some examples to give you a sense of how it works:

Type Aliases

typeAlias
RunnableLike
typeAlias
Checkpointer

Type of the checkpointer to use for a subgraph.

  • True enables persistent checkpointing for this subgraph.
  • False disables checkpointing, even if the parent graph has a checkpointer.
  • None inherits checkpointer from the parent graph.
View source on GitHub