Library with a single-table DynamoDB implementation of LangGraph checkpoint saver.
Project description
LangGraph Checkpoint DynamoDB
A single table DynamoDB implementation of the LangGraph checkpointer interface for persisting graph state and enabling features like human-in-the-loop, memory, time travel, and fault tolerance. Support sync and async methods with efficient DynamoDB queries and custom table configuration.
Installation
pip install langgraph-checkpoint-amazon-dynamodb
Quick Start
Note: For the default configuration to work, you need to have the AWS credentials configured for your environment. See the AWS Documentation for more details.
Create the DynamoDB table and compile the checkpointer with default settings:
from langgraph.graph import END, START, MessagesState, StateGraph
from langgraph_checkpoint_dynamodb import DynamoDBSaver
# Set deploy=True to create the table with default settings if it doesn't exist
checkpointer = DynamoDBSaver(deploy=True)
# Use with your LangGraph, below is a simple example
workflow = StateGraph(MessagesState)
workflow.add_node("chatbot", lambda state: {"messages": [{"role": "ai", "content": "Hello!"}]})
workflow.add_edge(START, "chatbot")
workflow.add_edge("chatbot", END)
graph = workflow.compile(checkpointer=checkpointer)
Use a thread ID to retain state between invocations:
config={"configurable": {"thread_id": "1"}}
# Checkpoints will be saved with the configured thread_id as DynamoDB partition key
print(graph.invoke({"messages": [{"role": "human", "content": "Hi!"}]}, config))
# Review last saved checkpoint
print(graph.get_state(config))
Configuration
To customize the DynamoDB table or client configuration, use the DynamoDBConfig and DynamoDBTableConfig classes:
from langgraph_checkpoint_dynamodb import DynamoDBSaver, DynamoDBConfig, DynamoDBTableConfig
config = DynamoDBConfig(
table_config=DynamoDBTableConfig(
# Customize table name as needed
table_name="langgraph-checkpoint",
),
# Optional AWS credentials (if not using default credentials)
aws_access_key_id="your-access-key-id",
aws_secret_access_key="your-secret-access-key",
aws_session_token="your-session-token"
)
# Create a table with custom configuration
checkpointer = DynamoDBSaver(config, deploy=True)
# Using an existing table with custom configuration
# checkpointer = DynamoDBSaver(config)
DynamoDB Table Configuration Options
The DynamoDBTableConfig class provides configuration options for the DynamoDB table:
from langgraph_checkpoint_dynamodb import DynamoDBTableConfig, BillingMode
table_config = DynamoDBTableConfig(
table_name="langgraph-checkpoint", # Name of the DynamoDB table
billing_mode=BillingMode.PAY_PER_REQUEST, # PAY_PER_REQUEST or PROVISIONED
enable_encryption=True, # Enable server-side encryption
enable_point_in_time_recovery=False, # Enable point-in-time recovery
ttl_days=30, # Enable TTL with 30 days expiration (set to None to disable)
ttl_attribute="expireAt", # TTL attribute name
# For PROVISIONED billing mode only:
read_capacity=None, # Provisioned read capacity units
write_capacity=None, # Provisioned write capacity units
# Optional auto-scaling configuration
min_read_capacity=None,
max_read_capacity=None,
min_write_capacity=None,
max_write_capacity=None
)
DynamoDB Client Configuration Options
The DynamoDBConfig class provides configuration for the DynamoDB client:
from langgraph_checkpoint_dynamodb import DynamoDBConfig
config = DynamoDBConfig(
table_config=table_config, # DynamoDBTableConfig instance
region_name="us-west-2", # AWS region selection
endpoint_url=None, # Custom endpoint URL (e.g., for local DynamoDB)
max_retries=3, # Maximum number of retries
initial_retry_delay=0.1, # Initial retry delay in seconds
max_retry_delay=1.0, # Maximum retry delay in seconds
# Optional AWS credentials (if not using default credentials)
aws_access_key_id=None,
aws_secret_access_key=None,
aws_session_token=None
)
Infrastructure Setup
There are three ways to set up the required DynamoDB infrastructure:
1. Using DynamoDBSaver Methods
The simplest way to create and manage the table:
from langgraph_checkpoint_dynamodb import DynamoDBSaver, DynamoDBConfig
config = DynamoDBConfig(...)
# Create table
checkpointer = DynamoDBSaver(config, deploy=True)
# Delete table when no longer needed
checkpointer.destroy()
2. Using CloudFormation
For simple deployments using CloudFormation:
# template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: 'DynamoDB table for LangGraph checkpoint storage'
Parameters:
TableName:
Type: String
Default: langgraph-checkpoint
Environment:
Type: String
Default: dev
AllowedValues: [dev, staging, prod]
Resources:
CheckpointTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Ref TableName
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: RANGE
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
SSESpecification:
SSEEnabled: true
Deploy using AWS CLI:
aws cloudformation deploy \
--template-file template.yaml \
--stack-name langgraph-checkpoint \
--parameter-overrides TableName=langgraph-checkpoint Environment=prod
3. Using AWS CDK
For more advanced infrastructure management using AWS CDK Python, install the infra optional dependency:
pip install "langgraph-checkpoint-amazon-dynamodb[infra]"
Then import the DynamoDBCheckpointStack to your CDK app:
from aws_cdk import App
from langgraph_checkpoint_dynamodb.infra import DynamoDBCheckpointStack
from langgraph_checkpoint_dynamodb import DynamoDBTableConfig
app = App()
# Create the stack with custom configuration
table_config = DynamoDBTableConfig(
table_name="langgraph-checkpoint",
enable_point_in_time_recovery=True,
enable_encryption=True
)
DynamoDBCheckpointStack(
app,
"LangGraphCheckpoint",
table_config=table_config,
tags={"Environment": "prod", "Tenant": "tenant-1"},
)
app.synth()
Example usage with LangGraph in production
In production, it's recommended to deploy the table independently and reference it in the DynamoDBConfig class (deploy=False):
from langgraph.graph import StateGraph
from langgraph_checkpoint_dynamodb import DynamoDBSaver, DynamoDBConfig
# Use an existing table as checkpointer
config = DynamoDBConfig(...)
checkpointer = DynamoDBSaver(config)
# Create your graph
workflow = StateGraph(...)
# ... configure your graph ...
# Compile with checkpointer
graph = workflow.compile(checkpointer=checkpointer)
# Use thread_id for persistence
config = {"configurable": {"thread_id": "<unique-thread-id>"}}
# Run the graph and get the final output state
graph.invoke({"messages": [{"type": "user", "content": "Hello!"}]}, config)
# Run with async methods and stream updates after every node
async for chunk in graph.astream(
{"messages": [{"type": "user", "content": "Hello!"}]},
config
):
chunk["messages"][-1].pretty_print()
Features
The DynamoDB checkpointer enables all LangGraph persistence features:
- Human-in-the-loop: Inspect, interrupt, and approve graph steps
- Memory: Retain state between interactions in the same thread
- Time Travel: Replay and debug specific graph steps
- Fault Tolerance: Recover from failures and resume from last successful step
- Sync and Async: Support sync and async methods using efficient DynamoDB KeyConditionExpressions for cost-effective queries
Running Tests
All tests use local/mock AWS services and never create cloud resources. For detailed testing instructions, see tests/README.md.
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langgraph_checkpoint_amazon_dynamodb-0.1.3.tar.gz.
File metadata
- Download URL: langgraph_checkpoint_amazon_dynamodb-0.1.3.tar.gz
- Upload date:
- Size: 36.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5501c9c53098f8dc07dfd44338b3916abb3223e530f1ce907bb3c0ee8628c422
|
|
| MD5 |
276fdf24ee0ef304a7b6fd66b52c6806
|
|
| BLAKE2b-256 |
e9f327c6c5d9157a767015e47870fee022261c2985983c9bd585e6dd3bc6688c
|
File details
Details for the file langgraph_checkpoint_amazon_dynamodb-0.1.3-py3-none-any.whl.
File metadata
- Download URL: langgraph_checkpoint_amazon_dynamodb-0.1.3-py3-none-any.whl
- Upload date:
- Size: 46.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb128236d036d91c81f04c15229fa2121283f86d86a82da0c34b56fb369f0b52
|
|
| MD5 |
9c607fcea684ae85d805c0199bdefec1
|
|
| BLAKE2b-256 |
0b7b093a48ecfa2d0210005537e610b202b379ccb4ab6a2ed5920eb1cd8b5e5c
|