Convert JSON Schema definitions to Pydantic models dynamically at runtime.
Overview
jsonschema-pydantic-converter is a Python library that transforms JSON Schema dictionaries into Pydantic v2 models. This is useful when you need to work with dynamic schemas, validate data against JSON Schema specifications, or bridge JSON Schema-based systems with Pydantic-based applications.
Features
Dynamic Model Generation: Convert JSON Schema to Pydantic models at runtime
TypeAdapter Support: Generate TypeAdapters for enhanced validation and serialization
: Automatically handles properties that would conflict with Pydantic (e.g., , , , , ) by renaming them internally and using aliases to preserve original JSON names
Reserved Property Names
_hidden
schema
copy
validate
model_fields
Pydantic v2 Compatible: Built for Pydantic 2.0+
Installation
pip install jsonschema-pydantic-converter
Or using uv:
uv add jsonschema-pydantic-converter
Usage
Basic Example (using transform)
from jsonschema_pydantic_converter import transform
# Define a JSON Schema
schema = {
"type": "object",
"properties": {
"name": {"type": "string", "description": "User's name"},
"age": {"type": "integer", "description": "User's age"},
"email": {"type": "string"}
},
"required": ["name", "age"]
}
# Convert to Pydantic model
UserModel = transform(schema)
# Use the model
user = UserModel(name="John Doe", age=30, email="john@example.com")
print(user.model_dump())
# {'name': 'John Doe', 'age': 30, 'email': 'john@example.com'}
Using TypeAdapter for Validation
The create_type_adapter function returns a Pydantic TypeAdapter, which provides additional validation and serialization capabilities:
Properties that start with underscores or shadow Pydantic BaseModel attributes (like schema, copy, validate) are automatically renamed to safe Python identifiers, with aliases preserving the original JSON names:
from jsonschema_pydantic_converter import transform
schema = {
"type": "object",
"properties": {
"_file": {"type": "string"},
"schema": {"type": "string"},
"name": {"type": "string"},
},
"required": ["_file", "schema", "name"],
}
Model = transform(schema)
# Validate using original JSON property names
instance = Model.model_validate({"_file": "a.txt", "schema": "v1", "name": "test"})
# model_dump() outputs original names by default
print(instance.model_dump())
# {'_file': 'a.txt', 'schema': 'v1', 'name': 'test'}
# model_json_schema() also uses original names
print(Model.model_json_schema()["properties"].keys())
# dict_keys(['_file', 'schema', 'name'])
# Internal Python field names are accessible too
print(instance.file) # 'a.txt' (leading _ stripped)
print(instance.schema_) # 'v1' (trailing _ added to avoid collision)
print(instance.name) # 'test' (unchanged)
Note (Pydantic <2.11): The serialize_by_alias config option was introduced in Pydantic 2.11. On older versions, model_dump() returns internal field names by default. Use instance.model_dump(by_alias=True) explicitly to get the original JSON property names. A runtime warning is emitted at import time when running on Pydantic <2.11.
# Using uv
uv run pytest
# With coverage
uv run pytest --cov=src --cov-report=html
# Using pytest directly (if in activated venv)
pytest
Code Quality
The project uses several tools to maintain code quality:
# Type checking with mypy
uv run mypy src/
# Linting with ruff
uv run ruff check .
# Format code with ruff
uv run ruff format .
Contributing
Contributions are welcome! Here's how you can help:
Reporting Issues
Check existing issues before creating a new one
Provide a clear description of the problem
Include a minimal reproducible example
Specify your Python and Pydantic versions
Submitting Pull Requests
Fork the repository
Create a new branch for your feature/fix:
git checkout -b feature/your-feature-name
Make your changes and ensure:
All tests pass: uv run pytest
Code is properly formatted: uv run ruff format .
No linting errors: uv run ruff check .
Type checking passes: uv run mypy src/
Add tests for new functionality
Update documentation if needed
Commit your changes with clear commit messages
Push to your fork and submit a pull request
Code Style
Follow PEP 8 guidelines
Use Google-style docstrings
Type hints are required for all functions
Line length: 88 characters (Black/Ruff default)
Development Guidelines
Write tests for all new features
Maintain backwards compatibility when possible
Update the README for user-facing changes
Keep dependencies minimal
Limitations
Optional fields without defaults are set to None rather than using Optional[T] type annotation to maintain JSON Schema round-trip consistency
When allOf contains $ref references, the generated json_schema() output may not preserve the exact original structure (validation still works correctly)
Some advanced JSON Schema features are not yet supported:
$anchor references (causes syntax errors with forward references)