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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytastic-0.6.0.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytastic-0.6.0-py3-none-any.whl (31.1 kB view details)

Uploaded Python 3

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

Hashes for pytastic-0.6.0.tar.gz
Algorithm Hash digest
SHA256 20c15e5819abd60332d19678c742580ee1874d91970a150231b1dbc4de6533f7
MD5 48e34280bf152b4ab8c707e357fd64fe
BLAKE2b-256 417b47d6eb7b671c82920db9eabce687e845a8f41a7651090415e547f42f250a

See more details on using hashes here.

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

Hashes for pytastic-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6323c4c0f70bbd04b0823a14aaefaf9bd8dc76c73326505251087e9d2628c196
MD5 977ec31f87f9e394326ec40cc2ffe1dc
BLAKE2b-256 a715a37352e704a8c6df9270c15b23578cda8e497bc872ea282231d4bf0b96b3

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 files

0.5.0

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.0

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

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