datamodel-code-generator
April 4, 2026 ยท View on GitHub
๐ 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
- 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!
๐ License
MIT License - see LICENSE for details.