datamodel-code-generator

Datamodel Code Generator

257 个版本 Python >=3.10
安装
pip install datamodel-code-generator
poetry add datamodel-code-generator
pipenv install datamodel-code-generator
conda install datamodel-code-generator
描述

datamodel-code-generator

🚀 Generate Python data models from schema definitions in seconds.

🧪 Try it in your browser: Playground

[!NOTE] Playground privacy: generation runs locally in your browser with Pyodide. Schemas 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

📣 💼 Maintainer update: Open to opportunities. 🔗 koxudaxi.dev

✨ 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

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

uv add --dev datamodel-code-generator
Other installation methods

pip:

pip install datamodel-code-generator

uv (run without adding to project):

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

conda:

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

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

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


📖 Documentation

👉 datamodel-code-generator.koxudaxi.dev


📥 Supported Input

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

📤 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

🤖 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:

- uses: koxudaxi/datamodel-code-generator@0.44.0
  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 projects use datamodel-code-generator. See the linked examples for real-world usage.

See all dependents →



🤝 Contributing

See Development & Contributing for how to get started!


👤 Maintainer

Koudai Aono (@koxudaxi)


📄 License

MIT License - see LICENSE for details.

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