The platform for continuously improving AI agents.
"""LangGraph checkpoints of experiment executions, in our own database. Their tables come
from a migration that runs the saver's setup; an execution's checkpoints go with it."""
from contextlib import contextmanager
from django.db import connection
from langgraph.checkpoint.postgres import PostgresSaver
from langgraph.checkpoint.serde.jsonplus import JsonPlusSerializer
from psycopg import Connection
from psycopg.rows import dict_row
@contextmanager
def store(database=None):
config = database or connection.settings_dict
options = config.get("OPTIONS", {})
# LangGraph's saver needs psycopg 3 with autocommit; Django's own connection is psycopg2.
with Connection.connect(
dbname=config["NAME"],
user=config["USER"],
password=config["PASSWORD"],
host=config["HOST"],
port=config["PORT"],
sslmode=options.get("sslmode", "prefer"),
autocommit=True,
prepare_threshold=0,
row_factory=dict_row,
) as database_connection:
# Only JSON-native values: a checkpoint never unpickles or imports a module.
yield PostgresSaver(
database_connection, serde=JsonPlusSerializer(allowed_msgpack_modules=[])
)