A Python library for automatically generating Pydantic v2 models from JSON Schema definitions
Project description
JSON Schema to Pydantic
A Python library for automatically generating Pydantic v2 models from JSON Schema definitions.
Features
- Converts JSON Schema to Pydantic v2 models
- Supports complex schema features including:
- References ($ref) with circular reference detection
- Combiners (allOf, anyOf, oneOf) with proper type discrimination
- Type constraints and validations
- Array and object validations
- Format validations (email, uri, uuid, date-time)
- Full type hinting support
- Clean, simple API
Installation
pip install json-schema-to-pydantic
Development Setup
- Clone the repository
- Install development dependencies:
# Using uv (recommended)
uv pip install -e ".[dev]"
# Or using pip
pip install -e ".[dev]"
- Run tests:
pytest
Quick Start
from json_schema_to_pydantic import create_model
# Define your JSON Schema
schema = {
"title": "User",
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string", "format": "email"},
"age": {"type": "integer", "minimum": 0}
},
"required": ["name", "email"]
}
# Generate your Pydantic model
UserModel = create_model(schema)
# Use the model
user = UserModel(
name="John Doe",
email="john@example.com",
age=30
)
# Example with relaxed array item validation
RelaxedModel = create_model(
{"type": "object", "properties": {"tags": {"type": "array"}}},
allow_undefined_array_items=True
)
relaxed_instance = RelaxedModel(tags=[1, "two", True])
Advanced Usage
For more complex scenarios, you can use the PydanticModelBuilder directly:
from json_schema_to_pydantic import PydanticModelBuilder
builder = PydanticModelBuilder()
model = builder.create_pydantic_model(schema, root_schema)
Error Handling
The library provides specific exceptions for different error cases:
from json_schema_to_pydantic import (
SchemaError, # Base class for all schema errors
TypeError, # Invalid or unsupported type
CombinerError, # Error in schema combiners
ReferenceError, # Error in schema references
)
try:
model = create_model(schema)
except TypeError as e:
print(f"Invalid type in schema: {e}")
except ReferenceError as e:
print(f"Invalid reference: {e}")
Documentation
See docs/features.md for detailed documentation of supported JSON Schema features.
Contributing
- Fork the repository
- Create a new branch for your feature
- Make your changes
- Run tests and ensure they pass
- Submit a pull request
License
This project is licensed under the terms of the license included in the repository.
Project details
Release history Release notifications | RSS feed
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 json_schema_to_pydantic-0.4.3.tar.gz.
File metadata
- Download URL: json_schema_to_pydantic-0.4.3.tar.gz
- Upload date:
- Size: 42.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6acb55fe573b64ef15649d243212309a039795bcf40d272b5bb79f9a8aab7f3f
|
|
| MD5 |
193c23261ff030e0cdeebcd5480f5d7c
|
|
| BLAKE2b-256 |
46d37d642bab174acc1d4bbd532fe78cd73f92c4df312953aff0ac875e2d54de
|
Provenance
The following attestation bundles were made for json_schema_to_pydantic-0.4.3.tar.gz:
Publisher:
publish.yaml on richard-gyiko/json-schema-to-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
json_schema_to_pydantic-0.4.3.tar.gz -
Subject digest:
6acb55fe573b64ef15649d243212309a039795bcf40d272b5bb79f9a8aab7f3f - Sigstore transparency entry: 582458329
- Sigstore integration time:
-
Permalink:
richard-gyiko/json-schema-to-pydantic@1636765af6c721e5ac411919e0bbe07e9f859a96 -
Branch / Tag:
refs/tags/v0.4.3 - Owner: https://github.com/richard-gyiko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@1636765af6c721e5ac411919e0bbe07e9f859a96 -
Trigger Event:
release
-
Statement type:
File details
Details for the file json_schema_to_pydantic-0.4.3-py3-none-any.whl.
File metadata
- Download URL: json_schema_to_pydantic-0.4.3-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f4b44446bffc27c922fc051b161ed0a758297674a0c41d0e74cc34d060bf1fd
|
|
| MD5 |
e5191158574a5e5029aa5a811a4a5ec3
|
|
| BLAKE2b-256 |
e676abac46ef31a693c344a9e387134baaafba3efef2fcab991696bd8364f591
|
Provenance
The following attestation bundles were made for json_schema_to_pydantic-0.4.3-py3-none-any.whl:
Publisher:
publish.yaml on richard-gyiko/json-schema-to-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
json_schema_to_pydantic-0.4.3-py3-none-any.whl -
Subject digest:
2f4b44446bffc27c922fc051b161ed0a758297674a0c41d0e74cc34d060bf1fd - Sigstore transparency entry: 582458333
- Sigstore integration time:
-
Permalink:
richard-gyiko/json-schema-to-pydantic@1636765af6c721e5ac411919e0bbe07e9f859a96 -
Branch / Tag:
refs/tags/v0.4.3 - Owner: https://github.com/richard-gyiko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@1636765af6c721e5ac411919e0bbe07e9f859a96 -
Trigger Event:
release
-
Statement type: