Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork427
Python data model generator (Pydantic, dataclasses, TypedDict, msgspec) from OpenAPI, JSON Schema, GraphQL, and raw data (JSON/YAML/CSV).
License
koxudaxi/datamodel-code-generator
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
🚀 Generate Python data models from schema definitions in seconds.
- 📄 ConvertsOpenAPI 3,JSON Schema,GraphQL, and raw data (JSON/YAML/CSV) into Python models
- 🐍 Generates fromexisting Python types (Pydantic, dataclass, TypedDict) via
--input-model - 🎯 GeneratesPydantic v1/v2,dataclasses,TypedDict, ormsgspec 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
- ⚙️pyproject.toml - Configuration file
- 🔄CI/CD Integration - GitHub Actions, pre-commit hooks
- 🚀One-liner Usage - uvx, pipx, clipboard integration
- ❓FAQ - Common questions
uv tool install datamodel-code-generator
Other installation methods
pip:
pip install datamodel-code-generator
uv (add to project):
uv add datamodel-code-generator
conda:
conda install -c conda-forge datamodel-code-generator
With HTTP support (for resolving remote$ref):
pip install'datamodel-code-generator[http]'With GraphQL support:
pip install'datamodel-code-generator[graphql]'Docker:
docker pull koxudaxi/datamodel-code-generator
datamodel-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.jsonfrom __future__importannotationsfromenumimportEnumfromtypingimportOptionalfrompydanticimportBaseModel,FieldclassSpecies(Enum):dog='dog'cat='cat'bird='bird'fish='fish'classPet(BaseModel):name:str=Field(...,description="The pet's name")species:Speciesage:Optional[int]=Field(None,description='Age in years',ge=0)vaccinated:Optional[bool]=False
- OpenAPI 3 (YAML/JSON)
- JSON Schema
- JSON / YAML / CSV data
- GraphQL schema
- Python types (Pydantic, dataclass, TypedDict) via
--input-model - Python dictionary
- pydantic v1 BaseModel
- 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 -p
SeeLLM 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-codegen
Seepyproject.toml Configuration for more options.
Validate generated models in your CI pipeline:
-uses:koxudaxi/datamodel-code-generator@0.44.0with:input:schemas/api.yamloutput:src/models/api.py
SeeCI/CD Integration for more options.
Astral |
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
- fastapi-code-generator - Generate FastAPI app from OpenAPI
- pydantic-pycharm-plugin - PyCharm plugin for Pydantic
SeeDevelopment & Contributing for how to get started!
MIT License - seeLICENSE for details.
About
Python data model generator (Pydantic, dataclasses, TypedDict, msgspec) from OpenAPI, JSON Schema, GraphQL, and raw data (JSON/YAML/CSV).
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.