Skip to main content

Datamodel Code Generator

Project description

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

๐Ÿ“– Documentation

๐Ÿ‘‰ datamodel-code-generator.koxudaxi.dev


๐Ÿ“ฆ 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

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


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


๐Ÿ’– 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.60.2.tar.gz (1.1 MB 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.60.2-py3-none-any.whl (338.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for datamodel_code_generator-0.60.2.tar.gz
Algorithm Hash digest
SHA256 521258c6b66155cf04c568f993ebe41094bdd176d20bf4de5647d7ff72a3480d
MD5 24722ff9d98128dde7d10976c7d55f52
BLAKE2b-256 d704c45120b0eb0e91565a383d765bfc4b71b9187136f8d432590805f10ae0fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for datamodel_code_generator-0.60.2.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.60.2-py3-none-any.whl.

File metadata

File hashes

Hashes for datamodel_code_generator-0.60.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4d154aa1bc9741f0ccf29b3584e557d9c5cdd7ea0d7eddeda0103765a8c00a98
MD5 0fc54f37ae0115001eb0c9090b377551
BLAKE2b-256 6f0efb3757846a2d141476f27f13c351285a7aef6471cbeed924c8c1f06b6bca

See more details on using hashes here.

Provenance

The following attestation bundles were made for datamodel_code_generator-0.60.2-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