Pytastic
No Magic. Just Python.
Pytastic validates JSON-shaped data against schemas you write as ordinary TypedDict
type hints. If you know TypedDict and Annotated, you already know how to use it.
Zero dependencies, and your validated data stays a plain dict.
Full documentation: rayattack.github.io/pytastic
from typing import TypedDict, Annotated, Literal
from pytastic import Pytastic, ValidationError
vx = Pytastic()
class User(TypedDict):
username: Annotated[str, "min_len=3; regex=^[a-z_]+$"]
age: Annotated[int, "min=18"]
role: Literal["admin", "user"]
user = vx.validate(User, {"username": "tersoo", "age": 25, "role": "admin"})
try:
vx.validate(User, {"username": "x", "age": 25, "role": "admin"})
except ValidationError as e:
print(e.errors) # [{'path': '.username', 'message': 'Min length 3'}]
Constraints are semicolon-separated key=value pairs inside the Annotated metadata
string. See the constraint reference.
Why?
- Zero dependencies. Pure standard library, so it installs anywhere a compiled wheel
is awkward.
orjsonis an optional accelerator. - Your data stays a dict. Validation returns the dict you passed in, typed as your
own
TypedDict— no model objects, no.model_dump()at every boundary. - No learning curve. Standard
typingconstructs your editor already understands. - Fast where it counts. See below.
Performance
Same six-field schema, same constraints, Python 3.12, best of 5 runs. Reproduce with
python benchmark.py:
| Comparison | Result |
|---|---|
vs Pydantic BaseModel(**kwargs) |
Pytastic ~1.5x faster |
vs Pydantic model_validate(dict) |
Pytastic ~1.4x faster |
vs Pydantic TypeAdapter(TypedDict) |
Pydantic ~1.3x faster |
| JSON bytes → validated | Pydantic ~2x faster (fused parse in Rust) |
| Memory, 10,000 records | Pytastic allocates nothing; Pydantic ~4.7 MiB |
To be straight about it: Pytastic beats Pydantic's BaseModel paths and loses to
TypeAdapter and to Pydantic's fused JSON parsing, which is compiled Rust. If raw
throughput on large JSON payloads is your only concern, use msgspec. Pytastic's case is
being fast and dependency-free and allocation-free while your data stays a dict.
Installation
pip install pytastic
With optional orjson acceleration:
pip install pytastic[fast]
Requires Python 3.9 or newer.
Two ways to call it
Typed — no registration, best for editor autocompletion:
user = vx.validate(User, data)
Dynamic — register once, then call the schema by name. Skips option handling, so it is marginally faster in a hot loop, but accepts no options:
vx.register(User)
user = vx.User(data)
Registering at startup also compiles the schema immediately, so a malformed schema fails at boot rather than on the first request.
JSON Schema export
print(vx.schema(User))
# {"type": "object", "properties": {"username": {"type": "string", "minLength": 3, ...}}, ...}
Returns a JSON string (Draft 2020-12). Use json.loads() if you need a dict.
Beyond the basics
Pytastic also supports partial/PATCH validation, unknown-field stripping, input and output field mapping, defaults, computed fields, pre- and post-validation hooks, dotted attribute access and conditional constraints. See Advanced Usage.
Known limitations
Worth knowing before you adopt:
- One error per call. Validation stops at the first failure rather than collecting every problem, unlike Pydantic.
- No rich types.
datetime,UUIDandDecimalare not validated natively; use astrfield withformat=plus agetter=hook to hydrate. - No recursive schemas. A self-referential
TypedDictraisesSchemaDefinitionError. - Validation mutates its input unless you pass
copy=True.
License
MIT
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 pytastic-0.6.0.tar.gz.
File metadata
- Download URL: pytastic-0.6.0.tar.gz
- Upload date:
- Size: 28.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
poetry/2.1.3 CPython/3.12.3 Linux/7.0.0-28-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20c15e5819abd60332d19678c742580ee1874d91970a150231b1dbc4de6533f7
|
|
| MD5 |
48e34280bf152b4ab8c707e357fd64fe
|
|
| BLAKE2b-256 |
417b47d6eb7b671c82920db9eabce687e845a8f41a7651090415e547f42f250a
|
File details
Details for the file pytastic-0.6.0-py3-none-any.whl.
File metadata
- Download URL: pytastic-0.6.0-py3-none-any.whl
- Upload date:
- Size: 31.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
poetry/2.1.3 CPython/3.12.3 Linux/7.0.0-28-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6323c4c0f70bbd04b0823a14aaefaf9bd8dc76c73326505251087e9d2628c196
|
|
| MD5 |
977ec31f87f9e394326ec40cc2ffe1dc
|
|
| BLAKE2b-256 |
a715a37352e704a8c6df9270c15b23578cda8e497bc872ea282231d4bf0b96b3
|