{"id":7328,"library":"jsonschema-pydantic","title":"JSON Schema to Pydantic Model Converter","description":"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.","status":"active","version":"0.6","language":"en","source_language":"en","source_url":"https://github.com/kreneskyp/jsonschema-pydantic/","tags":["pydantic","json-schema","code-generation","validation","serialization","schema-conversion"],"install":[{"cmd":"pip install jsonschema-pydantic","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for generated models and internal logic. The library aims for compatibility with both Pydantic v1 and v2.","package":"pydantic","optional":false}],"imports":[{"symbol":"jsonschema_to_pydantic","correct":"from jsonschema_pydantic import jsonschema_to_pydantic"}],"quickstart":{"code":"from jsonschema_pydantic import jsonschema_to_pydantic\n\njson_schema = {\n    \"type\": \"object\",\n    \"properties\": {\n        \"name\": {\"type\": \"string\", \"description\": \"The name of the user\"},\n        \"age\": {\"type\": \"integer\", \"minimum\": 0},\n        \"email\": {\"type\": \"string\", \"format\": \"email\"}\n    },\n    \"required\": [\"name\", \"age\"]\n}\n\n# Convert the JSON Schema to a Pydantic model\nUserModel = jsonschema_to_pydantic(json_schema, model_name='User')\n\n# Instantiate and validate the generated model\ntry:\n    user_data = UserModel(name=\"Alice\", age=30, email=\"alice@example.com\")\n    print(f\"Successfully created user: {user_data.model_dump_json(indent=2)}\")\n\n    # Example of validation error\n    invalid_user_data = UserModel(name=\"Bob\", age=-5) # age < minimum\nexcept Exception as e:\n    print(f\"Validation error: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"Pydantic V1 vs V2 Compatibility: While jsonschema-pydantic v0.5.0 introduced backward compatibility for Pydantic V1, Pydantic V2 itself has significant breaking changes. Ensure the Pydantic version installed in your environment matches the expected version for your generated models to avoid runtime issues.","severity":"breaking","affected_versions":"<0.5.0"},{"fix":"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}`.","message":"JSON Schema 'null' type conversion to Python 'None': As of v0.4.0, the library converts 'null' type in JSON Schema properties to Python's `None` type annotation. Older versions might have handled this differently, potentially leading to discrepancies if relying on specific default behaviors.","severity":"gotcha","affected_versions":"<0.4.0"},{"fix":"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.","message":"Schema and Field Descriptions/Titles: Version 0.6.0 improved the incorporation of JSON schema and field descriptions into the generated Pydantic models. For older versions, detailed descriptions or titles might have been ignored, impacting documentation or schema generation fidelity.","severity":"gotcha","affected_versions":"<0.6.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"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.","cause":"This error typically arises in Pydantic V2 when a Pydantic dataclass (or a BaseModel behaving like one) is configured to allow extra fields but also has fields set with `init=False`. This is a Pydantic-level restriction, which can surface if the `jsonschema-pydantic` generated model's configuration implicitly creates this conflict.","error":"pydantic.errors.PydanticUserError: Field ... has `init=False` and dataclass has config setting `extra=\"allow\"`. This combination is not allowed."},{"fix":"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.","cause":"This error indicates that a type annotation within the generated Pydantic model could not be resolved. This often occurs with circular references in the JSON schema or when a type is referenced before it's fully defined, particularly in complex nested schemas or self-referential structures.","error":"pydantic.errors.PydanticUndefinedAnnotation: Name '...' is not defined"},{"fix":"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.","cause":"The data provided for validation does not conform to the rules defined in the generated Pydantic model (and thus, the original JSON Schema). Common causes include missing required fields, incorrect data types, or values outside specified constraints (e.g., `minimum`, `maximum`).","error":"pydantic.ValidationError: 1 validation error for UserModel..."}]}