Datamodel Code Generator
Project description
datamodel-code-generator
🚀 Generate Python data models from schema definitions in seconds.
✨ 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 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
👉 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 v1 BaseModel
- 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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file datamodel_code_generator-0.53.0.tar.gz.
File metadata
- Download URL: datamodel_code_generator-0.53.0.tar.gz
- Upload date:
- Size: 809.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af46b57ad78e6435873132c52843ef0ec7b768a591d3b9917d3409dfc1ab1c90
|
|
| MD5 |
0c54717adc98ca223df635af83338ba5
|
|
| BLAKE2b-256 |
40653802abca0291263862a16e032e984e61e4d0d30a344d9be97815721d64ff
|
Provenance
The following attestation bundles were made for datamodel_code_generator-0.53.0.tar.gz:
Publisher:
publish.yaml on koxudaxi/datamodel-code-generator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
datamodel_code_generator-0.53.0.tar.gz -
Subject digest:
af46b57ad78e6435873132c52843ef0ec7b768a591d3b9917d3409dfc1ab1c90 - Sigstore transparency entry: 814883842
- Sigstore integration time:
-
Permalink:
koxudaxi/datamodel-code-generator@a6a7b04f44c14086e7eb5636af9509bafafdb040 -
Branch / Tag:
refs/tags/0.53.0 - Owner: https://github.com/koxudaxi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@a6a7b04f44c14086e7eb5636af9509bafafdb040 -
Trigger Event:
push
-
Statement type:
File details
Details for the file datamodel_code_generator-0.53.0-py3-none-any.whl.
File metadata
- Download URL: datamodel_code_generator-0.53.0-py3-none-any.whl
- Upload date:
- Size: 258.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1cc2abe79f99b8208c363f5f4b603c29290327ff4e3219a08c0fff45f42aff4
|
|
| MD5 |
d814940382f291102529fc6e472309b3
|
|
| BLAKE2b-256 |
ff435dbb6fe09842e10062f94016ccb48c9613f2443253866de3d7b815713b4d
|
Provenance
The following attestation bundles were made for datamodel_code_generator-0.53.0-py3-none-any.whl:
Publisher:
publish.yaml on koxudaxi/datamodel-code-generator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
datamodel_code_generator-0.53.0-py3-none-any.whl -
Subject digest:
d1cc2abe79f99b8208c363f5f4b603c29290327ff4e3219a08c0fff45f42aff4 - Sigstore transparency entry: 814883853
- Sigstore integration time:
-
Permalink:
koxudaxi/datamodel-code-generator@a6a7b04f44c14086e7eb5636af9509bafafdb040 -
Branch / Tag:
refs/tags/0.53.0 - Owner: https://github.com/koxudaxi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@a6a7b04f44c14086e7eb5636af9509bafafdb040 -
Trigger Event:
push
-
Statement type: