Datamodel Code Generator
Project Links
Meta
Author: Koudai Aono
Requires Python: >=3.10
Classifiers
Development Status
- 4 - Beta
License
- OSI Approved :: MIT License
Natural Language
- English
Programming Language
- Python :: 3 :: Only
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
- Python :: 3.13
- Python :: 3.14
- Python :: Implementation :: CPython
datamodel-code-generator
๐ Generate Python data models from schema definitions in seconds.
๐ฃ ๐ผ Maintainer update: Open to opportunities. ๐ koxudaxi.dev
โจ What it does
- ๐ Converts OpenAPI 3, JSON Schema, GraphQL, 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
- ๐ฅ๏ธ CLI Reference - All command-line options
- โ๏ธ pyproject.toml - Configuration file
- ๐ CI/CD Integration - GitHub Actions, pre-commit hooks
- ๐ One-liner Usage - uvx, pipx, clipboard integration
- โ FAQ - Common questions
๐ฆ 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 types (Pydantic, dataclass, TypedDict) via
--input-model - Python dictionary
๐ค Supported Output
- pydantic v2 BaseModel
- pydantic v2 dataclass
- dataclasses
- TypedDict
- msgspec Struct
๐ณ 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 |
๐ข Projects that use datamodel-code-generator
These projects use datamodel-code-generator. See the linked examples for real-world usage.
- PostHog/posthog - Generate models via npm run
- airbytehq/airbyte - Generate Python, Java/Kotlin, and Typescript protocol models
- apache/iceberg - Generate Python code
- open-metadata/OpenMetadata - datamodel_generation.py
- awslabs/aws-lambda-powertools-python - Recommended for advanced-use-cases
- Netflix/consoleme - Generate models from Swagger
- DataDog/integrations-core - Config models
- argoproj-labs/hera - Makefile
- SeldonIO/MLServer - generate-types.sh
- geojupyter/jupytergis - Python type generation from JSONSchema
- Nike-Inc/brickflow - Code generate tools
- cloudcoil/cloudcoil - Model generation
- IBM/compliance-trestle - Building models from OSCAL schemas
- hashintel/hash - codegen.sh
๐ Related Projects
- fastapi-code-generator - Generate FastAPI app from OpenAPI
- pydantic-pycharm-plugin - PyCharm plugin for Pydantic
๐ค Contributing
See Development & Contributing for how to get started!
๐ License
MIT License - see LICENSE for details.
0.55.0
Mar 10, 2026
0.54.1
Mar 04, 2026
0.54.0
Feb 14, 2026
0.53.0
Jan 12, 2026
0.52.2
Jan 05, 2026
0.52.1
Jan 03, 2026
0.52.0
Jan 02, 2026
0.51.0
Jan 01, 2026
0.50.0
Dec 28, 2025
0.49.0
Dec 25, 2025
0.48.0
Dec 24, 2025
0.47.0
Dec 23, 2025
0.46.0
Dec 20, 2025
0.45.0
Dec 19, 2025
0.44.0
Dec 18, 2025
0.43.1
Dec 12, 2025
0.43.0
Dec 10, 2025
0.42.2
Dec 08, 2025
0.42.1
Dec 08, 2025
0.42.0
Dec 08, 2025
0.41.0
Dec 05, 2025
0.40.0
Dec 03, 2025
0.39.0
Dec 02, 2025
0.38.0
Dec 02, 2025
0.37.0
Dec 01, 2025
0.36.0
Nov 26, 2025
0.35.0
Oct 09, 2025
0.34.0
Sep 28, 2025
0.33.0
Aug 14, 2025
0.32.0
Jul 25, 2025
0.31.2
Jun 22, 2025
0.31.1
Jun 17, 2025
0.31.0
Jun 12, 2025
0.30.2
Jun 07, 2025
0.30.1
Apr 28, 2025
0.30.0
Apr 17, 2025
0.29.0
Apr 17, 2025
0.28.5
Mar 24, 2025
0.28.4
Mar 11, 2025
0.28.3
Mar 10, 2025
0.28.2
Feb 27, 2025
0.28.1
Feb 15, 2025
0.28.0
Feb 14, 2025
0.27.3
Feb 11, 2025
0.27.2
Feb 07, 2025
0.27.1
Feb 06, 2025
0.27.0
Feb 06, 2025
0.26.5
Jan 14, 2025
0.26.4
Dec 15, 2024
0.26.3
Nov 10, 2024
0.26.2
Oct 17, 2024
0.26.1
Sep 27, 2024
0.26.0
Sep 02, 2024
0.25.9
Aug 07, 2024
0.25.8
Jul 04, 2024
0.25.7
Jun 11, 2024
0.25.6
Apr 25, 2024
0.25.5
Mar 16, 2024
0.25.4
Feb 13, 2024
0.25.3
Feb 01, 2024
0.25.2
Dec 21, 2023
0.25.1
Nov 26, 2023
0.25.0
Nov 25, 2023
0.24.2
Nov 16, 2023
0.24.1
Nov 16, 2023
0.24.0
Nov 16, 2023
0.23.0
Nov 08, 2023
0.22.1
Oct 08, 2023
0.22.0
Sep 23, 2023
0.21.5
Sep 06, 2023
0.21.4
Aug 09, 2023
0.21.3
Aug 03, 2023
0.21.2
Jul 20, 2023
0.21.1
Jul 06, 2023
0.21.0
Jul 03, 2023
0.20.0
Jun 06, 2023
0.19.0
May 10, 2023
0.18.1
Apr 27, 2023
0.18.0
Apr 16, 2023
0.17.2
Mar 31, 2023
0.17.1
Feb 06, 2023
0.17.0
Jan 30, 2023
0.16.1
Jan 22, 2023
0.16.0
Jan 16, 2023
0.15.0
Jan 04, 2023
0.14.1
Dec 28, 2022
0.14.0
Nov 18, 2022
0.13.5
Nov 06, 2022
0.13.4
Oct 31, 2022
0.13.3
Oct 27, 2022
0.13.2
Oct 17, 2022
0.13.1
Aug 11, 2022
0.13.0
May 27, 2022
0.12.3
May 27, 2022
0.12.2
May 27, 2022
0.12.1
May 19, 2022
0.12.0
Apr 18, 2022
0.11.20
Mar 12, 2022
0.11.19
Feb 08, 2022
0.11.18
Feb 02, 2022
0.11.17
Jan 23, 2022
0.11.16
Jan 17, 2022
0.11.15
Nov 29, 2021
0.11.14
Sep 30, 2021
0.11.13
Sep 15, 2021
0.11.12
Aug 27, 2021
0.11.11
Aug 12, 2021
0.11.10
Aug 12, 2021
0.11.9
Jul 31, 2021
0.11.8
Jun 11, 2021
0.11.7
Jun 09, 2021
0.11.6
May 28, 2021
0.11.5
May 16, 2021
0.11.4
May 05, 2021
0.11.3
Apr 29, 2021
0.11.2
Apr 26, 2021
0.11.1
Apr 22, 2021
0.11.0
Apr 21, 2021
0.10.3
Apr 18, 2021
0.10.2
Apr 03, 2021
0.10.1
Mar 31, 2021
0.10.0
Mar 27, 2021
0.9.4
Mar 21, 2021
0.9.3
Mar 18, 2021
0.9.2
Mar 11, 2021
0.9.1
Mar 08, 2021
0.9.0
Mar 04, 2021
0.8.3
Feb 25, 2021
0.8.2
Feb 20, 2021
0.8.1
Feb 18, 2021
0.8.0
Feb 17, 2021
0.7.3
Feb 16, 2021
0.7.2
Feb 09, 2021
0.7.1
Feb 05, 2021
0.7.0
Jan 31, 2021
0.6.26
Jan 26, 2021
0.6.25
Jan 24, 2021
0.6.24
Jan 23, 2021
0.6.23
Jan 21, 2021
0.6.22
Jan 19, 2021
0.6.21
Jan 18, 2021
0.6.20
Jan 15, 2021
0.6.19
Jan 15, 2021
0.6.18
Jan 08, 2021
0.6.17
Jan 06, 2021
0.6.16
Dec 31, 2020
0.6.15
Dec 28, 2020
0.6.14
Dec 27, 2020
0.6.13
Dec 25, 2020
0.6.12
Dec 24, 2020
0.6.11
Dec 17, 2020
0.6.10
Dec 07, 2020
0.6.9
Dec 02, 2020
0.6.8
Nov 29, 2020
0.6.7
Nov 17, 2020
0.6.6
Nov 14, 2020
0.6.5
Nov 12, 2020
0.6.4
Nov 11, 2020
0.6.3
Nov 09, 2020
0.6.2
Nov 05, 2020
0.6.1
Nov 01, 2020
0.6.0
Oct 18, 2020
0.5.39
Oct 06, 2020
0.5.38
Oct 05, 2020
0.5.37
Oct 03, 2020
0.5.36
Oct 02, 2020
0.5.35
Sep 23, 2020
0.5.34
Sep 19, 2020
0.5.33
Sep 17, 2020
0.5.32
Sep 16, 2020
0.5.31
Sep 12, 2020
0.5.30
Sep 04, 2020
0.5.29
Aug 25, 2020
0.5.28
Aug 21, 2020
0.5.27
Aug 16, 2020
0.5.26
Aug 14, 2020
0.5.25
Aug 13, 2020
0.5.24
Aug 03, 2020
0.5.23
Aug 02, 2020
0.5.22
Jul 30, 2020
0.5.21
Jul 30, 2020
0.5.20
Jul 27, 2020
0.5.19
Jul 24, 2020
0.5.18
Jul 23, 2020
0.5.17
Jul 22, 2020
0.5.16
Jul 19, 2020
0.5.15
Jul 19, 2020
0.5.14
Jul 14, 2020
0.5.13
Jun 30, 2020
0.5.12
Jun 29, 2020
0.5.11
Jun 25, 2020
0.5.10
Jun 20, 2020
0.5.9
Jun 19, 2020
0.5.8
Jun 17, 2020
0.5.7
Jun 14, 2020
0.5.6
Jun 13, 2020
0.5.5
Jun 12, 2020
0.5.4
Jun 11, 2020
0.5.3
Jun 11, 2020
0.5.2
Jun 05, 2020
0.5.1
Jun 05, 2020
0.5.0
Jun 02, 2020
0.4.11
May 19, 2020
0.4.10
May 06, 2020
0.4.9
Apr 22, 2020
0.4.8
Apr 18, 2020
0.4.7
Apr 14, 2020
0.4.6
Apr 06, 2020
0.4.5
Apr 05, 2020
0.4.4
Apr 01, 2020
0.4.3
Mar 31, 2020
0.4.2
Mar 30, 2020
0.4.1
Mar 23, 2020
0.4.0
Mar 16, 2020
0.3.3
Feb 26, 2020
0.3.2
Feb 05, 2020
0.3.1
Feb 03, 2020
0.3.0
Jan 09, 2020
0.2.16
Dec 13, 2019
0.2.15
Dec 04, 2019
0.2.14
Nov 25, 2019
0.2.13
Nov 22, 2019
0.2.12
Nov 04, 2019
0.2.11
Oct 18, 2019
0.2.10
Oct 18, 2019
0.2.9
Oct 17, 2019
0.2.8
Oct 16, 2019
0.2.7
Oct 15, 2019
0.2.6
Oct 10, 2019
0.2.5
Oct 09, 2019
0.2.4
Sep 26, 2019
0.2.3
Sep 13, 2019
0.2.2
Sep 13, 2019
0.2.1
Sep 13, 2019
0.2.0
Sep 05, 2019
0.1.0
Aug 06, 2019
0.0.6
Jul 31, 2019
0.0.5
Jul 26, 2019
0.0.4
Jul 23, 2019
0.0.3
Jul 23, 2019
0.0.2
Jul 23, 2019
0.0.1
Jul 23, 2019