A lightweight TOML-based memory system for AI agents
Project description
TOMLDiary
Memory, Simplified: TOML-Driven, Agent-Approved.
TOMLDiary is a dead-simple, customizable memory system for agentic applications. It stores data in human-readable TOML files so your agents can keep a tidy diary of only the useful stuff.
Key Benefits
- Human-readable TOML storage – easy to inspect, debug and manage.
- Fully customizable – define your own memory schema with simple Pydantic models.
- Minimal overhead – lightweight design, backend agnostic and easy to integrate.
- Atomic, safe writes – ensures data integrity with proper file locking.
Installation
Requires Python 3.11+
uv add tomldiary pydantic-ai
Quick Start
from pydantic import BaseModel
from typing import Dict
from tomldiary import Diary, PreferenceItem
from tomldiary.backends import LocalBackend
# Be as specific as possible in your preference schema, it passed to the system prompt of the agent extracting the data!
# This of the fields as the "slots" to organize facts into and tell the agent what to remember.
class MyPrefTable(BaseModel):
"""
likes : What the user enjoys
dislikes : Things user avoids
allergies: Substances causing reactions
routines : User’s typical habits
biography: User’s personal details
"""
likes: Dict[str, PreferenceItem] = {}
dislikes: Dict[str, PreferenceItem] = {}
allergies: Dict[str, PreferenceItem] = {}
routines: Dict[str, PreferenceItem] = {}
biography: Dict[str, PreferenceItem] = {}
diary = Diary(
backend=LocalBackend(path="./memories"),
pref_table_cls=MyPrefTable,
max_prefs_per_category=100,
max_conversations=50,
)
await diary.ensure_session(user_id, session_id)
await diary.update_memory(
user_id,
session_id,
user_message="I'm allergic to walnuts.",
agent_response="I'll remember you're allergic to walnuts.",
)
TOML Memory Example
[_meta]
version = "0.2"
schema_name = "MyPrefTable"
[allergies.walnuts]
text = "allergic to walnuts"
contexts = ["diet", "health"]
_count = 1
_created = "2024-01-01T00:00:00Z"
_updated = "2024-01-01T00:00:00Z"
Conversations File (alice_conversations.toml)
[_meta]
version = "0.2"
schema_name = "MyPrefTable"
[chat_123]
_created = "2024-01-01T00:00:00Z"
_turns = 5
summary = "Discussed food preferences and dietary restrictions"
keywords = ["food", "allergy", "italian"]
Advanced Usage
Custom Preference Categories
Create your own preference schema:
class DetailedPrefTable(BaseModel):
"""
dietary : Food preferences and restrictions
medical : Health conditions and medications
interests : Hobbies and topics of interest
goals : Personal objectives and aspirations
family : Family members and relationships
work : Professional information
"""
dietary: Dict[str, PreferenceItem] = {}
medical: Dict[str, PreferenceItem] = {}
interests: Dict[str, PreferenceItem] = {}
goals: Dict[str, PreferenceItem] = {}
family: Dict[str, PreferenceItem] = {}
work: Dict[str, PreferenceItem] = {}
Backend Options
The library supports different storage backends:
# Local filesystem (default)
from tomldiary.backends import LocalBackend
backend = LocalBackend(Path("./memories"))
# S3 backend (implement S3Backend)
# backend = S3Backend(bucket="my-memories")
# Redis backend (implement RedisBackend)
# backend = RedisBackend(host="localhost")
Memory Writer Configuration
# Configure the background writer
writer = MemoryWriter(
diary=diary,
workers=3, # Number of background workers
qsize=100, # Queue size
retry_limit=3, # Max retries on failure
retry_delay=1.0 # Delay between retries
)
API Reference
Diary
Main class for memory operations:
preferences(user_id): Get user preferences as TOML stringlast_conversations(user_id, n): Get last N conversation summariesensure_session(user_id, session_id): Create session if neededupdate_memory(user_id, session_id, user_msg, assistant_msg): Process and store memory
MemoryWriter
Background queue for non-blocking writes:
submit(user_id, session_id, user_message, assistant_response): Queue memory updateclose(): Graceful shutdownfailed_count(): Number of failed operations
Models
PreferenceItem: Single preference with text, contexts, and metadataConversationItem: Conversation with summary, keywords, and turn countMemoryDeps: Container for preferences and conversations
Examples
See the examples/ directory for:
example_simple_demo.py: Basic usage without LLMexample_tomldiary.py: Full LLM integrationexample_multi_user.py: Concurrent multi-user scenario
Development
# Install dev dependencies
uv sync --group dev
# Run tests
pytest
# Format code
ruff format .
# Lint code
ruff check .
License
MIT License - see LICENSE file for details.
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 tomldiary-0.0.2.tar.gz.
File metadata
- Download URL: tomldiary-0.0.2.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edfc2852b9a6df97dd88760c12db20dc919b0da7cf8b2f5a62fd780efc5054ac
|
|
| MD5 |
6b8d28fa009a59af505c31aa329cbf2c
|
|
| BLAKE2b-256 |
16b5c0af4a20f8b6f8da4ef5299f47c91008bedee2eb7e87615090f9eefa9b46
|
File details
Details for the file tomldiary-0.0.2-py3-none-any.whl.
File metadata
- Download URL: tomldiary-0.0.2-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b5e53964aa3ba201ec12ecdabee9d933706aa834c5b15db8772fb679dddada9
|
|
| MD5 |
c6b39c75a0e734beb2ddb436311da47b
|
|
| BLAKE2b-256 |
6d866c7be5f9378a7548ecf44888c1975aec56d0b0773098f2090e104e65c0a2
|