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 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
📦 Installation¶
🏃 Quick Start¶
1️⃣ Create a schema file¶
pet.json
{
"$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
}
}
}
2️⃣ Run the generator¶
datamodel-codegen --input pet.json --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --output model.py
3️⃣ Use your models¶
model.py
# generated by datamodel-codegen:
# filename: tutorial_pet.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
🎉 That's it! Your schema is now a fully-typed Python model.
📥 Choose Your Input¶
| Input Type | File Types | Example |
|---|---|---|
| 📘 OpenAPI 3 | .yaml, .json |
API specifications |
| 📋 JSON Schema | .json |
Data validation schemas |
| 🔷 GraphQL | .graphql |
GraphQL type definitions |
| 📊 JSON/YAML Data | .json, .yaml |
Infer schema from data |
📤 Choose Your Output¶
# 🆕 Pydantic v2 (recommended for new projects)
datamodel-codegen --output-model-type pydantic_v2.BaseModel ...
# 🔄 Pydantic v1 (default, for compatibility)
datamodel-codegen --output-model-type pydantic.BaseModel ...
# 🏗️ Python dataclasses
datamodel-codegen --output-model-type dataclasses.dataclass ...
# 📝 TypedDict (for type hints without validation)
datamodel-codegen --output-model-type typing.TypedDict ...
# ⚡ msgspec (high-performance serialization)
datamodel-codegen --output-model-type msgspec.Struct ...
See Supported Data Types for the full list.
🍳 Common Recipes¶
🤖 Get CLI Help from LLMs¶
Generate a prompt to ask LLMs about CLI options:
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¶
pyproject.toml
[tool.datamodel-codegen]
input = "schema.yaml"
output = "src/models.py"
output-model-type = "pydantic_v2.BaseModel"
Then simply run:
See pyproject.toml Configuration for more options.
🔄 CI/CD Integration¶
Validate generated models in your CI pipeline:
.github/workflows/validate-models.yml
- uses: koxudaxi/[email protected]
with:
input: schemas/api.yaml
output: src/models/api.py
See CI/CD Integration for more options.
📚 Next Steps¶
- 🖥️ CLI Reference - All command-line options with examples
- ⚙️ pyproject.toml Configuration - Configure via pyproject.toml
- 🚀 One-liner Usage - uvx, pipx, clipboard integration
- 🔄 CI/CD Integration - GitHub Actions and CI validation
- 🎨 Custom Templates - Customize generated code with Jinja2
- 🖌️ Code Formatting - Configure black, isort, and ruff
- ❓ FAQ - Common questions and troubleshooting
💖 Sponsors¶
|
Astral |
🏢 Used by¶
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