Skip to main content

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. orjson is 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 typing constructs 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, UUID and Decimal are not validated natively; use a str field with format= plus a getter= hook to hydrate.
  • No recursive schemas. A self-referential TypedDict raises SchemaDefinitionError.
  • Validation mutates its input unless you pass copy=True.

License

MIT

Metadata

Release files for pytastic 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytastic 0.6.0
File Size Uploaded Provenance
pytastic-0.6.0.tar.gz 28.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pytastic 0.6.0
File Interpreter ABI Platform
pytastic-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 60.0 kB

Release files / pytastic-0.6.0.tar.gz

Download URL pytastic-0.6.0.tar.gz
Size 28.9 kB
Tags Source
SHA-256 checksum
How to use checksums
20c15e5819abd60332d19678c742580ee1874d91970a150231b1dbc4de6533f7
BLAKE2b-256 checksum
How to use checksums
417b47d6eb7b671c82920db9eabce687e845a8f41a7651090415e547f42f250a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.12.3 Linux/7.0.0-28-generic

Release files / pytastic-0.6.0-py3-none-any.whl

Download URL pytastic-0.6.0-py3-none-any.whl
Size 31.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6323c4c0f70bbd04b0823a14aaefaf9bd8dc76c73326505251087e9d2628c196
BLAKE2b-256 checksum
How to use checksums
a715a37352e704a8c6df9270c15b23578cda8e497bc872ea282231d4bf0b96b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.12.3 Linux/7.0.0-28-generic

Security for pytastic 0.6.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page