Skip to main content

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.

PyPI - Version PyPI - Downloads codecov

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

  1. Clone the repository
  2. Install development dependencies:
# Using uv (recommended)
uv pip install -e ".[dev]"

# Or using pip
pip install -e ".[dev]"
  1. 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 validation
RelaxedModel = create_model(
    {
        "type": "object",
        "properties": {
            "tags": {"type": "array"},  # Array without items schema
            "metadata": {}  # Field without type
        }
    },
    allow_undefined_array_items=True,  # Allows arrays without items schema
    allow_undefined_type=True  # Allows fields without type (defaults to Any)
)
relaxed_instance = RelaxedModel(
    tags=[1, "two", True],
    metadata={"custom": "data"}
)

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

  1. Fork the repository
  2. Create a new branch for your feature
  3. Make your changes
  4. Run tests and ensure they pass
  5. Submit a pull request

License

This project is licensed under the terms of the license included in the repository.

Project details


Download files

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

Source Distribution

json_schema_to_pydantic-0.4.7.tar.gz (50.1 kB view details)

Uploaded Source

Built Distribution

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

json_schema_to_pydantic-0.4.7-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file json_schema_to_pydantic-0.4.7.tar.gz.

File metadata

  • Download URL: json_schema_to_pydantic-0.4.7.tar.gz
  • Upload date:
  • Size: 50.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for json_schema_to_pydantic-0.4.7.tar.gz
Algorithm Hash digest
SHA256 a6384825fee7609715641a1e5095ddf41aa3bdd06cd91d4a6b6d88a3dfcad920
MD5 6154ccad25c59fddbc49b755350aaa35
BLAKE2b-256 e6b1fbcbda5e23ecb5ff987076d9caccb11d182a249cedd5424236aaca4e65b8

See more details on using hashes here.

Provenance

The following attestation bundles were made for json_schema_to_pydantic-0.4.7.tar.gz:

Publisher: publish.yaml on richard-gyiko/json-schema-to-pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file json_schema_to_pydantic-0.4.7-py3-none-any.whl.

File metadata

File hashes

Hashes for json_schema_to_pydantic-0.4.7-py3-none-any.whl
Algorithm Hash digest
SHA256 e329506e42b63f9a0ae0a17f7082ec7fc7cb3326138ec5c24904e0af5a660e63
MD5 5f9fbcd29bfa49e68d56a2bda83f6767
BLAKE2b-256 b2aff64664df8d4d52a371195e16c6ff4419968ee3eb6f96b21e596e3623dcf3

See more details on using hashes here.

Provenance

The following attestation bundles were made for json_schema_to_pydantic-0.4.7-py3-none-any.whl:

Publisher: publish.yaml on richard-gyiko/json-schema-to-pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page