Getting Started¶
Install datamodel-code-generator and generate your first Pydantic v2 model.
Installation¶
Use uv tool install when you want datamodel-codegen available as a standalone CLI. Use uv add --dev when a project
or CI workflow should pin the generator version in its lockfile.
Default output model
When --output-model-type is omitted, datamodel-code-generator generates Pydantic v2 BaseModel output
(pydantic_v2.BaseModel). You can pass --output-model-type explicitly when you want another model family.
Quick Start¶
Command¶
datamodel-codegen \
--input schema.json \
--input-file-type jsonschema \
--output-model-type pydantic_v2.BaseModel \
--preset standard-py312-20260619 \
--output model.py
This quick start uses standard-py312-20260619 as the modern Python 3.12 baseline.
Preset names include the target Python version: py312 means Python 3.12.
See CLI Reference for all options. See Presets,
--preset, --input-file-type, and
--output-model-type for this command.
For more schema-aware output that preserves schema-authored names, reuses models, and embeds generated
documentation, use practical-py312-20260619.
Input (schema.json)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Pet",
"type": "object",
"required": ["name"],
"properties": {
"name": {
"type": "string",
"description": "The pet's name"
},
"species": {
"type": "string",
"enum": ["dog", "cat", "bird", "fish"],
"default": "dog"
},
"age": {
"type": "integer",
"minimum": 0,
"description": "Age in years"
},
"vaccinated": {
"type": "boolean",
"default": false
}
}
}
Output¶
# generated by datamodel-codegen:
# filename: schema.json
from __future__ import annotations
from enum import StrEnum
from typing import Annotated
from pydantic import BaseModel, ConfigDict, Field
class Species(StrEnum):
dog = 'dog'
cat = 'cat'
bird = 'bird'
fish = 'fish'
class Pet(BaseModel):
model_config = ConfigDict(
populate_by_name=True,
)
name: Annotated[str, Field(description="The pet's name")]
species: Species = Species.dog
age: Annotated[int | None, Field(description='Age in years', ge=0)] = None
vaccinated: bool = False
🎉 That's it! Your schema is now a fully-typed Python model.
âš¡ Speed up generation¶
By default, generated Python is currently formatted with black and isort. For faster generation without external
formatter dependencies, add --formatters builtin for standard generated model modules. In a future version, the
Black/isort dependencies will become opt-in and the default formatter will change to builtin.
If you prefer Ruff, install it with pip install 'datamodel-code-generator[ruff]' and use
--formatters ruff-check ruff-format for a fast external formatter.
Custom templates can emit Python outside the standard generated model patterns covered by builtin, so
custom-template output is not exhaustively validated. If --formatters builtin produces invalid or poorly formatted
output with a custom template, please open an issue with a small reproducer. See
Formatter Behavior for details.
See Performance Benchmarks for release benchmark data and interactive charts.