Pydantic turns models into JSON Schema. This library does the reverse — it turns a JSON Schema into a Pydantic model, so you can validate data against a schema you already have, with all of Pydantic's runtime checks and editor support.
Reach for it when the schema comes first: API contracts, config files, tool definitions, or validating LLM output against a fixed shape.
uv add pydantic-jsonschemaRequires Python 3.12+. See the installation guide for optional validator libraries.
from pydantic_jsonschema import Schema, to_model
schema = Schema.model_validate({
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0},
},
"required": ["name"],
})
User = to_model(schema, model_name="User")
user = User(name="Alice", age=30)
print(user.model_dump())
#> {'name': 'Alice', 'age': 30}Three building blocks — and a fourth on the way.
A Pydantic model for JSON Schema itself: parse, inspect, and serialize schemas with full
type safety. $refs are parsed as Reference objects and resolved during conversion.
from pydantic_jsonschema import DataType, Schema
schema = Schema(
type=DataType.OBJECT,
properties={"name": Schema(type=DataType.STRING)},
required=["name"],
)
print(schema.model_dump_json())
#> {"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}Turns a Schema into a Pydantic model (see Quick start). It resolves
$ref / $defs, maps anyOf / oneOf (including discriminated unions) / allOf to
Python types, and applies string, number, array, and object constraints as Pydantic
validation.
Built-in types for every format in the JSON Schema spec (email, uri, uuid,
date-time, hostname, json-pointer, regex, and more) — with zero extra
dependencies. Map your own Pydantic type for custom formats.
from pydantic_jsonschema import Schema, to_model
from pydantic_jsonschema.formats import Email
schema = Schema.model_validate({
"type": "object",
"properties": {"email": {"type": "string", "format": "email"}},
"required": ["email"],
})
User = to_model(schema, formats={"email": Email})
print(User(email="alice@example.com").email)
#> alice@example.comPer-object control over how each type is loaded, inspired by adaptix.
https://danipulok.github.io/pydantic-jsonschema/
MIT License - see LICENSE for details.