Skip to content

datamodel-code-generator

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

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

๐Ÿงช Try It In Your Browser

Generate models in your browser without installing anything.

Open Playground

Playground privacy

The playground runs datamodel-code-generator locally in your browser with Pyodide. Your schema and options are not sent to a backend for generation. If you copy a repro URL, the schema and options are encoded 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.


๐Ÿ“ฆ Installation

uv tool install datamodel-code-generator
pip install datamodel-code-generator
uv add --dev datamodel-code-generator
conda install -c conda-forge datamodel-code-generator
pipx install datamodel-code-generator
uvx datamodel-codegen --help

Use uv tool install when you want datamodel-codegen available as a standalone CLI. Use uv add --dev when a project or CI workflow should pin the generator version in its lockfile.


Omitting --output-model-type is deprecated

Starting from version 0.53.0, omitting --output-model-type is deprecated.

We recommend using --output-model-type pydantic_v2.BaseModel for new projects.


๐Ÿƒ Quick Start

1๏ธโƒฃ Create a schema file

pet.json
{
  "$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
    }
  }
}

2๏ธโƒฃ Run the generator

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

3๏ธโƒฃ Use your models

model.py
# generated by datamodel-codegen:
#   filename:  tutorial_pet.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

๐ŸŽ‰ That's it! Your schema is now a fully-typed Python model.


๐Ÿ“ฅ Choose Your Input

Input Type File Types Example
๐Ÿ“˜ OpenAPI 3.0/3.1/3.2 .yaml, .json API specifications
๐Ÿ“ก AsyncAPI .yaml, .json Event-driven API specifications
๐Ÿ“‹ JSON Schema .json, .yaml Data validation schemas
๐Ÿชถ Apache Avro .avsc, .json Avro schemas
๐Ÿงพ XML Schema .xsd XML document schemas
๐Ÿงฉ Protocol Buffers / gRPC .proto Protobuf messages and service schemas
๐Ÿ”ท GraphQL .graphql GraphQL type definitions
๐Ÿ› ๏ธ MCP Tool Schemas .json, .yaml MCP tool input/output schemas
๐Ÿ“Š JSON/YAML/CSV Data .json, .yaml, .csv Infer schema from data
๐Ÿ Python Models .py Pydantic, dataclass, TypedDict

๐Ÿ“ค Choose Your Output

# ๐Ÿ†• Pydantic v2 (recommended for new projects)
datamodel-codegen --output-model-type pydantic_v2.BaseModel ...

# ๐Ÿ—๏ธ Python dataclasses
datamodel-codegen --output-model-type dataclasses.dataclass ...

# ๐Ÿ“ TypedDict (for type hints without validation)
datamodel-codegen --output-model-type typing.TypedDict ...

# โšก msgspec (high-performance serialization)
datamodel-codegen --output-model-type msgspec.Struct ...

See Supported Data Types for the full list.


๐Ÿณ 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

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:

.github/workflows/validate-models.yml
- uses: koxudaxi/datamodel-code-generator@0.60.0
  with:
    input: schemas/api.yaml
    output: src/models/api.py

See CI/CD Integration for more options.


๐Ÿ“š Next Steps


๐Ÿ’– Sponsors

Astral Logo

Astral


๐Ÿข Used by

These projects use datamodel-code-generator. See the linked examples for real-world usage.

See all dependents โ†’