fastjsonschema
Fast JSON schema validator for Python that compiles schemas to generated code
Repository Health
Technical Analysis
fastjsonschema is a Python library that validates JSON documents against JSON Schema (drafts 04, 06, and 07) by compiling each schema into generated Python source code rather than interpreting it at validation time. This code-generation approach makes it dramatically faster than interpreter-based validators like jsonschema — the project’s own benchmarks show roughly 25ms for valid inputs versus multi-second run times for popular alternatives — which matters when validation runs on every request in a high-throughput service.
The library is used as the JSON Schema validation engine inside Jupyter’s nbformat, making it one of the most widely deployed validators in the Python data-science ecosystem. It exposes both a one-shot validate() function and a compile() function that returns a reusable validator, so callers who validate the same schema repeatedly can pay the compilation cost once and reuse the generated function afterward.
What You Get
- A
validate(schema, data)function for one-off, one-shot JSON Schema validation - A
compile(schema)function that generates and returns a reusable validator function for repeated validation of the same schema - Support for JSON Schema drafts 04, 06, and 07, each implemented in its own module (
draft04.py,draft06.py,draft07.py) - Optional code-generation-to-file mode for squeezing out extra performance in hot paths
- Descriptive
JsonSchemaExceptionerrors that point to the offending path and value
Common Use Cases
- Validating request/response payloads in high-throughput APIs where interpreter-based validators are too slow
- Validating notebook documents against the nbformat JSON Schema (its largest real-world deployment, via Jupyter)
- Enforcing config-file or data-pipeline schema conformance in CLI tools and data engineering scripts
- Any workload that validates the same schema many times and can benefit from a compiled, reusable validator
Under The Hood
Architecture: The core is a CodeGenerator (fastjsonschema/generator.py) shared by draft-specific subclasses (CodeGeneratorDraft04, Draft06, Draft07) that walk a JSON Schema document and emit Python source implementing each keyword’s validation logic, joined via an indent.py helper for readable generated code. A ref_resolver.py module resolves $ref pointers across the schema (including remote refs) before generation, and __init__.py exposes the public validate()/compile() entry points that tie generation, exec(), and caching together.
Tech Stack: Pure Python (3.3+) standard library only — no runtime dependencies — which keeps the package lightweight and easy to vendor; the JSON-Schema-Test-Suite git submodule provides the official cross-language JSON Schema conformance tests used in CI.
Code Quality: The tests/ directory runs the official JSON-Schema-Test-Suite plus custom tests per draft and per keyword, giving broad spec-conformance coverage; pylintrc and tox.ini wire up linting and multi-version testing. The README explicitly documents known deviations from strict JSON Schema semantics (e.g. Python regex dialect, $ anchor handling), which is a notably honest and useful practice for a validation library.
API Design: The public surface is deliberately tiny — validate(schema, data) for convenience and compile(schema) for performance-sensitive reuse — mirroring the ergonomics of Python’s re module (match-once vs. compile-and-reuse). Errors raise a single JsonSchemaException type carrying a human-readable message and the failing path, so callers don’t need to learn a large exception hierarchy.
Used by 4 apps in this directory
argilla
AI Development · Data Engineering
Collaborate on high-quality AI training data with a self-hosted annotation platform built for LLMs, NLP, and multimodal models.
Bugsink
Developer Tools · Monitoring
Self-hosted error tracking that accepts Sentry SDKs out of the box, so you keep your instrumentation and drop the monthly bill.
GrowthBook
Developer Tools · Analytics · Monitoring
Open source feature flags, A/B testing, and warehouse-native experimentation that queries your existing data infrastructure—no data movement required.
OSV.dev
Security
Google's open-source vulnerability database that maps CVEs to exact package versions across 50+ ecosystems with a public API and data dumps.