Skip to main content

Datamodel Code Generator

Project description

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

uv tool install datamodel-code-generator
Other installation methods

pip:

pip install datamodel-code-generator

uv (add to project):

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 →


🔗 Related Projects


🤝 Contributing

See Development & Contributing for how to get started!

📄 License

MIT License - see LICENSE for details.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datamodel_code_generator-0.46.0.tar.gz (557.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datamodel_code_generator-0.46.0-py3-none-any.whl (183.7 kB view details)

Uploaded Python 3

File details

Details for the file datamodel_code_generator-0.46.0.tar.gz.

File metadata

File hashes

Hashes for datamodel_code_generator-0.46.0.tar.gz
Algorithm Hash digest
SHA256 c020f9b014f93c02b526791b03bac235b49803ee178f11598e0a3465dadf9317
MD5 4b1dfcce2328942b4c06049242cd7ef1
BLAKE2b-256 60f58c1e9c0cb0752ee98baebd1fb98c389d8ee125e21c7acd8d7d9f6883b314

See more details on using hashes here.

Provenance

The following attestation bundles were made for datamodel_code_generator-0.46.0.tar.gz:

Publisher: publish.yaml on koxudaxi/datamodel-code-generator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file datamodel_code_generator-0.46.0-py3-none-any.whl.

File metadata

File hashes

Hashes for datamodel_code_generator-0.46.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cdb990bdb129ee1ceb37ac243879decabb91fa53e27439ba67da20da9caee0ee
MD5 8c0cd8981ce0d3d6f3d029f2e307ab8c
BLAKE2b-256 f23117c3217d35848d3aff2828d5de91f2f608734bad5cd2e4e4f845037753e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for datamodel_code_generator-0.46.0-py3-none-any.whl:

Publisher: publish.yaml on koxudaxi/datamodel-code-generator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page