When using oneOf or anyOf, the generated models may not match your expectations. Use --union-mode to control how unions are generated:
# Smart union (Pydantic v2 only) - validates against types in order
datamodel-codegen --union-mode smart --output-model-type pydantic_v2.BaseModel ...
# Left-to-right validation
datamodel-codegen --union-mode left_to_right ...See CLI Reference: --union-mode for details.
Control how allOf schemas merge fields:
# Merge only constraints (minItems, maxItems, pattern, etc.) - default
datamodel-codegen --allof-merge-mode constraints ...
# Merge constraints + annotations (default, examples)
datamodel-codegen --allof-merge-mode all ...
# Don't merge any fields
datamodel-codegen --allof-merge-mode none ...See CLI Reference: --allof-merge-mode for details.
📎 Related: #399
Use a directory as input, or use $ref to reference other files:
# Generate from directory containing multiple schemas
datamodel-codegen --input schemas/ --output models/For schemas with cross-file $ref, ensure you have the HTTP extra for remote refs:
pip install 'datamodel-code-generator[http]'📎 Related: #215
YAML 1.1 treats unquoted keywords like YES, NO, on, off, true, false as boolean values. This tool preserves them as strings to avoid unexpected conversions in schema contexts:
# Input YAML
enum:
- YES
- NO
- NOT_APPLICABLE# Generated code (strings preserved)
class MyEnum(Enum):
YES = 'YES'
NO = 'NO'
NOT_APPLICABLE = 'NOT_APPLICABLE'This matches the expected behavior when type: string is specified in your schema. If you need the previous behavior where YAML bool keywords were converted to Python booleans, please open an issue describing your use case.
📎 Related: #1653, #1766, #2338
If mypy reports errors about conint, constr, or other constrained types, use --field-constraints or --use-annotated:
# Use Field(..., ge=0) instead of conint(ge=0)
datamodel-codegen --field-constraints ...
# Use Annotated[int, Field(ge=0)]
datamodel-codegen --use-annotated ...See Field Constraints for more information.
Ensure you're using the correct target Python version:
datamodel-codegen --target-python-version 3.11 ...This affects type syntax generation (e.g., list[str] vs List[str], X | Y vs Union[X, Y]).
Properties like class, from, import are automatically renamed with a field_ prefix. Control this behavior:
# Custom prefix (default: "field")
datamodel-codegen --special-field-name-prefix my_prefix ...
# Remove special prefix entirely
datamodel-codegen --remove-special-field-name-prefix ...JSON/YAML property names with spaces, dashes, or special characters are converted to valid Python identifiers. An alias is automatically generated to preserve the original name:
class Model(BaseModel):
my_field: str = Field(..., alias='my-field')To disable aliases:
datamodel-codegen --no-alias ...See Field Aliases for custom alias mappings.
datamodel-codegen --snake-case-field ...This generates snake_case field names with camelCase aliases:
class User(BaseModel):
first_name: str = Field(..., alias='firstName')The timestamp in the header changes on each run. Disable it for reproducible output:
datamodel-codegen --disable-timestamp ...Ensure consistent formatting across environments:
# Explicitly set formatters
datamodel-codegen --formatters black isort ...
# Or disable formatting entirely for raw output
datamodel-codegen --formatters ...Also ensure the same Python version and formatter configurations (pyproject.toml) are used.
Use --check mode in CI to verify generated files are up-to-date:
datamodel-codegen --check --input schema.yaml --output models.pyThis exits with code 1 if the output would differ, without modifying files.
For very large schemas with many models:
- Use
--reuse-modelto deduplicate identical models - Consider splitting schemas into multiple files
- Use
--disable-warningsto reduce output
datamodel-codegen --reuse-model --disable-warnings ...See Model Reuse and Deduplication for details.
- Pydantic v2 (
pydantic_v2.BaseModel): ✨ Recommended for new projects. Better performance and modern API. - dataclasses: Simple data containers without validation.
- TypedDict: Type hints for dict structures.
- msgspec: High-performance serialization.
See Output Model Types for a detailed comparison.
# For new projects
datamodel-codegen --output-model-type pydantic_v2.BaseModel ...See Output Model Types for more details.
📎 Related: #803
Ensure the output model type matches your installed Pydantic version:
# Check your Pydantic version
python -c "import pydantic; print(pydantic.VERSION)"
# Generate for Pydantic v2
datamodel-codegen --output-model-type pydantic_v2.BaseModel ...Install the HTTP extra:
pip install 'datamodel-code-generator[http]'For authenticated endpoints:
datamodel-codegen --url https://api.example.com/schema.yaml \
--http-headers "Authorization: Bearer TOKEN" \
--output model.pyFor development/testing with self-signed certificates:
datamodel-codegen --url https://... --http-ignore-tls --output model.py!!! warning "--http-ignore-tls in trusted environments.
Use --read-only-write-only-model-type to generate separate Request/Response models:
# Generate Request/Response models only
datamodel-codegen --read-only-write-only-model-type request-response ...
# Generate Base + Request + Response models
datamodel-codegen --read-only-write-only-model-type all ...📎 Related: #727
Use --strict-nullable to treat nullable fields as truly optional:
datamodel-codegen --strict-nullable ...📎 Related: #327
Use --use-type-alias (experimental) to generate type aliases instead of root models:
datamodel-codegen --use-type-alias --output-model-type pydantic_v2.BaseModel ...See Root Models and Type Aliases for details.
📎 Related: #2505
- 🖥️ CLI Reference - Complete option documentation
- ⚙️ pyproject.toml Configuration - Configure options via file
- 🐛 GitHub Issues - Report bugs or request features
- 💬 Discussions - Ask questions and share ideas