Introduction to datamodel-code-generator
The datamodel-code-generator
is a versatile tool designed to streamline the generation of Python data models from various schema definitions or structured data formats. This project specifically caters to Python developers by providing an automated way to create models compatible with libraries like Pydantic, dataclasses, and more.
Overview
The main function of the datamodel-code-generator
is to parse inputs such as OpenAPI files, JSON Schema, and other data formats, and output Python models. These models can be based on Pydantic's BaseModel, dataclasses, and other types. This tool is particularly helpful for developers working with APIs, where models are frequently needed to handle requests and responses.
Features
-
Supports Multiple Input Formats: It supports OpenAPI 3 (in YAML or JSON), JSON Schema, plain JSON/YAML/CSV data, Python dictionaries, and GraphQL schemas. This flexibility allows developers to use existing schema definitions without any conversions.
-
Generates Various Output Models: Models can be output as Pydantic BaseModel versions (v1 and v2), dataclasses, TypedDict, or msgspec.Struct. There is also support for custom templates via Jinja2, allowing even more customization.
-
Rich Set of Command Options: The generator comes with a variety of command-line options that enable customization of the generated models. You can set specific base classes, handle enums and fields uniquely, choose your target Python version, and more.
Installation
Getting started with datamodel-code-generator
is straightforward. It can be installed via pip:
$ pip install datamodel-code-generator
There are also options to install with extras for handling HTTP and GraphQL, or even use a Docker image from Docker Hub.
Basic Usage
The tool can quickly generate models from a local OpenAPI file with a simple command:
$ datamodel-codegen --input api.yaml --output model.py
This will create a model.py
file, which contains Python classes generated from the specifications in api.yaml
.
Advanced Usage
For more complex scenarios, models can be generated from URLs or with additional HTTP headers and query parameters. Additionally, the tool can be configured to retrieve remote file references over HTTP, making it very adaptable.
Support and Documentation
Users can find more detailed explanations and advanced use cases in the documentation, which provides guidance on all aspects of using the generator effectively.
Contribution and Community
datamodel-code-generator
is part of a larger ecosystem with many projects relying on it for model generation. Some of these projects include airbytehq/airbyte
, apache/iceberg
, and Netflix/consoleme
. The community’s active engagement helps continuously improve the tool with real-world feedback and examples.
Sponsors and Recognition
The project is backed by notable sponsors such as JetBrains, Astral, and Datadog, showcasing its utility and credibility within the industry.
In conclusion, datamodel-code-generator
offers a robust solution for Python developers needing to create sophisticated data models easily and efficiently. Whether you are dealing with complex API integrations or looking to automate model generation from existing schemas, this tool provides all the capabilities needed to integrate seamlessly into your development workflow.