Skip to main content

datamodel-code-generator

🚀 Generate Python data models from schema definitions in seconds.

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

✨ What it does

  • 📄 Converts OpenAPI 3, JSON Schema, GraphQL, and raw data (JSON/YAML/CSV) into Python models
  • 🎯 Generates Pydantic v1/v2, 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

📖 Documentation

👉 koxudaxi.github.io/datamodel-code-generator


📦 Installation

pip install datamodel-code-generator
Other installation methods

uv:

uv add datamodel-code-generator

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]'

Docker:

docker pull koxudaxi/datamodel-code-generator

🏃 Quick Start

datamodel-codegen --input schema.json --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --output model.py
📄 schema.json (input)
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Pet",
  "type": "object",
  "required": ["name", "species"],
  "properties": {
    "name": {
      "type": "string",
      "description": "The pet's name"
    },
    "species": {
      "type": "string",
      "enum": ["dog", "cat", "bird", "fish"]
    },
    "age": {
      "type": "integer",
      "minimum": 0,
      "description": "Age in years"
    },
    "vaccinated": {
      "type": "boolean",
      "default": false
    }
  }
}
🐍 model.py (output)
# generated by datamodel-codegen:
#   filename:  schema.json

from __future__ import annotations

from enum import Enum
from typing import Optional

from pydantic import BaseModel, Field


class Species(Enum):
    dog = 'dog'
    cat = 'cat'
    bird = 'bird'
    fish = 'fish'


class Pet(BaseModel):
    name: str = Field(..., description="The pet's name")
    species: Species
    age: Optional[int] = Field(None, description='Age in years', ge=0)
    vaccinated: Optional[bool] = False

📥 Supported Input

  • OpenAPI 3 (YAML/JSON)
  • JSON Schema
  • JSON / YAML / CSV data
  • GraphQL schema
  • Python dictionary

📤 Supported Output


🍳 Common Recipes

🆕 Generate Pydantic v2 models

datamodel-codegen --input schema.json --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --output model.py

🌐 Generate from URL

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

⚙️ Use with pyproject.toml

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

See pyproject.toml Configuration for more options.

🔄 CI/CD Integration

datamodel-codegen --check

Verify generated code stays in sync with schemas. See CI/CD Integration for GitHub Actions and pre-commit hooks.


💖 Sponsors

Astral Logo

Astral


🏢 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!

📄 License

MIT License - see LICENSE for details.

Metadata

Release files for datamodel-code-generator 0.45.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for datamodel-code-generator 0.45.0
File Size Uploaded Provenance Actions
datamodel_code_generator-0.45.0.tar.gz 554.4 kB With provenance Details for datamodel_code_generator-0.45.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for datamodel-code-generator 0.45.0
File Interpreter ABI Platform Actions
datamodel_code_generator-0.45.0-py3-none-any.whl Python 3 none any Details for datamodel_code_generator-0.45.0-py3-none-any.whl

Total release size: 736.4 kB

Release files / datamodel_code_generator-0.45.0.tar.gz

Download URL datamodel_code_generator-0.45.0.tar.gz
Size 554.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a493add7a59e5a9c1620deb508694bc84e2e2ca80da4bb58a75262a28b9b2c84
BLAKE2b-256 checksum
How to use checksums
f545993fb3f7f4005bfa167fa94929e5737c8f574dc77fb1a4ebca359a106b56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 19, 2025.

Transparency log

Release files / datamodel_code_generator-0.45.0-py3-none-any.whl

Download URL datamodel_code_generator-0.45.0-py3-none-any.whl
Size 182.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1c7065d3c9ebff7a0c967f2282e0472be971c721593c7e9cbe3f374f378749b8
BLAKE2b-256 checksum
How to use checksums
4d15b9b64a3576c3ac230c8f69806f32937364f837cda4facaa9b6d09dafd872
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 19, 2025.

Transparency log

Security for datamodel-code-generator 0.45.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for datamodel_code_generator-0.45.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 19, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.45.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page