datamodel-code-generator
Datamodel Code Generator
What it is and what it does
Datamodel-code-generator is a CLI tool and library that converts schema definitions into type-safe Python model code. It reads from OpenAPI 3, AsyncAPI, JSON Schema, Apache Avro, XML Schema, Protocol Buffers, GraphQL, MCP tool schemas, and raw data (JSON/YAML/CSV), then outputs Pydantic v2, Pydantic v2 dataclass, dataclass, TypedDict, or msgspec models. The generated code handles complex schema features like $ref, allOf, oneOf, anyOf, enums, and nested types.
It's designed for developers who need to keep Python models in sync with external schema definitions or who want to avoid hand-writing boilerplate. The tool supports both standalone CLI use and integration as a development dependency in projects. It can also retarget existing Python types (Pydantic, dataclass, TypedDict) to different output formats via the --input-model flag. Output is formatted by default with black and isort, though faster builtin or ruff formatters are available.
Use it for:
- Generate Pydantic models from OpenAPI specs to keep API clients and servers synchronized with schema changes.
- Convert JSON Schema definitions into dataclasses for configuration file parsing with type safety.
- Transform Protocol Buffer definitions into Python models for gRPC service development.
- Generate TypedDict or msgspec models from raw JSON/YAML/CSV data for rapid prototyping.
- Retarget existing Pydantic models to dataclass output for environments that don't use Pydantic.
- Automate model generation in CI/CD pipelines to enforce schema-driven code generation.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Generates Python data models (Pydantic, dataclasses, TypedDict, msgspec) from schema definitions like OpenAPI, JSON Schema, Protocol Buffers, GraphQL, and raw data formats.
Yes. Active maintenance, no known vulnerabilities, low install friction, and permissive MIT license make it a safe choice. The tool solves a real problem—keeping Python models in sync with external schemas—and supports a wide range of input formats. Install as a dev dependency or standalone CLI depending on your workflow.
Install
datamodel-code-generator on PyPI
pip
pip install datamodel-code-generatoruv
uv add datamodel-code-generatorpoetry
poetry add datamodel-code-generatorInstalling datamodel-code-generator
Before you install
Low friction: pure Python wheel with nine runtime dependencies (argcomplete, black, genson, inflect, isort, jinja2, pydantic, pyyaml, tomli). Active maintenance—released 2 days ago with 4002 GitHub stars and commits as recent as 2026-08-14.
License in practice
MIT license (permissive) allows commercial and private use with minimal restrictions; attribution required but no copyleft obligations.
Quickstart
pip install datamodel-code-generator
datamodel-codegen \
--input schema.json \
--input-file-type jsonschema \
--output-model-type pydantic_v2.BaseModel \
--output model.py
Requires Python 3.10 or later; optional extras (http, graphql, protobuf, ruff) needed for specific schema types or formatters.
Verify before relying
- Performance characteristics for very large or deeply nested schemas
- Completeness of schema feature support across all input formats (e.g., edge cases in $ref resolution)
- Whether the experimental httpx2 backend is production-ready
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 9 — argcomplete, black, genson, inflect, isort, jinja2, pydantic, pyyaml, tomli |
| Maintenance | actively maintained — 2 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 17,555,589/month — #1,113 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: datamodel_code_generator-0.72.4-py3-none-any.whl
Tags
More Build Tools packages
Provides reusable utilities for Python…
permissive · top 100 on PyPI
tqdmWraps any iterable to display a real-time…
copyleft · top 100 on PyPI
pippip is the standard installer for Python…
permissive · top 100 on PyPI
hatchlingHatchling is a standards-compliant Python build…
permissive · top 100 on PyPI
grpcio-toolsGenerates Python gRPC service stubs and message…
permissive · top 1,000 on PyPI
pre-commitpre-commit is a framework for installing and…
permissive · top 1,000 on PyPI
dataclasses-avroschemaGenerates Avro schemas from Python dataclasses,…
permissive · top 5,000 on PyPI
py-avro-schemaGenerates Apache Avro schemas from Python…
permissive · top 15,000 on PyPI
jsonschema-gentypesGenerates Python TypedDict types from JSON…
permissive · top 15,000 on PyPI
dataclasses-jsonschemaGenerates JSON Schema from Python dataclasses…
permissive · top 15,000 on PyPI
graphql-queryBuilds GraphQL query strings programmatically…
permissive · top 15,000 on PyPI
apischemaApischema handles JSON…
permissive · top 15,000 on PyPI
polyfactoryPolyfactory generates mock data objects from…
permissive · top 5,000 on PyPI
adaptixAdaptix converts between data models,…
permissive · top 15,000 on PyPI
graphene-pydanticConverts Pydantic models into GraphQL object…
permissive · top 15,000 on PyPI
pydantic-avroConverts between pydantic class definitions and…
permissive · top 5,000 on PyPI