Skip to content

datamodel-code-generator

πŸš€ Generate Python data models from schema definitions in seconds.

Getting Started Open Playground Lead maintainer available for work

PyPI version Conda-forge Downloads PyPI - Python Version codecov license Pydantic v2


✨ What it does

Schema files, raw data, and existing Python models flow through datamodel-code-generator into Python model output types Schema files, raw data, and existing Python models flow through datamodel-code-generator into Python model output types

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

πŸ§ͺ Try It In Your Browser

Generate models in your browser without installing anything.

Playground privacy

Generation runs locally in your browser with Pyodide. Your schema 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.


πŸš€ Start Here

Install the CLI and generate your first model from Getting Started.

Default output model

When --output-model-type is omitted, datamodel-code-generator generates Pydantic v2 BaseModel output (pydantic_v2.BaseModel). Use --output-model-type explicitly when you want dataclasses, TypedDict, or msgspec output.


πŸ“₯ Choose Your Input

Input Type File Types Example
πŸ“˜ OpenAPI 3.0/3.1/3.2 .yaml, .json API specifications
πŸ“‘ AsyncAPI .yaml, .json Event-driven API specifications
πŸ“‹ JSON Schema .json, .yaml Data validation schemas
πŸͺΆ Apache Avro .avsc, .json Avro schemas
🧾 XML Schema .xsd XML document schemas
🧩 Protocol Buffers / gRPC .proto Protobuf messages and service schemas
πŸ”· GraphQL .graphql GraphQL type definitions
πŸ› οΈ MCP Tool Schemas .json, .yaml MCP tool input/output schemas
πŸ“Š JSON/YAML/CSV Data .json, .yaml, .csv Infer schema from data
🐍 Python Models .py Pydantic, dataclass, TypedDict

βœ… 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.


πŸ“€ Choose Your Output

# πŸ†• Pydantic v2 (recommended for new projects)
datamodel-codegen --output-model-type pydantic_v2.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

CLI option quick starts

Use these starting points when combining options; each option links to the generated CLI reference for details and examples.

See the CLI Reference for the full option list and category-specific 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

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:

.github/workflows/validate-models.yml
# Replace vX.Y.Z with a released action version.
- uses: koxudaxi/datamodel-code-generator@vX.Y.Z
  with:
    input: schemas/api.yaml
    output: src/models/api.py

See CI/CD Integration for more options.


πŸ“š Next Steps


πŸ’– Sponsors

Astral Logo

Astral

OpenAI Logo

OpenAI


🏒 Used by

These public examples are grouped by how each project uses datamodel-code-generator.

Code generation and runtime integration

Development, testing, and evaluation

See all dependents β†’


πŸ‘₯ Maintainers