@@ -18,62 +18,72 @@ datamodel-code-generator allows you to customize how schema types are mapped to
1818
1919## ` --type-mappings `
2020
21- Maps schema types to custom Python types . This is the most flexible way to customize type output .
21+ Maps schema type+format combinations to other format names . This allows you to override how specific formats are interpreted .
2222
2323### Format
2424
2525``` bash
26- --type-mappings < schema_type > =< python_type > [< schema_type >=< python_type > ...]
26+ --type-mappings < type+format > =< target_format > [< type+format >=< target_format > ...]
2727```
2828
2929### Basic Examples
3030
3131``` bash
32- # Map string format to custom type
32+ # Map binary format to string (generates str instead of bytes)
3333datamodel-codegen --input schema.json --output models.py \
34- --type-mappings " string+uri=pydantic.HttpUrl "
34+ --type-mappings " binary=string "
3535
36- # Map integer to custom ID type
36+ # Map email format to string (generates str instead of EmailStr)
3737datamodel-codegen --input schema.json --output models.py \
38- --type-mappings " integer=myproject.types.ID "
38+ --type-mappings " string+email=string "
3939
4040# Multiple mappings
4141datamodel-codegen --input schema.json --output models.py \
42- --type-mappings " string+date-time=datetime.datetime " " string+uri=str "
42+ --type-mappings " string+email=string " " string+uuid=string "
4343```
4444
4545### Mapping Syntax
4646
4747| Syntax | Description | Example |
4848| --------| -------------| ---------|
49- | ` type ` | Map base type | ` integer=int ` |
50- | ` type+format ` | Map type with format | ` string+uuid=uuid.UUID ` |
51- | ` type+format+pattern ` | Map with pattern | ` string++^[A-Z]{2}$=CountryCode ` |
49+ | ` format=target ` | Map format (assumes string type) | ` binary=string ` |
50+ | ` type+format=target ` | Map type with format | ` string+email=string ` |
5251
53- ### Common Mappings
52+ ### Avoiding Pydantic Optional Extras
5453
55- ``` bash
56- # Use AwareDatetime for timezone-aware datetimes
57- --type-mappings " string+date-time=pydantic.AwareDatetime"
58-
59- # Use custom Email type
60- --type-mappings " string+email=myapp.types.Email"
54+ Some Pydantic types require optional dependencies. Use ` --type-mappings ` to generate plain types instead:
6155
62- # Use pathlib.Path for file paths
63- --type-mappings " string+uri-reference=pathlib.Path"
56+ ``` bash
57+ # Avoid pydantic[email] dependency (EmailStr requires email-validator)
58+ --type-mappings " string+email=string" " string+idn-email=string"
6459
65- # Use Decimal for money
66- --type-mappings " number=decimal.Decimal "
60+ # Generate str instead of UUID for uuid format
61+ --type-mappings " string+uuid=string "
6762```
6863
64+ ### Available Target Formats
65+
66+ | Target | Generated Type |
67+ | --------| ----------------|
68+ | ` string ` | ` str ` |
69+ | ` integer ` | ` int ` |
70+ | ` number ` | ` float ` |
71+ | ` boolean ` | ` bool ` |
72+ | ` binary ` | ` bytes ` |
73+ | ` date ` | ` datetime.date ` |
74+ | ` date-time ` | ` datetime.datetime ` |
75+ | ` uuid ` | ` UUID ` |
76+ | ` email ` | ` EmailStr ` |
77+ | ` uri ` | ` AnyUrl ` |
78+
6979### pyproject.toml Configuration
7080
7181``` toml
7282[tool .datamodel-codegen ]
7383type-mappings = [
74- " string+date-time=datetime.datetime " ,
75- " string+uuid=uuid.UUID " ,
76- " number=decimal.Decimal " ,
84+ " string+email=string " ,
85+ " string+idn-email=string " ,
86+ " binary=string " ,
7787]
7888```
7989
@@ -264,13 +274,13 @@ class Product(BaseModel):
264274
265275## Common Patterns
266276
267- ### Pattern 1: Financial application
277+ ### Pattern 1: Minimal dependencies
278+
279+ Avoid optional Pydantic dependencies by mapping special formats to plain types:
268280
269281``` bash
270282datamodel-codegen --input schema.json --output models.py \
271- --use-decimal-for-multiple-of \
272- --type-mappings " number=decimal.Decimal" \
273- --strict-types str int
283+ --type-mappings " string+email=string" " string+idn-email=string"
274284```
275285
276286### Pattern 2: Strict API validation
@@ -282,14 +292,12 @@ datamodel-codegen --input schema.json --output models.py \
282292 --field-constraints
283293```
284294
285- ### Pattern 3: Custom type library
295+ ### Pattern 3: Financial application
286296
287297``` bash
288298datamodel-codegen --input schema.json --output models.py \
289- --type-mappings \
290- " string+email=myapp.types.Email" \
291- " string+uri=myapp.types.URL" \
292- " integer=myapp.types.ID"
299+ --use-decimal-for-multiple-of \
300+ --strict-types str int
293301```
294302
295303### Pattern 4: Pendulum datetime handling
@@ -306,15 +314,16 @@ datamodel-codegen --input schema.json --output models.py \
306314
307315### Common Format Mappings
308316
309- | Schema Format | Default Type | Common Custom Mapping |
310- | ---------------| --------------| ----------------------|
311- | ` date-time ` | ` datetime ` | ` pydantic.AwareDatetime ` , ` pendulum.DateTime ` |
312- | ` date ` | ` date ` | ` pendulum.Date ` |
313- | ` time ` | ` time ` | ` pendulum.Time ` |
317+ | Schema Format | Default Type | With ` --type-mappings "format=string" ` |
318+ | ---------------| --------------| ----------------------------------------|
319+ | ` email ` | ` EmailStr ` | ` str ` |
320+ | ` idn-email ` | ` EmailStr ` | ` str ` |
314321| ` uuid ` | ` UUID ` | ` str ` |
315- | ` email ` | ` EmailStr ` | ` str ` , custom Email class |
316- | ` uri ` | ` AnyUrl ` | ` str ` , ` pydantic.HttpUrl ` |
317- | ` binary ` | ` bytes ` | ` str ` (base64-encoded) |
322+ | ` uri ` | ` AnyUrl ` | ` str ` |
323+ | ` binary ` | ` bytes ` | ` str ` |
324+
325+ !!! note "Other type customization options"
326+ For datetime types, use ` --output-datetime-class ` or ` --use-pendulum ` instead of ` --type-mappings ` .
318327
319328---
320329
0 commit comments