0
# Data Types and Configuration
1
2
Core enums, constants, and configuration types that control the behavior of datamodel-code-generator. These types define supported input formats, output model types, and various generation options.
3
4
## Capabilities
5
6
### Input Format Types
7
8
Enum defining all supported input schema formats with automatic detection capability.
9
10
```python { .api }
11
class InputFileType(Enum):
12
"""Supported input schema formats for code generation."""
13
14
Auto = "auto" # Automatic format detection
15
OpenAPI = "openapi" # OpenAPI 3.x specification
16
JsonSchema = "jsonschema" # JSON Schema (Draft 4, 6, 7, 2019-09, 2020-12)
17
Json = "json" # Raw JSON data (converted to JSON Schema)
18
Yaml = "yaml" # Raw YAML data (converted to JSON Schema)
19
Dict = "dict" # Python dictionary (converted to JSON Schema)
20
CSV = "csv" # CSV data (inferred schema from headers/first row)
21
GraphQL = "graphql" # GraphQL schema definition
22
```
23
24
### Output Model Types
25
26
Enum defining all supported Python model types for code generation output.
27
28
```python { .api }
29
class DataModelType(Enum):
30
"""Supported Python model types for generated output."""
31
32
PydanticBaseModel = "pydantic.BaseModel" # Pydantic v1 BaseModel
33
PydanticV2BaseModel = "pydantic_v2.BaseModel" # Pydantic v2 BaseModel
34
DataclassesDataclass = "dataclasses.dataclass" # Python dataclasses
35
TypingTypedDict = "typing.TypedDict" # TypedDict for type hints
36
MsgspecStruct = "msgspec.Struct" # msgspec Struct (high-performance)
37
```
38
39
### OpenAPI Parsing Scopes
40
41
Configuration for which parts of an OpenAPI specification to process during code generation.
42
43
```python { .api }
44
class OpenAPIScope(Enum):
45
"""OpenAPI specification sections to include in parsing."""
46
47
Schemas = "schemas" # Component schemas (data models)
48
Paths = "paths" # API endpoint definitions
49
Tags = "tags" # API operation groupings
50
Parameters = "parameters" # Reusable parameter definitions
51
```
52
53
### GraphQL Parsing Scopes
54
55
Configuration for GraphQL schema parsing scope.
56
57
```python { .api }
58
class GraphQLScope(Enum):
59
"""GraphQL schema sections to include in parsing."""
60
61
Schema = "schema" # Full GraphQL schema definition
62
```
63
64
### Python Version Targeting
65
66
Enum for specifying target Python version compatibility in generated code.
67
68
```python { .api }
69
class PythonVersion(Enum):
70
"""Python version targets for generated code compatibility."""
71
72
PY_39 = "3.9"
73
PY_310 = "3.10"
74
PY_311 = "3.11"
75
PY_312 = "3.12"
76
PY_313 = "3.13"
77
78
# Constants
79
PythonVersionMin: PythonVersion = PythonVersion.PY_39 # Minimum supported version
80
```
81
82
### Code Formatter Types
83
84
Enum defining available code formatters for post-processing generated code.
85
86
```python { .api }
87
class Formatter(Enum):
88
"""Available code formatters for generated Python code."""
89
90
BLACK = "black" # Black code formatter
91
ISORT = "isort" # Import statement sorter
92
RUFF_CHECK = "ruff-check" # Ruff linter (check mode)
93
RUFF_FORMAT = "ruff-format" # Ruff formatter
94
95
# Default formatter configuration
96
DEFAULT_FORMATTERS: list[Formatter] = [Formatter.BLACK, Formatter.ISORT]
97
```
98
99
### Datetime Class Options
100
101
Enum for specifying datetime class types in generated models.
102
103
```python { .api }
104
class DatetimeClassType(Enum):
105
"""Datetime class types for temporal field generation."""
106
107
Datetime = "datetime" # Standard datetime.datetime
108
Awaredatetime = "awaredatetime" # Timezone-aware datetime
109
Naivedatetime = "naivedatetime" # Timezone-naive datetime
110
```
111
112
### Literal Type Configuration
113
114
Enum controlling how enum fields are converted to literal types.
115
116
```python { .api }
117
class LiteralType(Enum):
118
"""Literal type conversion options for enum fields."""
119
120
All = "all" # Convert all enum fields to literals
121
One = "one" # Convert single-value enums to literals
122
```
123
124
### Strict Type Configuration
125
126
Enum for enabling strict typing validation on specific Python types.
127
128
```python { .api }
129
class StrictTypes(Enum):
130
"""Python types that can have strict validation enabled."""
131
132
str = "str" # Strict string validation
133
bytes = "bytes" # Strict bytes validation
134
int = "int" # Strict integer validation
135
float = "float" # Strict float validation
136
bool = "bool" # Strict boolean validation
137
```
138
139
### Union Mode Configuration (Pydantic v2)
140
141
Enum for configuring union type handling in Pydantic v2 models.
142
143
```python { .api }
144
class UnionMode(Enum):
145
"""Union discrimination modes for Pydantic v2."""
146
147
left_to_right = "left_to_right" # Try union members left to right
148
smart = "smart" # Smart union discrimination
149
```
150
151
### Version Constants
152
153
Constants defining supported Python version range.
154
155
```python { .api }
156
MIN_VERSION: Final[int] = 9 # Minimum Python version (3.9)
157
MAX_VERSION: Final[int] = 13 # Maximum Python version (3.13)
158
```
159
160
### Default Configuration Values
161
162
Common default values used throughout the generation process.
163
164
```python { .api }
165
DEFAULT_BASE_CLASS: str = "pydantic.BaseModel" # Default base class
166
DEFAULT_ENCODING: str = "utf-8" # Default file encoding
167
DEFAULT_MAX_VARIABLE_LENGTH: int = 100 # Debug variable display limit
168
169
# Format module constants
170
DEFAULT_FORMATTERS: list[Formatter] = [Formatter.BLACK, Formatter.ISORT]
171
PythonVersionMin: PythonVersion = PythonVersion.PY_39 # Minimum supported version
172
```
173
174
## Usage Examples
175
176
### Input Type Detection and Configuration
177
```python
178
from datamodel_code_generator import InputFileType, generate
179
180
# Explicit input type specification
181
generate(
182
input_=schema_content,
183
input_file_type=InputFileType.JsonSchema, # Explicit JSON Schema
184
output=output_path
185
)
186
187
# Automatic detection (default)
188
generate(
189
input_=schema_file,
190
input_file_type=InputFileType.Auto, # Auto-detect from content
191
output=output_path
192
)
193
```
194
195
### Output Model Type Selection
196
```python
197
from datamodel_code_generator import DataModelType
198
199
# Generate different model types
200
model_types = [
201
DataModelType.PydanticV2BaseModel, # Modern Pydantic v2
202
DataModelType.DataclassesDataclass, # Standard library dataclasses
203
DataModelType.TypingTypedDict, # Type hints only
204
DataModelType.MsgspecStruct, # High-performance msgspec
205
]
206
207
for model_type in model_types:
208
generate(
209
input_=schema_path,
210
output=Path(f"models_{model_type.value.replace('.', '_')}.py"),
211
output_model_type=model_type
212
)
213
```
214
215
### Python Version Targeting
216
```python
217
from datamodel_code_generator import PythonVersion
218
219
# Target modern Python features
220
generate(
221
input_=schema_path,
222
output=output_path,
223
target_python_version=PythonVersion.PY_311,
224
use_union_operator=True, # Use | instead of Union
225
use_standard_collections=True # Use list instead of List
226
)
227
```
228
229
### OpenAPI Scope Configuration
230
```python
231
from datamodel_code_generator import OpenAPIScope
232
233
# Generate only data models (no path/operation models)
234
generate(
235
input_=openapi_spec,
236
output=output_path,
237
input_file_type=InputFileType.OpenAPI,
238
openapi_scopes=[OpenAPIScope.Schemas] # Only component schemas
239
)
240
241
# Generate comprehensive API models
242
generate(
243
input_=openapi_spec,
244
output=output_path,
245
input_file_type=InputFileType.OpenAPI,
246
openapi_scopes=[
247
OpenAPIScope.Schemas,
248
OpenAPIScope.Paths,
249
OpenAPIScope.Parameters
250
]
251
)
252
```
253
254
### Code Formatter Configuration
255
```python
256
from datamodel_code_generator.format import Formatter
257
258
# Custom formatter chain
259
generate(
260
input_=schema_path,
261
output=output_path,
262
formatters=[
263
Formatter.BLACK, # Format code style
264
Formatter.ISORT, # Sort imports
265
Formatter.RUFF_FORMAT # Additional Ruff formatting
266
]
267
)
268
269
# No formatting (raw output)
270
generate(
271
input_=schema_path,
272
output=output_path,
273
formatters=[] # Skip all formatters
274
)
275
```
276
277
### Strict Typing Configuration
278
```python
279
from datamodel_code_generator.types import StrictTypes
280
281
# Enable strict validation for specific types
282
generate(
283
input_=schema_path,
284
output=output_path,
285
strict_types=[
286
StrictTypes.str, # Strict string validation
287
StrictTypes.int, # Strict integer validation
288
StrictTypes.bool # Strict boolean validation
289
]
290
)
291
```
292
293
### Literal Type Configuration
294
```python
295
from datamodel_code_generator.parser import LiteralType
296
297
# Convert all enums to literals (useful for TypedDict)
298
generate(
299
input_=schema_path,
300
output=output_path,
301
output_model_type=DataModelType.TypingTypedDict,
302
enum_field_as_literal=LiteralType.All
303
)
304
```
305
306
### Pydantic v2 Union Mode
307
```python
308
from datamodel_code_generator.model.pydantic_v2 import UnionMode
309
310
# Configure smart union discrimination
311
generate(
312
input_=schema_path,
313
output=output_path,
314
output_model_type=DataModelType.PydanticV2BaseModel,
315
union_mode=UnionMode.smart # Smart union handling
316
)
317
```