datamodel-code-generator 0.71.0


pip install datamodel-code-generator

  Latest version

Released: Jul 24, 2026

Project Links

Meta
Author: Koudai Aono
Requires Python: >=3.10

Classifiers

Development Status
  • 4 - Beta

License
  • OSI Approved :: MIT License

Natural Language
  • English

Programming Language
  • Python :: 3 :: Only
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13
  • Python :: 3.14
  • Python :: Implementation :: CPython

datamodel-code-generator

πŸš€ Generate Python data models from schema definitions in seconds.

πŸ“š Documentation Β· πŸ§ͺ Playground Β· πŸ’Ό Lead maintainer available for work

[!NOTE] Playground privacy: Generation runs locally in your browser with Pyodide. Your schema and options are not sent to a backend. Shared repro URLs encode them in the URL fragment (#state=...), which browsers do not send to the server; the full URL can still be stored in your browser history or wherever you share it.

PyPI version Conda-forge Downloads PyPI - Python Version codecov license Pydantic v2

✨ What it does

Schema files, raw data, and existing Python models flow through datamodel-code-generator into Python model output types

Pick any one of the supported inputs and pick the Python model style you want as output. --input-model path/to/file.py:ClassName can even retarget an existing Pydantic, dataclass, or TypedDict class defined in another Python file to a different output type.

  • πŸ“„ Converts OpenAPI 3, AsyncAPI, JSON Schema, Apache Avro, XML Schema, Protocol Buffers/gRPC, GraphQL, MCP tool schemas, and raw data (JSON/YAML/CSV) into Python models
  • 🐍 Generates from existing Python types (Pydantic, dataclass, TypedDict) via --input-model
  • 🎯 Generates Pydantic v2, Pydantic v2 dataclass, dataclasses, TypedDict, or msgspec output
  • πŸ”— Handles complex schemas: $ref, allOf, oneOf, anyOf, enums, and nested types
  • βœ… Produces type-safe, validated code ready for your IDE and type checker

πŸ“¦ Installation

Recommended for standalone CLI use:

uv tool install datamodel-code-generator

Conda users can install from conda-forge:

conda install -c conda-forge datamodel-code-generator

For projects that should pin the generator version, add it as a development dependency instead:

uv add --dev datamodel-code-generator

[!NOTE] Community-maintained distribution packages are also available from Debian, Ubuntu, nixpkgs, and openSUSE Tumbleweed. Availability and versions vary by distribution.

Other installation methods

pip:

pip install datamodel-code-generator

uv (run without adding to project):

uv run --with datamodel-code-generator datamodel-codegen --help

With HTTP support (for resolving remote $ref):

pip install 'datamodel-code-generator[http]'

With GraphQL support:

pip install 'datamodel-code-generator[graphql]'

With Protocol Buffers support:

pip install 'datamodel-code-generator[protobuf]'

Docker:

docker pull koxudaxi/datamodel-code-generator

Published Docker images run as a non-root appuser. When writing generated files to a bind-mounted directory, make sure the directory is writable by the container user or pass an explicit Docker user, for example --user "$(id -u):$(id -g)".


πŸƒ 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 (model.py)

# 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

⚑ 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.


πŸ“– Documentation

πŸ‘‰ Read the full documentation β†’


πŸ“₯ Supported Input

  • OpenAPI 3 (YAML/JSON)
  • AsyncAPI (YAML/JSON)
  • JSON Schema
  • MCP tool schemas
  • XML Schema (XSD)
  • Protocol Buffers / gRPC (.proto)
  • Apache Avro schema (AVSC)
  • JSON data
  • YAML data
  • Python dictionary
  • CSV data
  • GraphQL schema
  • Python types (Pydantic, dataclass, TypedDict) via --input-model

πŸ“€ Supported Output

βœ… Conformance Signals

CI exercises datamodel-code-generator against pinned external corpora for XML Schema, JSON Schema, AsyncAPI, Apache Avro, and Protocol Buffers. See the Conformance Dashboard for the generated summary of runner scripts, tox environments, CI jobs, expected corpus counts, and upstream sources.


🍳 Common Recipes

CLI option quick starts

Use these starting points when combining options; each option links to the generated CLI reference for details and examples.

See the CLI Reference for the full option list and category-specific recipes.

πŸ€– Get CLI Help from LLMs

Generate a prompt to ask LLMs about CLI options:

datamodel-codegen --generate-prompt "Best options for Pydantic v2?" | claude -p

See LLM Integration for more examples.

🌐 Generate from URL

pip install 'datamodel-code-generator[http]'
datamodel-codegen --url https://example.com/api/openapi.yaml --output model.py

βš™οΈ Use with pyproject.toml

[tool.datamodel-codegen]
input = "schema.yaml"
output = "src/models.py"
output-model-type = "pydantic_v2.BaseModel"

Then simply run:

datamodel-codegen

See pyproject.toml Configuration for more options.

πŸ”„ CI/CD Integration

Validate generated models in your CI pipeline:

# Replace vX.Y.Z with a released action version.
- uses: koxudaxi/datamodel-code-generator@vX.Y.Z
  with:
    input: schemas/api.yaml
    output: src/models/api.py

See CI/CD Integration for more options.


Coding agent skill

This repository includes an experimental Agent Skill that teaches compatible coding agents to run datamodel-codegen when generating Python models from OpenAPI, AsyncAPI, JSON Schema, GraphQL, JSON/YAML/CSV sample data, MCP tool schemas, Protocol Buffers, XML Schema, Apache Avro, or existing Python model objects.

See Coding Agent Skill for detailed guidance and troubleshooting.

Install the directory for your agent:

# Codex, project-local
mkdir -p .agents/skills
cp -R skills/datamodel-code-generator .agents/skills/datamodel-code-generator

# Claude Code, project-local
mkdir -p .claude/skills
cp -R skills/datamodel-code-generator .claude/skills/datamodel-code-generator

For a personal install, copy the same directory to $HOME/.agents/skills/datamodel-code-generator/ for Codex or ~/.claude/skills/datamodel-code-generator/ for Claude Code.

Check your agent's current documentation for exact search paths.


πŸ’– Sponsors

Astral Logo

Astral

OpenAI Logo

OpenAI


🏒 Projects that use datamodel-code-generator

These public examples are grouped by how each project uses datamodel-code-generator.

Code generation and runtime integration

Development, testing, and evaluation

See all dependents β†’


πŸ”— Related Projects


🀝 Contributing

See Development & Contributing for how to get started!


πŸ‘₯ Maintainers


πŸ“„ License

MIT License - see LICENSE for details.

0.71.0 Jul 24, 2026
0.70.0 Jul 23, 2026
0.69.0 Jul 19, 2026
0.68.1 Jul 08, 2026
0.68.0 Jul 06, 2026
0.67.0 Jul 03, 2026
0.66.3 Jul 01, 2026
0.66.2 Jul 01, 2026
0.66.1 Jun 30, 2026
0.66.0 Jun 26, 2026
0.65.1 Jun 25, 2026
0.65.0 Jun 21, 2026
0.64.1 Jun 19, 2026
0.64.0 Jun 14, 2026
0.63.0 Jun 12, 2026
0.62.0 Jun 10, 2026
0.61.0 Jun 08, 2026
0.60.2 Jun 08, 2026
0.60.1 Jun 07, 2026
0.60.0 Jun 04, 2026
0.59.1 Jun 03, 2026
0.59.0 May 29, 2026
0.58.0 May 25, 2026
0.57.0 May 07, 2026
0.56.1 Apr 16, 2026
0.56.0 Apr 04, 2026
0.55.0 Mar 10, 2026
0.54.1 Mar 04, 2026
0.54.0 Feb 14, 2026
0.53.0 Jan 12, 2026
0.52.2 Jan 05, 2026
0.52.1 Jan 03, 2026
0.52.0 Jan 02, 2026
0.51.0 Jan 01, 2026
0.50.0 Dec 28, 2025
0.49.0 Dec 25, 2025
0.48.0 Dec 24, 2025
0.47.0 Dec 23, 2025
0.46.0 Dec 20, 2025
0.45.0 Dec 19, 2025
0.44.0 Dec 18, 2025
0.43.1 Dec 12, 2025
0.43.0 Dec 10, 2025
0.42.2 Dec 08, 2025
0.42.1 Dec 08, 2025
0.42.0 Dec 08, 2025
0.41.0 Dec 05, 2025
0.40.0 Dec 03, 2025
0.39.0 Dec 02, 2025
0.38.0 Dec 02, 2025
0.37.0 Dec 01, 2025
0.36.0 Nov 26, 2025
0.35.0 Oct 09, 2025
0.34.0 Sep 28, 2025
0.33.0 Aug 14, 2025
0.32.0 Jul 25, 2025
0.31.2 Jun 22, 2025
0.31.1 Jun 17, 2025
0.31.0 Jun 12, 2025
0.30.2 Jun 07, 2025
0.30.1 Apr 28, 2025
0.30.0 Apr 17, 2025
0.29.0 Apr 17, 2025
0.28.5 Mar 24, 2025
0.28.4 Mar 11, 2025
0.28.3 Mar 10, 2025
0.28.2 Feb 27, 2025
0.28.1 Feb 15, 2025
0.28.0 Feb 14, 2025
0.27.3 Feb 11, 2025
0.27.2 Feb 07, 2025
0.27.1 Feb 06, 2025
0.27.0 Feb 06, 2025
0.26.5 Jan 14, 2025
0.26.4 Dec 15, 2024
0.26.3 Nov 10, 2024
0.26.2 Oct 17, 2024
0.26.1 Sep 27, 2024
0.26.0 Sep 02, 2024
0.25.9 Aug 07, 2024
0.25.8 Jul 04, 2024
0.25.7 Jun 11, 2024
0.25.6 Apr 25, 2024
0.25.5 Mar 16, 2024
0.25.4 Feb 13, 2024
0.25.3 Feb 01, 2024
0.25.2 Dec 21, 2023
0.25.1 Nov 26, 2023
0.25.0 Nov 25, 2023
0.24.2 Nov 16, 2023
0.24.1 Nov 16, 2023
0.24.0 Nov 16, 2023
0.23.0 Nov 08, 2023
0.22.1 Oct 08, 2023
0.22.0 Sep 23, 2023
0.21.5 Sep 06, 2023
0.21.4 Aug 09, 2023
0.21.3 Aug 03, 2023
0.21.2 Jul 20, 2023
0.21.1 Jul 06, 2023
0.21.0 Jul 03, 2023
0.20.0 Jun 06, 2023
0.19.0 May 10, 2023
0.18.1 Apr 27, 2023
0.18.0 Apr 16, 2023
0.17.2 Mar 31, 2023
0.17.1 Feb 06, 2023
0.17.0 Jan 30, 2023
0.16.1 Jan 22, 2023
0.16.0 Jan 16, 2023
0.15.0 Jan 04, 2023
0.14.1 Dec 28, 2022
0.14.0 Nov 18, 2022
0.13.5 Nov 06, 2022
0.13.4 Oct 31, 2022
0.13.3 Oct 27, 2022
0.13.2 Oct 17, 2022
0.13.1 Aug 11, 2022
0.13.0 May 27, 2022
0.12.3 May 27, 2022
0.12.2 May 27, 2022
0.12.1 May 19, 2022
0.12.0 Apr 18, 2022
0.11.20 Mar 12, 2022
0.11.19 Feb 08, 2022
0.11.18 Feb 02, 2022
0.11.17 Jan 23, 2022
0.11.16 Jan 17, 2022
0.11.15 Nov 29, 2021
0.11.14 Sep 30, 2021
0.11.13 Sep 15, 2021
0.11.12 Aug 27, 2021
0.11.11 Aug 12, 2021
0.11.10 Aug 12, 2021
0.11.9 Jul 31, 2021
0.11.8 Jun 11, 2021
0.11.7 Jun 09, 2021
0.11.6 May 28, 2021
0.11.5 May 16, 2021
0.11.4 May 05, 2021
0.11.3 Apr 29, 2021
0.11.2 Apr 26, 2021
0.11.1 Apr 22, 2021
0.11.0 Apr 21, 2021
0.10.3 Apr 18, 2021
0.10.2 Apr 03, 2021
0.10.1 Mar 31, 2021
0.10.0 Mar 27, 2021
0.9.4 Mar 21, 2021
0.9.3 Mar 18, 2021
0.9.2 Mar 11, 2021
0.9.1 Mar 08, 2021
0.9.0 Mar 04, 2021
0.8.3 Feb 25, 2021
0.8.2 Feb 20, 2021
0.8.1 Feb 18, 2021
0.8.0 Feb 17, 2021
0.7.3 Feb 16, 2021
0.7.2 Feb 09, 2021
0.7.1 Feb 05, 2021
0.7.0 Jan 31, 2021
0.6.26 Jan 26, 2021
0.6.25 Jan 24, 2021
0.6.24 Jan 23, 2021
0.6.23 Jan 21, 2021
0.6.22 Jan 19, 2021
0.6.21 Jan 18, 2021
0.6.20 Jan 15, 2021
0.6.19 Jan 15, 2021
0.6.18 Jan 08, 2021
0.6.17 Jan 06, 2021
0.6.16 Dec 31, 2020
0.6.15 Dec 28, 2020
0.6.14 Dec 27, 2020
0.6.13 Dec 25, 2020
0.6.12 Dec 24, 2020
0.6.11 Dec 17, 2020
0.6.10 Dec 07, 2020
0.6.9 Dec 02, 2020
0.6.8 Nov 29, 2020
0.6.7 Nov 17, 2020
0.6.6 Nov 14, 2020
0.6.5 Nov 12, 2020
0.6.4 Nov 11, 2020
0.6.3 Nov 09, 2020
0.6.2 Nov 05, 2020
0.6.1 Nov 01, 2020
0.6.0 Oct 18, 2020
0.5.39 Oct 06, 2020
0.5.38 Oct 05, 2020
0.5.37 Oct 03, 2020
0.5.36 Oct 02, 2020
0.5.35 Sep 23, 2020
0.5.34 Sep 19, 2020
0.5.33 Sep 17, 2020
0.5.32 Sep 16, 2020
0.5.31 Sep 12, 2020
0.5.30 Sep 04, 2020
0.5.29 Aug 25, 2020
0.5.28 Aug 21, 2020
0.5.27 Aug 16, 2020
0.5.26 Aug 14, 2020
0.5.25 Aug 13, 2020
0.5.24 Aug 03, 2020
0.5.23 Aug 02, 2020
0.5.22 Jul 30, 2020
0.5.21 Jul 30, 2020
0.5.20 Jul 27, 2020
0.5.19 Jul 24, 2020
0.5.18 Jul 23, 2020
0.5.17 Jul 22, 2020
0.5.16 Jul 19, 2020
0.5.15 Jul 19, 2020
0.5.14 Jul 14, 2020
0.5.13 Jun 30, 2020
0.5.12 Jun 29, 2020
0.5.11 Jun 25, 2020
0.5.10 Jun 20, 2020
0.5.9 Jun 19, 2020
0.5.8 Jun 17, 2020
0.5.7 Jun 14, 2020
0.5.6 Jun 13, 2020
0.5.5 Jun 12, 2020
0.5.4 Jun 11, 2020
0.5.3 Jun 11, 2020
0.5.2 Jun 05, 2020
0.5.1 Jun 05, 2020
0.5.0 Jun 02, 2020
0.4.11 May 19, 2020
0.4.10 May 06, 2020
0.4.9 Apr 22, 2020
0.4.8 Apr 18, 2020
0.4.7 Apr 14, 2020
0.4.6 Apr 06, 2020
0.4.5 Apr 05, 2020
0.4.4 Apr 01, 2020
0.4.3 Mar 31, 2020
0.4.2 Mar 30, 2020
0.4.1 Mar 23, 2020
0.4.0 Mar 16, 2020
0.3.3 Feb 26, 2020
0.3.2 Feb 05, 2020
0.3.1 Feb 03, 2020
0.3.0 Jan 09, 2020
0.2.16 Dec 13, 2019
0.2.15 Dec 04, 2019
0.2.14 Nov 25, 2019
0.2.13 Nov 22, 2019
0.2.12 Nov 04, 2019
0.2.11 Oct 18, 2019
0.2.10 Oct 18, 2019
0.2.9 Oct 17, 2019
0.2.8 Oct 16, 2019
0.2.7 Oct 15, 2019
0.2.6 Oct 10, 2019
0.2.5 Oct 09, 2019
0.2.4 Sep 26, 2019
0.2.3 Sep 13, 2019
0.2.2 Sep 13, 2019
0.2.1 Sep 13, 2019
0.2.0 Sep 05, 2019
0.1.0 Aug 06, 2019
0.0.6 Jul 31, 2019
0.0.5 Jul 26, 2019
0.0.4 Jul 23, 2019
0.0.3 Jul 23, 2019
0.0.2 Jul 23, 2019
0.0.1 Jul 23, 2019
Extras:
Dependencies:
argcomplete (<4,>=2.10.1)
black (>=19.10b0)
genson (<2,>=1.2.1)
inflect (<8,>=4.1)
isort (<9,>=4.3.21)
jinja2 (<4,>=2.10.1)
pydantic (<3,>=2.12)
pydantic (<3,>=2)
pyyaml (>=6.0.1)
tomli (<3,>=2.2.1)