Generated code is automatically formatted using code formatters. By default, black and isort are used to produce consistent, well-formatted output.
!!! warning "Future Change"
In a future version, the default formatters will change from black and isort to ruff.
To prepare for this change, consider switching to ruff now using --formatters ruff-format ruff-check.
**CLI users**: To suppress this warning, use `--disable-warnings` or explicitly specify `--formatters black isort`.
**Library users**: Explicitly pass `formatters=[Formatter.BLACK, Formatter.ISORT]` to suppress this warning.
datamodel-codegen \
--input schema.yaml \
--output-model-type pydantic_v2.BaseModel \
--output model.pyThis runs the following formatters in order:
- isort - Sorts and organizes imports
- black - Formats code style
| Formatter | Description |
|---|---|
black |
Code formatting (PEP 8 style) |
isort |
Import sorting |
ruff-check |
Linting with auto-fix |
ruff-format |
Fast code formatting (black alternative) |
Ruff is a fast Python linter and formatter. To use it:
!!! note "Installation Required"
ruff is an optional dependency. Install it with:
bash pip install 'datamodel-code-generator[ruff]'
# Use ruff for both linting and formatting
datamodel-codegen \
--formatters ruff-check ruff-format \
--input schema.yaml \
--output-model-type pydantic_v2.BaseModel \
--output model.py
# Use ruff-format as a black replacement
datamodel-codegen \
--formatters isort ruff-format \
--input schema.yaml \
--output-model-type pydantic_v2.BaseModel \
--output model.pydatamodel-codegen requires at least one formatter when using the CLI --formatters option.
To disable built-in formatting entirely, configure it via pyproject.toml:
[tool.datamodel-codegen]
formatters = []Formatters read their configuration from pyproject.toml. The tool searches for pyproject.toml in:
- The output file's directory
- Parent directories (up to the git repository root)
[tool.black]
line-length = 100
skip-string-normalization = true
[tool.isort]
profile = "black"
line_length = 100
[tool.ruff]
line-length = 100
[tool.ruff.format]
quote-style = "single"By default, string quote style is determined by your formatter configuration. To force double quotes regardless of configuration:
datamodel-codegen \
--use-double-quotes \
--input schema.yaml \
--output-model-type pydantic_v2.BaseModel \
--output model.pyThis overrides skip_string_normalization in black config.
You can create custom formatters for specialized formatting needs. See Custom Formatters for details.
- 🖥️ CLI Reference:
--formatters- Specify code formatters - 💬 CLI Reference:
--use-double-quotes- Force double quotes - 🎨 Custom Formatters - Create your own formatters
- ⚙️ pyproject.toml Configuration - Configure datamodel-codegen options