jsonschema-pydantic is a Python library that programmatically converts JSON Schemas into Pydantic models, enabling strong data validation and serialization based on schema definitions. Currently at version 0.6, it is actively developed with frequent minor releases to enhance schema feature support and Pydantic version compatibility.
pip install jsonschema-pydanticVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to define a JSON Schema, convert it into a Pydantic model using `jsonschema_to_pydantic`, and then use the generated model for data validation and instantiation. This example also shows how schema properties like 'description' and 'minimum' are translated to Pydantic model fields and validations.
Upgrade jsonschema-pydantic to v0.5.0+ and ensure your Pydantic installation (v1 or v2) aligns with your project's requirements. Review Pydantic's official migration guide if transitioning from Pydantic V1 to V2.
Update to jsonschema-pydantic v0.4.0 or newer. Explicitly define defaults in your JSON schema for nullable fields if precise behavior is required, e.g., `{'type': ['string', 'null'], 'default': None}`.Upgrade to jsonschema-pydantic v0.6.0+ to ensure `description` and `title` fields from your JSON schema are properly reflected in the generated Pydantic models.
Review the JSON schema definition for properties that might lead to `init=False` (e.g., complex default factories or computed fields) in conjunction with any `extra_properties` or `additionalProperties` settings in the schema. Adjust the JSON schema or manually modify the generated Pydantic model (if applicable) to remove this conflict.
For complex nested or circular schemas, Pydantic might require explicit forward references or a `model_rebuild()` call. While `jsonschema-pydantic` handles common cases, for very intricate schemas, you might need to simplify the JSON schema or manually adjust the generated model to include `from typing import ForwardRef` and `Model.model_rebuild()` after all class definitions.
Carefully examine the `ValidationError` message, specifically the `loc` (location) and `msg` (message) fields, to identify which part of your input data violates the schema. Adjust your input data to match the expected structure and constraints defined by the JSON schema.