pydantic-store
A collection of Pydantic models and storage approaches for ease of reading to/from files or creating caches.
Installation
pip install pydantic-store
Optional Dependencies
Additional dependencies needed for storing as YAML or TOML (not included to keep the dependency limited to pydantic)
# For YAML support
pip install ruamel.yaml
# For TOML writing support
pip install tomli-w
# For TOML reading on Python < 3.11
pip install tomli
Quick Start
from pydantic_store import BaseModel, ListModel, DictModel, JsonStore, PydanticDBM
# Define your models
class User(BaseModel):
name: str
age: int
email: str
# Create and save to file
user = User(name="Alice", age=30, email="alice@example.com")
user.to_file("user.json")
# Load from file
loaded_user = User.from_file("user.json")
Model Types
BaseModel
Enhanced Pydantic BaseModel with file I/O capabilities.
from pydantic_store import BaseModel
class Config(BaseModel):
database_url: str
debug: bool = False
max_connections: int = 10
# Save to different formats
config = Config(database_url="postgresql://localhost/mydb")
config.to_file("config.json") # JSON format
config.to_file("config.yaml") # YAML format
config.to_file("config.toml") # TOML format
# Load from file (format auto-detected by extension)
config = Config.from_file("config.yaml")
RootModel
A generic root model for wrapping single values with validation.
from pydantic_store import RootModel
class Port(RootModel[int]):
pass
port = Port(8080)
port.to_file("port.json") # Saves: 8080
loaded_port = Port.from_file("port.json")
ListModel
A list-like model that behaves like a Python list while providing Pydantic validation.
from pydantic_store import ListModel
TodoList = ListModel[str]
todos = TodoList(["Buy groceries", "Walk the dog"])
# Use like a regular list
todos.append("Read a book")
todos.extend(["Exercise", "Cook dinner"])
print(len(todos)) # 5
print(todos[0]) # "Buy groceries"
# Persist to file
todos.to_file("todos.json")
# Load from file
loaded_todos = TodoList.from_file("todos.json")
DictModel
A dictionary-like model that behaves like a Python dict with validation.
from pydantic_store import DictModel
Settings = DictModel[str, int]
settings = Settings({"timeout": 30, "retries": 3})
# Use like a regular dict
settings["max_workers"] = 4
settings.update({"cache_size": 1000})
print(settings.keys())
print(len(settings))
# Persist to file
settings.to_file("settings.yaml")
PydanticDBM
A wrapper around the sqlite DBM backend (backported from 3.14) to store and retrieve pydantic models.
from pydantic_store import PydanticDBM
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
UserDBM = PydanticDBM[User]
# Method 1: Type subscription
with UserDBM("users.db") as db:
user = User(name="Alice", age=30)
db["alice"] = user
retrieved_user = db["alice"] # Automatically validated as User
# Method 2: Explicit storage format
with PydanticDBM("users.db", storage_format=User) as db:
db["bob"] = User(name="Bob", age=25)
JsonStore
A persistent dictionary that automatically saves changes to disk.
from pydantic_store import JsonStore
from pathlib import Path
# Connect to a JSON file (creates if doesn't exist)
store = JsonStore[str].connect(Path("data.json"))
# Changes are automatically persisted
store["user:1"] = "Alice"
store["user:2"] = "Bob"
# Data is immediately written to data.json
print(store["user:1"]) # "Alice"
Supported Formats
pydantic-store supports multiple file formats with automatic format detection:
- JSON: Always available
- YAML: Human-readable format (requires
ruamel.yaml) - TOML: Configuration-friendly format (requires
tomlifor reading,tomli-wfor writing)
You can also specify the format:
model.to_file("data.txt", file_format="json")
Licence
MIT Licence - see LICENSE.md for details.
Metadata
Release files for pydantic-store 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pydantic_store-0.1.1.tar.gz | 7.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydantic_store-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.2 kB
Release files / pydantic_store-0.1.1.tar.gz
| Download URL | pydantic_store-0.1.1.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a5a99aacb196377540be7412c209f5c0b3747976f227bd37f6e1f83ff6dbb2ad
|
|
BLAKE2b-256 checksum How to use checksums |
8879a5dd783b8294e64825b3656ee0bc3339a550486862e53194c9ceac571ae3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 2, 2026.
Transparency logRelease files / pydantic_store-0.1.1-py3-none-any.whl
| Download URL | pydantic_store-0.1.1-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ab8c197dccacc301e100fa6fac581d36160f24db9f87758fc899f23a8882c40f
|
|
BLAKE2b-256 checksum How to use checksums |
c6b9e1d787fa5c5d215405284dee7c880c60a8ffeb88f598cb5685bb173b5cce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 2, 2026.
Transparency log