Datamodel Code Generator is a Python library and command-line utility for generating data models from various structured input formats like OpenAPI, JSON Schema, GraphQL, and raw data (JSON/YAML/CSV). It supports outputting models for Pydantic v2, dataclasses, TypedDict, and msgspec. Currently at version 0.56.0, it maintains an active development pace with frequent releases addressing new features and breaking changes, particularly around Pydantic compatibility.
pip install datamodel-code-generatorVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to programmatically generate Pydantic v2 models from a JSON Schema string. The `generate` function takes the schema content and a `GenerateConfig` object to specify input/output types.
Ensure Pydantic v2 is installed (`pip install pydantic>=2`) in your environment. Update any existing generated models to Pydantic v2 standards if they were generated with `--output-model-type pydantic.BaseModel` (which is now deprecated in favor of `pydantic_v2.BaseModel`).
Review your generated models and code that interacts with default values. While often a cosmetic change, if you had custom logic around `default_factory`, you might need to adapt it.
Always explicitly specify the output model type. For new projects, `--output-model-type pydantic_v2.BaseModel` (or `DataModelType.PydanticV2BaseModel`) is recommended.
If you rely on specific enum member names for `oneOf`/`anyOf` constructs, check your schema definitions. Ensure `title` fields are accurate or adjust your code to the new naming convention.
To use `GenerateConfig`, update `datamodel-code-generator` to version 0.53.0 or newer. If you need to remain on an older version, configure the generator using direct function arguments instead of `GenerateConfig`.
To prepare for this change, consider using `formatters=[Formatter.RUFF_FORMAT, Formatter.RUFF_CHECK]` and install ruff with `pip install 'datamodel-code-generator[ruff]'`. To suppress this warning or retain the current formatting behavior, explicitly specify your desired formatters (e.g., `formatters=[Formatter.BLACK, Formatter.ISORT]`).
Update your code to use `YourModel.model_validate(data)` instead of `YourModel.parse_obj(data)`. Similarly, replace `dict()` with `model_dump()` and `__fields_set__` with `model_fields_set`.
Ensure Pydantic is installed by running `pip install pydantic` in your active Python environment. If it persists, check for any user-created files named `pydantic.py` that could shadow the actual library.
Align your Pydantic version with your code: if using Pydantic v2, ensure your code imports `@field_validator` or `@model_validator` and that `datamodel-code-generator` outputs `pydantic_v2.BaseModel`. If you require Pydantic v1, use `@validator` and target `pydantic.BaseModel` in the generator options.
Review your custom Jinja2 templates and the `--extra-template-data` JSON/YAML file. Ensure you access dictionary elements using bracket or dot notation (e.g., `{{ my_dict.key }}` or `{{ my_dict['key'] }}`) rather than attempting to call them like a function (e.g., `{{ my_dict() }}`).