LangGraph checkpoint implementation for Django
Project description
LangGraph Checkpoint Django
A Django-based checkpoint saver for LangGraph, providing persistent storage for conversation states and workflow checkpoints using Django ORM.
Features
- 🗄️ Persistent Storage: Store LangGraph checkpoints in your Django database
- 🔄 Thread Management: Support for conversation threads and namespaces
- 🚀 Django Integration: Seamless integration with existing Django projects
Installation
Install the package using pip:
pip install langgraph-checkpoint-django
Quick Start
1. Add to Django Settings
Add the checkpoint app to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
# Your existing apps
'langgraph.checkpoint.django.checkpoint',
# Other apps...
]
2. Run Migrations
Create and apply the database migrations:
python manage.py migrate
3. Basic Usage
Synchronous Usage
from langgraph.checkpoint.django.saver import DjangoSaver
from langgraph.graph import StateGraph, MessagesState
from langchain_core.runnables import RunnableConfig
def example():
# Initialize the checkpoint saver
checkpointer = DjangoSaver()
# Create your graph with checkpointing
def my_node(state: MessagesState):
return {"messages": state["messages"] + ["processed"]}
graph = StateGraph(MessagesState)
graph.add_node("process", my_node)
graph.set_entry_point("process")
# Compile with checkpointing
app = graph.compile(checkpointer=checkpointer)
# Use with thread configuration
config = RunnableConfig(
configurable={
"thread_id": "conversation-1",
"checkpoint_ns": "default"
}
)
# Run the graph
result = app.invoke({"messages": ["hello"]}, config=config)
print(result)
Asynchronous Usage
import asyncio
from langgraph.checkpoint.django.aio import AsyncDjangoSaver
from langgraph.graph import StateGraph, MessagesState
from langchain_core.runnables import RunnableConfig
async def example():
# Initialize the async checkpoint saver
checkpointer = AsyncDjangoSaver()
# Create your graph with checkpointing
async def my_async_node(state: MessagesState):
# Simulate async processing
await asyncio.sleep(0.1)
return {"messages": state["messages"] + ["processed async"]}
graph = StateGraph(MessagesState)
graph.add_node("process", my_async_node)
graph.set_entry_point("process")
# Compile with async checkpointing
app = graph.compile(checkpointer=checkpointer)
# Use with thread configuration
config = RunnableConfig(
configurable={
"thread_id": "conversation-async-1",
"checkpoint_ns": "default"
}
)
# Run the graph
result = await app.ainvoke({"messages": ["hello async"]}, config=config)
print(result)
Database Models
The package creates three main models:
- Checkpoint: Stores the main checkpoint data and metadata
- Write: Stores pending writes and task information
API Reference
DjangoSaver
The synchronous checkpoint saver class for Django ORM integration.
Methods
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]: Retrieve a checkpoint tuplelist(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]: List checkpoints with optional filteringput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig: Store a checkpoint and return updated configput_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = "") -> None: Store pending writes for a checkpointdelete_thread(thread_id: str) -> None: Delete all checkpoints and writes for a thread
AsyncDjangoSaver
The asynchronous checkpoint saver class for Django ORM integration, using Django's async ORM capabilities.
Methods
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]: Asynchronously retrieve a checkpoint tuplealist(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]: Asynchronously list checkpoints with optional filteringaput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig: Asynchronously store a checkpoint and return updated configaput_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = "") -> None: Asynchronously store pending writes for a checkpointadelete_thread(thread_id: str) -> None: Asynchronously delete all checkpoints and writes for a thread
Performance Considerations
- Indexing: The package creates appropriate database indexes for optimal query performance
- Cleanup: Regularly clean up old checkpoints to manage database size
- Connection Pooling: Use Django's database connection pooling for better performance
- Bulk Operations: The saver uses bulk operations where possible to reduce database queries
Requirements
Basic Requirements (DjangoSaver)
- Python 3.8+
- Django 3.1+
- LangGraph 0.1+
- LangChain Core
Async Requirements (AsyncDjangoSaver)
- Python 3.8+
- Django 4.1+ (required for full async ORM support)
- LangGraph 0.1+
- LangChain Core
- ASGI server (e.g., uvicorn, daphne) for production deployment
Database Support
JSONField Support: This package requires Django with
models.JSONFieldsupport:
- MySQL: MySQL 5.7.8+ or MariaDB 10.2.7+
- SQLite: SQLite 3.9.0+ (with the JSON1 extension enabled)
Async Database Support (for AsyncDjangoSaver only):
- PostgreSQL: Requires
asyncpgdriver- MySQL: Requires
aiomysqldriver- SQLite: Built-in async support in Django 4.1+
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
v0.1.0
- Initial release
- Basic checkpoint functionality
- Django ORM integration
- Thread management support
Made with ❤️ for the LangGraph community
Project details
Release history Release notifications | RSS feed
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_django-0.1.1.tar.gz.
File metadata
- Download URL: langgraph_checkpoint_django-0.1.1.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57cc3e4dfebef25a686ae1c77a00f9a11c24e38667f5e68215c1a90545c334e1
|
|
| MD5 |
b6d599a6ce5ccf36ea5d66f01dbd3a1f
|
|
| BLAKE2b-256 |
738fd857b7cbc17bddb63e5946d260323dabc2dd5ae2f175ee084473be6cf87b
|
File details
Details for the file langgraph_checkpoint_django-0.1.1-py3-none-any.whl.
File metadata
- Download URL: langgraph_checkpoint_django-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e79d406c3285d1437545a978ef86dd25820fd5d5e7eff30e74d97de4f733a2a
|
|
| MD5 |
174633b542ba6ac7e07c7b58eb02ec6b
|
|
| BLAKE2b-256 |
0334442bab007f15e7efcfd210d46c1775c5f736005aec88c787535b8754bf6a
|