Skip to content

Persistence Reference

persistence

Combined LangGraph persistence helpers.

Purpose

Provide a layered async context manager that opens both the LangGraph Postgres store and Postgres checkpointer together.

Design

This module uses AsyncExitStack so both async context managers are opened and closed in one place. It is the most convenient API for: - graph compilation with both store and checkpointer - bootstrap/setup of both persistence layers - application lifespan initialization

Attributes:

Name Type Description
PersistencePair

Tuple alias for the opened store and checkpointer.

Examples:

.. code-block:: python

async with persistence_context() as (store, checkpointer):
    graph = builder.compile(
        checkpointer=checkpointer,
        store=store,
    )

.. code-block:: python

await setup_persistence()

persistence_context async

persistence_context(*, setup=False)

Yield both the LangGraph store and checkpointer together.

Parameters:

Name Type Description Default
setup bool

Whether to initialize both persistence layers before yielding them. This is mainly intended for first-run bootstrap.

False

Yields:

Type Description
AsyncIterator[PersistencePair]

A tuple of (store, checkpointer).

Raises:

Type Description
Exception

Propagates database or connection errors from the underlying LangGraph / psycopg layers.

Examples:

.. code-block:: python

async with persistence_context() as (store, checkpointer):
    graph = builder.compile(
        checkpointer=checkpointer,
        store=store,
    )

.. code-block:: python

async with persistence_context(setup=True):
    pass

setup_persistence async

setup_persistence()

Initialize both the LangGraph store and checkpoint tables.

Returns:

Type Description
None

None

Raises:

Type Description
Exception

Propagates database or connection errors from the underlying LangGraph / psycopg layers.

Examples:

.. code-block:: python

await setup_persistence()