π 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
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
π datamodel-code-generator.koxudaxi.dev
- π₯οΈ CLI Reference - All command-line options
- π§ͺ Playground - Try generation in your browser
- βοΈ pyproject.toml - Configuration file
- π CI/CD Integration - GitHub Actions, pre-commit hooks
- π One-liner Usage - uvx, pipx, clipboard integration
- β FAQ - Common questions
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-generatorFor 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.
Recommended for standalone CLI use:
uv tool install datamodel-code-generatorFor projects that should pin the generator version, add it as a development dependency instead:
uv add --dev datamodel-code-generatorOther installation methods
pip:
pip install datamodel-code-generatoruv (run without adding to project):
uv run --with datamodel-code-generator datamodel-codegen --helpconda:
conda install -c conda-forge datamodel-code-generatorWith 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-generatordatamodel-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- 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
- pydantic v2 BaseModel
- pydantic v2 dataclass
- dataclasses
- TypedDict
- msgspec Struct
Generate a prompt to ask LLMs about CLI options:
datamodel-codegen --generate-prompt "Best options for Pydantic v2?" | claude -pSee LLM Integration for more examples.
pip install 'datamodel-code-generator[http]'
datamodel-codegen --url https://example.com/api/openapi.yaml --output model.py[tool.datamodel-codegen]
input = "schema.yaml"
output = "src/models.py"
output-model-type = "pydantic_v2.BaseModel"Then simply run:
datamodel-codegenSee pyproject.toml Configuration for more options.
Validate generated models in your CI pipeline:
- uses: koxudaxi/datamodel-code-generator@0.44.0
with:
input: schemas/api.yaml
output: src/models/api.pySee CI/CD Integration for more options.
|
Astral |
OpenAI |
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
- fastapi-code-generator - Generate FastAPI app from OpenAPI
- pydantic-pycharm-plugin - PyCharm plugin for Pydantic
See Development & Contributing for how to get started!
MIT License - see LICENSE for details.