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.
📣 💼 Maintainer update: Open to opportunities. 🔗 koxudaxi.dev
✨ What it does
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
📦 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
Command
datamodel-codegen \
--input schema.json \
--input-file-type jsonschema \
--output-model-type pydantic_v2.BaseModel \
--preset standard-py312-20260619 \
--output model.py
This quick start uses standard-py312-20260619 as the modern Python 3.12 baseline.
Preset names include the target Python version: py312 means Python 3.12.
See CLI Reference for all options. See Presets,
--preset, --input-file-type, and
--output-model-type for this command.
For more schema-aware output that preserves schema-authored names, reuses models, and embeds generated
documentation, use practical-py312-20260619.
Input (schema.json)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Pet",
"type": "object",
"required": ["name"],
"properties": {
"name": {
"type": "string",
"description": "The pet's name"
},
"species": {
"type": "string",
"enum": ["dog", "cat", "bird", "fish"],
"default": "dog"
},
"age": {
"type": "integer",
"minimum": 0,
"description": "Age in years"
},
"vaccinated": {
"type": "boolean",
"default": false
}
}
}
Output (model.py)
# generated by datamodel-codegen:
# filename: schema.json
from __future__ import annotations
from enum import StrEnum
from typing import Annotated
from pydantic import BaseModel, ConfigDict, Field
class Species(StrEnum):
dog = 'dog'
cat = 'cat'
bird = 'bird'
fish = 'fish'
class Pet(BaseModel):
model_config = ConfigDict(
populate_by_name=True,
)
name: Annotated[str, Field(description="The pet's name")]
species: Species = Species.dog
age: Annotated[int | None, Field(description='Age in years', ge=0)] = None
vaccinated: bool = False
⚡ Speed up generation
By default, generated Python is currently formatted with black and isort. For faster generation without external
formatter dependencies, add --formatters builtin for standard generated model modules. In a future version, the
Black/isort dependencies will become opt-in and the default formatter will change to builtin.
If you prefer Ruff, install it with pip install 'datamodel-code-generator[ruff]' and use
--formatters ruff-check ruff-format for a fast external formatter.
Custom templates can emit Python outside the standard generated model patterns covered by builtin, so
custom-template output is not exhaustively validated. If --formatters builtin produces invalid or poorly formatted
output with a custom template, please open an issue with a small reproducer. See
Formatter Behavior for details.
📖 Documentation
👉 datamodel-code-generator.koxudaxi.dev
- 🧰 Presets - Recommended option bundles for modern output
- 🖥️ CLI Reference - All command-line options
- 🧪 Playground - Try generation in your browser
- ⚙️ pyproject.toml - Configuration file
- 🔄 CI/CD Integration - GitHub Actions, pre-commit hooks
- ✅ Conformance Dashboard - External corpus coverage signals
- 🧭 Architecture - Generation pipeline and synchronized component inventory
- 🚀 One-liner Usage - uvx, pipx, clipboard integration
- ❓ FAQ - Common questions
📥 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
- pydantic v2 BaseModel
- pydantic v2 dataclass
- dataclasses
- TypedDict
- msgspec Struct
✅ Conformance Signals
CI exercises datamodel-code-generator against pinned external corpora for XML Schema, JSON Schema, AsyncAPI, Apache Avro, and Protocol Buffers. See the Conformance Dashboard for the generated summary of runner scripts, tox environments, CI jobs, expected corpus counts, and upstream sources.
🍳 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.
Coding agent skill
This repository includes an experimental Agent Skill that teaches compatible coding agents to run datamodel-codegen when generating Python models from OpenAPI, AsyncAPI, JSON Schema, GraphQL, JSON/YAML/CSV sample data, MCP tool schemas, Protocol Buffers, XML Schema, Apache Avro, or existing Python model objects.
See Coding Agent Skill for detailed guidance and troubleshooting.
Install the directory for your agent:
# Codex, project-local
mkdir -p .agents/skills
cp -R skills/datamodel-code-generator .agents/skills/datamodel-code-generator
# Claude Code, project-local
mkdir -p .claude/skills
cp -R skills/datamodel-code-generator .claude/skills/datamodel-code-generator
For a personal install, copy the same directory to $HOME/.agents/skills/datamodel-code-generator/ for Codex or ~/.claude/skills/datamodel-code-generator/ for Claude Code.
Check your agent's current documentation for exact search paths.
💖 Sponsors
|
Astral |
OpenAI |
🏢 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
- openai/codex - Python SDK dev dependency
- vllm-project/vllm - Test dependency for model tests
- stanfordnlp/dspy - Generate Pydantic models from JSON Schema for reliability tests
- topoteretes/cognee - Runtime generation of graph data models from JSON Schema
- e2b-dev/E2B - Generate MCP server TypedDict models via Makefile
- apache/airflow - Generate OpenAPI datamodels for airflow-ctl and task-sdk via pyproject codegen config
- browser-use/browser-use - Eval dependency
- firebase/genkit - Generate core typing models from JSON Schema
- open-telemetry/opentelemetry-python - Generate SDK configuration dataclasses from JSON Schema
- DataDog/integrations-core - Config models
- argoproj-labs/hera - Makefile
- tensorzero/tensorzero - Generate Python dataclasses from JSON Schema in the schema generation pipeline
- IBM/compliance-trestle - Building models from OSCAL schemas
🔗 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!
👤 Maintainer
📄 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.65.0.tar.gz.
File metadata
- Download URL: datamodel_code_generator-0.65.0.tar.gz
- Upload date:
- Size: 1.4 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccad880007104f5cc462d9f880183fb1ed54f4d8106a3a2ad578861d16548432
|
|
| MD5 |
52af85e70efeb58b009158f6203ca92d
|
|
| BLAKE2b-256 |
aa2802b8ce032f6cd68d530519e0c3570616c7ba28aad377cb19059001f5ef3b
|
Provenance
The following attestation bundles were made for datamodel_code_generator-0.65.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.65.0.tar.gz -
Subject digest:
ccad880007104f5cc462d9f880183fb1ed54f4d8106a3a2ad578861d16548432 - Sigstore transparency entry: 1900518882
- Sigstore integration time:
-
Permalink:
koxudaxi/datamodel-code-generator@19039d08d0e7a565abdba9493c93e80f64e65fec -
Branch / Tag:
refs/tags/0.65.0 - Owner: https://github.com/koxudaxi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@19039d08d0e7a565abdba9493c93e80f64e65fec -
Trigger Event:
push
-
Statement type:
File details
Details for the file datamodel_code_generator-0.65.0-py3-none-any.whl.
File metadata
- Download URL: datamodel_code_generator-0.65.0-py3-none-any.whl
- Upload date:
- Size: 402.0 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 |
2fe413a46932dae930be069b951818a228815cd3056a478527ecc9369a831b60
|
|
| MD5 |
cb11c1ec3961672008c91d46762b2259
|
|
| BLAKE2b-256 |
99dd8e7254f0117a7599622f0509741c1a051946442bf6bc2135df645b6e5d8f
|
Provenance
The following attestation bundles were made for datamodel_code_generator-0.65.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.65.0-py3-none-any.whl -
Subject digest:
2fe413a46932dae930be069b951818a228815cd3056a478527ecc9369a831b60 - Sigstore transparency entry: 1900518985
- Sigstore integration time:
-
Permalink:
koxudaxi/datamodel-code-generator@19039d08d0e7a565abdba9493c93e80f64e65fec -
Branch / Tag:
refs/tags/0.65.0 - Owner: https://github.com/koxudaxi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@19039d08d0e7a565abdba9493c93e80f64e65fec -
Trigger Event:
push
-
Statement type: