Registry / serialization / datamodel-code-generator

datamodel-code-generator

JSON →
library0.75.1pypypi✓ verified 27d ago

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-generator
INSTALL
IMPORT
SIG · DATAMODEL-CODE-GEN
D
datamodel-code-generator
serializationpythonv0.75.1
Install
5.5s avg
Import
138ms
Disk
49MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.75.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.915 runs
installs and imports cleanly · install 0.0s · import 0.145s · 47.1MB
glibc
py 3.103.915 runs
installs and imports cleanly · install 5.5s · import 0.131s · 52MB
49MB installed
● package 49MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

generate
from datamodel_code_generator import generate
GenerateConfig
from datamodel_code_generator import GenerateConfig
InputFileType
from datamodel_code_generator import InputFileType
DataModelType
from datamodel_code_generator import DataModelType

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.

from datamodel_code_generator import InputFileType, generate, GenerateConfig, DataModelType json_schema: str = """{ "type": "object", "properties": { "number": {"type": "number"}, "street_name": {"type": "string"}, "street_type": {"type": "string", "enum": ["Street", "Avenue", "Boulevard"]} } }""" config = GenerateConfig( input_file_type=InputFileType.JsonSchema, input_filename="example.json", output_model_type=DataModelType.PydanticV2BaseModel, ) result = generate(json_schema, config=config) print(result)
datamodel-codegen --version
Debug
Known issues
breakingPydantic v1 runtime support has been completely removed in version 0.55.0. All generated models and internal operations now require Pydantic v2 or newer. Attempting to use the generator or its output with Pydantic v1 will result in errors.
fix
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`).
affects: >=0.55.0
breakingAs of version 0.56.0, the generated syntax for fields with structured defaults (dicts, lists, model references) has changed. They now use `Field(default_value, validate_default=True)` instead of `default_factory=lambda: ...`. This change simplifies the output but may require adjustments if you were relying on the previous factory-based default initialization behavior.
fix
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.
affects: >=0.56.0
deprecatedOmitting the `--output-model-type` option when using the CLI (or `output_model_type` in `GenerateConfig`) has been deprecated since version 0.53.0. The default output model type switched from Pydantic v1 to Pydantic v2 in 0.55.0.
fix
Always explicitly specify the output model type. For new projects, `--output-model-type pydantic_v2.BaseModel` (or `DataModelType.PydanticV2BaseModel`) is recommended.
affects: >=0.53.0
breakingIn version 0.54.0, enum member names derived from `oneOf`/`anyOf` const constructs now correctly utilize the `title` field from the schema. Previously, `title` was ignored, and names were generated as `{type}_{value}`. This can change generated enum member names.
fix
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.
affects: >=0.54.0
breakingThe `GenerateConfig` class was introduced in version 0.53.0. Attempting to import `GenerateConfig` from `datamodel_code_generator` in versions prior to 0.53.0 will result in an `ImportError` because it did not exist.
fix
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`.
affects: <0.53.0
gotchaThe default formatters (black, isort) will be replaced by ruff in a future version. This change can affect the formatting of generated code.
fix
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]`).
affects: >=0.56.0
Errors
Common errors & fixes
PydanticDeprecatedSince20: The parse_obj method is deprecated; use model_validate instead.
This warning indicates that code generated with or for Pydantic v1 is being used with Pydantic v2, where the `parse_obj` method on BaseModel has been replaced by `model_validate`.
fix
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`.
ModuleNotFoundError: No module named 'pydantic'
The Pydantic library, a fundamental dependency for `datamodel-code-generator` and its output, is not installed in your current Python environment or a local file named 'pydantic.py' is causing an import conflict.
fix
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.
ImportError: cannot import name 'field_validator' from 'pydantic'
This error occurs when you try to import or use Pydantic v2's `@field_validator` decorator in an environment where Pydantic v1 is installed, or conversely, using `@validator` (from v1) when Pydantic v2 is active without the `pydantic.v1` alias.
fix
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.
TypeError: 'dict' object is not callable
This typically happens when using custom Jinja2 templates with `--extra-template-data`, where a dictionary value within the template data is mistakenly treated as a function call.
fix
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() }}`).
Upgrade
Version history
0.75.1latest on PyPI · released Aug 24, 2026
Audit
Dependencies
pydanticrequiredRequired runtime dependency for generated models, Pydantic v2+ is mandatory since 0.55.0.
Agent activity
33 hits · last 30 days
node
30
OpenAI (training)
1
Resources
datamodel-code-generator — pip install datamodel-code-generator · libregistry