https://github.com/koxudaxi/datamodel-code-generator
Python data model generator (Pydantic, dataclasses, TypedDict, msgspec) from OpenAPI, JSON Schema, GraphQL, and raw data (JSON/YAML/CSV).
https://github.com/koxudaxi/datamodel-code-generator
code-generator csv dataclass datamodel fastapi generator graphql json-schema msgspec openapi openapi-codegen pydantic python swagger swagger-codegen typeddict yaml
Last synced: 10 days ago
JSON representation
Python data model generator (Pydantic, dataclasses, TypedDict, msgspec) from OpenAPI, JSON Schema, GraphQL, and raw data (JSON/YAML/CSV).
- Host: GitHub
- URL: https://github.com/koxudaxi/datamodel-code-generator
- Owner: koxudaxi
- License: mit
- Created: 2019-05-29T08:01:32.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2026-01-07T02:57:44.000Z (19 days ago)
- Last Synced: 2026-01-09T10:58:29.265Z (17 days ago)
- Topics: code-generator, csv, dataclass, datamodel, fastapi, generator, graphql, json-schema, msgspec, openapi, openapi-codegen, pydantic, python, swagger, swagger-codegen, typeddict, yaml
- Language: Python
- Homepage: https://koxudaxi.github.io/datamodel-code-generator/
- Size: 24.3 MB
- Stars: 3,691
- Watchers: 27
- Forks: 424
- Open Issues: 38
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yaml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Support: docs/supported-data-types.md
Awesome Lists containing this project
- awesome-pydantic - datamodel-code-generator - Pydantic model generator for easy conversion of JSON, OpenAPI, JSON Schema, GraphQL Schema, and YAML data sources. (Utilities)
- StarryDivineSky - koxudaxi/datamodel-code-generator - code-generator 是一个代码生成器,用于从 JSON、OpenAPI、JSON Schema 和 YAML 数据源轻松生成 Pydantic 模型和 dataclasses.dataclass。它简化了数据模型的创建过程,避免了手动编写重复代码。该工具通过解析数据源的结构,自动生成对应的 Python 类定义,包括字段类型、验证规则等。这极大地提高了开发效率,并确保数据模型与数据源保持同步。项目支持自定义模板和插件,可以灵活地调整代码生成过程以满足不同的需求。它旨在帮助开发者快速构建可靠且易于维护的数据模型,从而提升整体项目的质量和效率。 (其他_大数据 / 资源传输下载)
- best-of-web-python - GitHub - 37% open · ⏱️ 03.11.2025): (OpenAPI Utilities)
- jimsghstars - koxudaxi/datamodel-code-generator - Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources. (Python)
README
# datamodel-code-generator
🚀 Generate Python data models from schema definitions in seconds.
[](https://pypi.python.org/pypi/datamodel-code-generator)
[](https://anaconda.org/conda-forge/datamodel-code-generator)
[](https://pepy.tech/project/datamodel-code-generator)
[](https://pypi.python.org/pypi/datamodel-code-generator)
[](https://codecov.io/gh/koxudaxi/datamodel-code-generator)

[](https://pydantic.dev)
[](https://pydantic.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 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
---
## 📖 Documentation
**👉 [datamodel-code-generator.koxudaxi.dev](https://datamodel-code-generator.koxudaxi.dev)**
- 🖥️ [CLI Reference](https://datamodel-code-generator.koxudaxi.dev/cli-reference/) - All command-line options
- ⚙️ [pyproject.toml](https://datamodel-code-generator.koxudaxi.dev/pyproject_toml/) - Configuration file
- 🔄 [CI/CD Integration](https://datamodel-code-generator.koxudaxi.dev/ci-cd/) - GitHub Actions, pre-commit hooks
- 🚀 [One-liner Usage](https://datamodel-code-generator.koxudaxi.dev/oneliner/) - uvx, pipx, clipboard integration
- ❓ [FAQ](https://datamodel-code-generator.koxudaxi.dev/faq/) - Common questions
---
## 📦 Installation
```bash
uv tool install datamodel-code-generator
```
Other installation methods
**pip:**
```bash
pip install datamodel-code-generator
```
**uv (add to project):**
```bash
uv add datamodel-code-generator
```
**conda:**
```bash
conda install -c conda-forge datamodel-code-generator
```
**With HTTP support** (for resolving remote `$ref`):
```bash
pip install 'datamodel-code-generator[http]'
```
**With GraphQL support:**
```bash
pip install 'datamodel-code-generator[graphql]'
```
**Docker:**
```bash
docker pull koxudaxi/datamodel-code-generator
```
---
## 🏃 Quick Start
```bash
datamodel-codegen --input schema.json --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --output model.py
```
📄 schema.json (input)
```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
}
}
}
```
🐍 model.py (output)
```python
# 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 v1](https://docs.pydantic.dev/1.10/) BaseModel
- [pydantic v2](https://docs.pydantic.dev/) BaseModel
- [pydantic v2](https://docs.pydantic.dev/) dataclass
- [dataclasses](https://docs.python.org/3/library/dataclasses.html)
- [TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict)
- [msgspec](https://github.com/jcrist/msgspec) Struct
---
## 🍳 Common Recipes
### 🤖 Get CLI Help from LLMs
Generate a prompt to ask LLMs about CLI options:
```bash
datamodel-codegen --generate-prompt "Best options for Pydantic v2?" | claude -p
```
See [LLM Integration](https://datamodel-code-generator.koxudaxi.dev/llm-integration/) for more examples.
### 🌐 Generate from URL
```bash
pip install 'datamodel-code-generator[http]'
datamodel-codegen --url https://example.com/api/openapi.yaml --output model.py
```
### ⚙️ Use with pyproject.toml
```toml
[tool.datamodel-codegen]
input = "schema.yaml"
output = "src/models.py"
output-model-type = "pydantic_v2.BaseModel"
```
Then simply run:
```bash
datamodel-codegen
```
See [pyproject.toml Configuration](https://datamodel-code-generator.koxudaxi.dev/pyproject_toml/) for more options.
### 🔄 CI/CD Integration
Validate generated models in your CI pipeline:
```yaml
- uses: koxudaxi/datamodel-code-generator@0.44.0
with:
input: schemas/api.yaml
output: src/models/api.py
```
See [CI/CD Integration](https://datamodel-code-generator.koxudaxi.dev/ci-cd/) for more options.
---
## 💖 Sponsors
---
## 🏢 Projects that use datamodel-code-generator
These projects use datamodel-code-generator. See the linked examples for real-world usage.
- [PostHog/posthog](https://github.com/PostHog/posthog) - *[Generate models via npm run](https://github.com/PostHog/posthog/blob/e1a55b9cb38d01225224bebf8f0c1e28faa22399/package.json#L41)*
- [airbytehq/airbyte](https://github.com/airbytehq/airbyte) - *[Generate Python, Java/Kotlin, and Typescript protocol models](https://github.com/airbytehq/airbyte-protocol/tree/main/protocol-models/bin)*
- [apache/iceberg](https://github.com/apache/iceberg) - *[Generate Python code](https://github.com/apache/iceberg/blob/d2e1094ee0cc6239d43f63ba5114272f59d605d2/open-api/README.md?plain=1#L39)*
- [open-metadata/OpenMetadata](https://github.com/open-metadata/OpenMetadata) - *[datamodel_generation.py](https://github.com/open-metadata/OpenMetadata/blob/main/scripts/datamodel_generation.py)*
- [awslabs/aws-lambda-powertools-python](https://github.com/awslabs/aws-lambda-powertools-python) - *[Recommended for advanced-use-cases](https://awslabs.github.io/aws-lambda-powertools-python/2.6.0/utilities/parser/#advanced-use-cases)*
- [Netflix/consoleme](https://github.com/Netflix/consoleme) - *[Generate models from Swagger](https://github.com/Netflix/consoleme/blob/master/docs/gitbook/faq.md#how-do-i-generate-models-from-the-swagger-specification)*
- [DataDog/integrations-core](https://github.com/DataDog/integrations-core) - *[Config models](https://github.com/DataDog/integrations-core/blob/master/docs/developer/meta/config-models.md)*
- [argoproj-labs/hera](https://github.com/argoproj-labs/hera) - *[Makefile](https://github.com/argoproj-labs/hera/blob/c8cbf0c7a676de57469ca3d6aeacde7a5e84f8b7/Makefile#L53-L62)*
- [SeldonIO/MLServer](https://github.com/SeldonIO/MLServer) - *[generate-types.sh](https://github.com/SeldonIO/MLServer/blob/master/hack/generate-types.sh)*
- [geojupyter/jupytergis](https://github.com/geojupyter/jupytergis) - *[Python type generation from JSONSchema](https://jupytergis.readthedocs.io/en/latest/contributor_guide/explanation/code-generation.html)*
- [Nike-Inc/brickflow](https://github.com/Nike-Inc/brickflow) - *[Code generate tools](https://github.com/Nike-Inc/brickflow/blob/e3245bf638588867b831820a6675ada76b2010bf/tools/README.md?plain=1#L8)*
- [cloudcoil/cloudcoil](https://github.com/cloudcoil/cloudcoil) - *[Model generation](https://github.com/cloudcoil/cloudcoil#%EF%B8%8F-model-generation)*
- [IBM/compliance-trestle](https://github.com/IBM/compliance-trestle) - *[Building models from OSCAL schemas](https://github.com/IBM/compliance-trestle/blob/develop/docs/contributing/website.md#building-the-models-from-the-oscal-schemas)*
- [hashintel/hash](https://github.com/hashintel/hash) - *[codegen.sh](https://github.com/hashintel/hash/blob/9762b1a1937e14f6b387677e4c7fe4a5f3d4a1e1/libs/%40local/hash-graph-client/python/scripts/codegen.sh#L21-L39)*
[See all dependents →](https://github.com/koxudaxi/datamodel-code-generator/network/dependents)
---
## 🔗 Related Projects
- **[fastapi-code-generator](https://github.com/koxudaxi/fastapi-code-generator)** - Generate FastAPI app from OpenAPI
- **[pydantic-pycharm-plugin](https://github.com/koxudaxi/pydantic-pycharm-plugin)** - PyCharm plugin for Pydantic
---
## 🤝 Contributing
See [Development & Contributing](https://datamodel-code-generator.koxudaxi.dev/development-contributing/) for how to get started!
## 📄 License
MIT License - see [LICENSE](LICENSE) for details.