{"id":8248,"library":"jsonschema-pydantic-converter","title":"JSON Schema Pydantic Converter","description":"This library dynamically converts JSON Schema definitions into Pydantic models at runtime. It currently supports both Pydantic v1 and v2. The latest version is 0.4.0, with development actively progressing through minor releases.","status":"active","version":"0.4.0","language":"en","source_language":"en","source_url":"https://github.com/akshaylive/jsonschema-pydantic-converter","tags":["json-schema","pydantic","conversion","codegen","runtime","schema-to-model"],"install":[{"cmd":"pip install jsonschema-pydantic-converter","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for generating and validating models.","package":"pydantic","optional":false},{"reason":"Core dependency for parsing and validating JSON schemas.","package":"jsonschema","optional":false}],"imports":[{"note":"The primary conversion function is nested within the `jsonschema_pydantic.builder` submodule, not directly under the top-level package.","wrong":"from jsonschema_pydantic_converter import build_pydantic_model","symbol":"build_pydantic_model","correct":"from jsonschema_pydantic.builder import build_pydantic_model"}],"quickstart":{"code":"from jsonschema_pydantic.builder import build_pydantic_model\n\n# Define a JSON Schema\nschema = {\n    \"title\": \"User\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"name\": {\"type\": \"string\", \"minLength\": 1},\n        \"age\": {\"type\": \"integer\", \"minimum\": 0, \"maximum\": 150},\n        \"email\": {\"type\": \"string\", \"format\": \"email\"}\n    },\n    \"required\": [\"name\", \"age\"]\n}\n\n# Build the Pydantic model from the schema\nUserModel = build_pydantic_model(schema_data=schema)\n\n# Instantiate and use the generated model\ntry:\n    user_data = UserModel(name=\"Alice\", age=30, email=\"alice@example.com\")\n    print(\"Valid user data:\")\n    print(user_data.model_dump_json(indent=2))\nexcept Exception as e:\n    print(f\"Error creating valid user: {e}\")\n\n# Demonstrate validation failure\ntry:\n    invalid_user_data = UserModel(name=\"\", age=-5)\n    print(\"Invalid user data (should not reach here):\", invalid_user_data)\nexcept Exception as e:\n    print(\"\\nValidation error for invalid user data:\")\n    print(e)","lang":"python","description":"This quickstart demonstrates how to define a simple JSON Schema, use `build_pydantic_model` to generate a Pydantic model dynamically, and then instantiate and validate data against the newly created model. It also shows an example of a validation failure."},"warnings":[{"fix":"Always test generated models with your target Pydantic version. For Pydantic v1, use `.dict()` and `.json()`. For Pydantic v2, use `.model_dump()` and `.model_dump_json()`.","message":"While the library supports both Pydantic v1 and v2, the behavior of the *generated* models (e.g., method names like `.dict()` vs `.model_dump()`, specific validator implementations) can differ. Ensure your application's Pydantic version aligns with the expected model behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Validate your JSON Schema externally (e.g., using `jsonschema.validate`) before passing it to `build_pydantic_model`. Refer to the official JSON Schema specification for valid syntax and keywords.","message":"The library relies on well-formed and compliant JSON Schema definitions. Malformed schemas, unsupported keywords, or custom extensions not handled by the library can lead to conversion errors or unexpected Pydantic model structures.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Simplify complex `$ref` structures where possible. Ensure all referenced schemas are accessible and well-defined. For external `$ref`s, ensure the converter's environment can resolve them (though current versions focus on in-schema refs).","message":"Schemas with deeply nested or complex `$ref` references, especially those involving circular dependencies or external files, might encounter resolution issues or performance degradation.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install the package using `pip install jsonschema-pydantic-converter`.","cause":"The `jsonschema-pydantic-converter` package is either not installed or not accessible in the current Python environment.","error":"ModuleNotFoundError: No module named 'jsonschema_pydantic'"},{"fix":"Review the `schema_data` against JSON Schema specification and library's capabilities. Debug the schema's structure and ensure all types and formats are correctly defined.","cause":"The input `schema_data` dictionary is not a valid JSON Schema or contains constructs that the converter cannot process into a Pydantic model.","error":"pydantic.error_wrappers.ValidationError: ... (for Pydantic v1) OR pydantic_core._pydantic_core.ValidationError: ... (for Pydantic v2) occurring during `build_pydantic_model` call."},{"fix":"If Pydantic v1 is intended, use `.dict()` and `.json()` instead. If Pydantic v2 is required, ensure `pydantic>=2.0` is installed in your environment.","cause":"This typically happens when you are using Pydantic v1 (or an environment with v1 installed) but attempting to call Pydantic v2 methods like `model_dump_json` or `model_dump`.","error":"AttributeError: 'GeneratedModel' object has no attribute 'model_dump_json'"}]}