Skip to content

koxudaxi/datamodel-code-generator

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

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.

About

Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

Contributors 198

Languages