⚙️ pyproject.toml Configuration¶
datamodel-code-generator can be configured using pyproject.toml. The tool automatically searches for pyproject.toml in the current directory and parent directories (stopping at the git repository root).
HTTP client selection uses the same values as --http-backend. For example,
the experimental HTTPX2 pair can be required explicitly:
The default is "auto", which prefers stable HTTPX. See
HTTP backend selection.
🚀 Basic Usage¶
[tool.datamodel-codegen]
input = "schema.yaml"
output = "models.py"
target-python-version = "3.11"
snake-case-field = true
field-constraints = true
All CLI options can be used in pyproject.toml by converting them to kebab-case (e.g., --snake-case-field becomes snake-case-field).
Formatter settings¶
Formatter selection is configured with the same keys as the CLI:
builtin-format-line-length is used only by the built-in formatter. It controls wrapping for from ... import ... statements and generated model statements.
If it is not set, the built-in formatter reads existing formatter settings in this order:
[tool.ruff].line-length[tool.black].line-length[tool.isort].line_length88
See Formatter Behavior for the full built-in formatter scope.
📋 Named Profiles¶
You can define multiple named profiles for different use cases within a single project:
[tool.datamodel-codegen]
target-python-version = "3.10"
snake-case-field = true
[tool.datamodel-codegen.profiles.api]
input = "schemas/api.yaml"
output = "src/models/api.py"
target-python-version = "3.11"
[tool.datamodel-codegen.profiles.database]
input = "schemas/db.json"
output = "src/models/db.py"
input-file-type = "jsonschema"
Base settings in [tool.datamodel-codegen] are used when no profile is specified, and also serve as defaults for profiles.
Use a profile with the --profile option:
🎯 Configuration Priority¶
Settings are applied in the following priority order (highest to lowest):
- 🖥️ CLI arguments - Always take precedence
- 📋 Profile settings - From
[tool.datamodel-codegen.profiles.<name>] - ⚙️ Base settings - From
[tool.datamodel-codegen] - 🔧 Default values - Built-in defaults
🔀 Merge Rules¶
When using profiles, settings are merged using shallow merge:
- Profile values completely replace base values (no deep merging)
- Settings not specified in the profile are inherited from the base configuration
- Lists and dictionaries are replaced entirely, not merged
📝 Example¶
[tool.datamodel-codegen]
strict-types = ["str", "int"]
http-headers = ["Authorization: Bearer token"]
[tool.datamodel-codegen.profiles.api]
strict-types = ["bytes"]
When using --profile api:
strict-typesbecomes["bytes"](completely replaces base, not merged)http-headersis inherited from base as["Authorization: Bearer token"]
🚫 Ignoring pyproject.toml¶
To ignore all pyproject.toml configuration and use only CLI arguments:
🔧 Generating Configuration¶
Generate a pyproject.toml configuration section from CLI arguments:
datamodel-codegen --input schema.yaml --output models.py --snake-case-field --generate-pyproject-config
✨ Output:
Generate CLI command from existing pyproject.toml:
With a specific profile:
📖 See Also¶
- 🧰 Presets - Recommended immutable option bundles for modern output
- 🖥️ CLI Reference:
--ignore-pyproject- Ignore pyproject.toml configuration - 🔧 CLI Reference:
--generate-pyproject-config- Generate pyproject.toml from CLI arguments - 🖥️ CLI Reference:
--generate-cli-command- Generate CLI command from pyproject.toml