{"id":5980,"library":"json-schema-to-pydantic","title":"JSON Schema to Pydantic Model Generator","description":"json-schema-to-pydantic is a Python library designed to automatically generate Pydantic v2 models from JSON Schema definitions. It supports complex schema features like references, combiners (allOf, anyOf, oneOf), type constraints, and format validations. The library is actively maintained, with regular updates and improvements, currently at version 0.4.11.","status":"active","version":"0.4.11","language":"en","source_language":"en","source_url":"https://github.com/richard-gyiko/json-schema-to-pydantic","tags":["json-schema","pydantic","code-generation","validation","openapi"],"install":[{"cmd":"pip install json-schema-to-pydantic","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"The library generates models for Pydantic v2. Requires Pydantic >=2.0.","package":"pydantic","optional":false}],"imports":[{"symbol":"create_model","correct":"from json_schema_to_pydantic import create_model"}],"quickstart":{"code":"from json_schema_to_pydantic import create_model\n\n# Define your JSON Schema\nschema = {\n    \"title\": \"User\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"name\": {\"type\": \"string\"},\n        \"email\": {\"type\": \"string\", \"format\": \"email\"},\n        \"age\": {\"type\": \"integer\", \"minimum\": 0}\n    },\n    \"required\": [\"name\", \"email\"]\n}\n\n# Generate your Pydantic model\nUserModel = create_model(schema)\n\n# Use the model\nuser_data = {\"name\": \"John Doe\", \"email\": \"john@example.com\", \"age\": 30}\nuser = UserModel(**user_data)\n\nprint(user.model_dump_json(indent=2))\n# Expected output (approx):\n# {\n#   \"name\": \"John Doe\",\n#   \"email\": \"john@example.com\",\n#   \"age\": 30\n# }","lang":"python","description":"This quickstart demonstrates how to define a basic JSON Schema and use `create_model` to generate a Pydantic v2 model. It then shows how to instantiate and use the generated model for data validation and serialization."},"warnings":[{"fix":"Ensure Pydantic v2 (or higher) is installed in your environment. If you are locked into Pydantic v1, consider using 'datamodel-code-generator' or manually creating Pydantic v1 models.","message":"This library is designed exclusively for Pydantic v2 and is not compatible with Pydantic v1 models or schemas. Attempting to use it with Pydantic v1 environments or schemas may lead to unexpected errors.","severity":"breaking","affected_versions":"<0.4.5 (Pydantic <3.9 required), All versions (Pydantic v1)"},{"fix":"For versions 0.4.9 and later, pass `populate_by_name=True` to `create_model` to automatically sanitize these field names and create aliases, allowing population by the original name. Example: `UserModel = create_model(schema, populate_by_name=True)`.","message":"JSON Schema fields starting with underscores (e.g., `_links`, `_embedded`) are common in OpenAPI specifications but are not valid Pydantic field names. By default, these fields might be dropped or cause issues.","severity":"gotcha","affected_versions":"<0.4.9 (fields dropped/errors), >=0.4.9 (requires parameter)"},{"fix":"Versions 0.4.7 and later support top-level array schemas using Pydantic's `RootModel`. Ensure you are on a compatible version. The library handles this automatically for you.","message":"If your JSON Schema defines a top-level array or scalar type (not an object), directly mapping it to a `BaseModel` might not work as expected in Pydantic v2 without `RootModel`.","severity":"gotcha","affected_versions":"<0.4.7"},{"fix":"Upgrade to version 0.4.8 or later, which includes fixes for `oneOf` handling. Thoroughly test schemas with complex `oneOf` structures after upgrading.","message":"Schemas with `oneOf` combiners, especially for simple types or references, can lead to incorrect type discrimination or validation issues in older versions.","severity":"gotcha","affected_versions":"<0.4.8"},{"fix":"Use the `allow_undefined_array_items=True` and/or `allow_undefined_type=True` parameters in `create_model` to relax validation and allow `Any` for such fields. Example: `RelaxedModel = create_model(schema, allow_undefined_array_items=True, allow_undefined_type=True)`.","message":"When converting loosely defined JSON schemas (e.g., arrays without `items` schema or fields without a `type`), the generated Pydantic models might be too strict or fail during creation.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}