🐍 Generate from Python Models¶
Generate code from existing Python types: Pydantic models, dataclasses, TypedDict, or dict schemas. This is useful for converting between model types or generating from programmatically-defined schemas.
🚀 Quick Start¶
Format¶
Simply specify the file path and object name separated by :.
Example¶
myproject/
├── src/
│ └── models/
│ └── user.py # class User(BaseModel): ...
└── schemas.py # USER_SCHEMA = {...}
# From file path (recommended - easy to copy-paste)
datamodel-codegen --input-model src/models/user.py:User --output model.py
datamodel-codegen --input-model ./schemas.py:USER_SCHEMA --input-file-type jsonschema
# Windows paths also work
datamodel-codegen --input-model src\models\user.py:User
Copy-paste friendly
Just copy the file path from your editor or file explorer, add :ClassName, and you're done!
Module format (alternative)¶
You can also use Python module notation with dots:
datamodel-codegen --input-model src.models.user:User
datamodel-codegen --input-model schemas:USER_SCHEMA --input-file-type jsonschema
Current directory is auto-added to sys.path
No PYTHONPATH configuration needed.
File and package name conflict
If both mymodule.py and mymodule/ directory exist, use ./ prefix:
Supported Input Types¶
| Type | Description | Requires |
|---|---|---|
| Pydantic BaseModel | Pydantic v2 models with model_json_schema() |
Pydantic v2 |
| dataclass | Standard library @dataclass |
Pydantic v2 (for TypeAdapter) |
| Pydantic dataclass | @pydantic.dataclasses.dataclass |
Pydantic v2 |
| TypedDict | typing.TypedDict subclasses |
Pydantic v2 (for TypeAdapter) |
| dict | Dict containing JSON Schema or OpenAPI spec | - |
Pydantic v2 Required
All Python type inputs (except raw dict) require Pydantic v2 runtime to convert to JSON Schema.
📝 Examples¶
Pydantic BaseModel¶
mymodule.py
✨ Generated model.py
from __future__ import annotations
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
Convert Pydantic to TypedDict¶
datamodel-codegen --input-model ./mymodule.py:User --output-model-type typing.TypedDict --output model.py
✨ Generated model.py
from __future__ import annotations
from typing import TypedDict
class User(TypedDict):
name: str
age: int
Standard dataclass¶
mymodule.py
TypedDict¶
mymodule.py
Dict Schema (JSON Schema)¶
mymodule.py
USER_SCHEMA = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
},
"required": ["name", "age"],
}
datamodel-codegen --input-model ./mymodule.py:USER_SCHEMA --input-file-type jsonschema --output model.py
Dict requires --input-file-type
When using a dict schema, you must specify --input-file-type (e.g., jsonschema, openapi).
Dict Schema (OpenAPI)¶
mymodule.py
OPENAPI_SPEC = {
"openapi": "3.0.0",
"info": {"title": "API", "version": "1.0.0"},
"paths": {},
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
},
}
}
},
}
datamodel-codegen --input-model ./mymodule.py:OPENAPI_SPEC --input-file-type openapi --output model.py
Mutual Exclusion¶
--input-model cannot be used with:
--input(file input)--url(URL input)--watch(file watching)
📖 See Also¶
- 🖥️ CLI Reference - Complete CLI options reference
- 📁 CLI Reference: Base Options -
--input-modeloption details - 📋 Generate from JSON Schema - JSON Schema input documentation