@@ -371,6 +371,100 @@ print(Settings().model_dump())
371
371
# > {'numbers': [1, 2, 3]}
372
372
```
373
373
374
+ ### Disabling JSON parsing
375
+
376
+ pydantic-settings by default parses complex types from environment variables as JSON strings. If you want to disable
377
+ this behavior for a field and parse the value in your own validator, you can annotate the field with
378
+ [ ` NoDecode ` ] ( ../api/pydantic_settings.md#pydantic_settings.NoDecode ) :
379
+
380
+ ``` py
381
+ import os
382
+ from typing import List
383
+
384
+ from pydantic import field_validator
385
+ from typing_extensions import Annotated
386
+
387
+ from pydantic_settings import BaseSettings, NoDecode
388
+
389
+
390
+ class Settings (BaseSettings ):
391
+ numbers: Annotated[List[int ], NoDecode] # (1)!
392
+
393
+ @field_validator (' numbers' , mode = ' before' )
394
+ @ classmethod
395
+ def decode_numbers (cls , v : str ) -> List[int ]:
396
+ return [int (x) for x in v.split(' ,' )]
397
+
398
+
399
+ os.environ[' numbers' ] = ' 1,2,3'
400
+ print (Settings().model_dump())
401
+ # > {'numbers': [1, 2, 3]}
402
+ ```
403
+
404
+ 1 . The ` NoDecode ` annotation disables JSON parsing for the ` numbers ` field. The ` decode_numbers ` field validator
405
+ will be called to parse the value.
406
+
407
+ You can also disable JSON parsing for all fields by setting the ` enable_decoding ` config setting to ` False ` :
408
+
409
+ ``` py
410
+ import os
411
+ from typing import List
412
+
413
+ from pydantic import field_validator
414
+
415
+ from pydantic_settings import BaseSettings, SettingsConfigDict
416
+
417
+
418
+ class Settings (BaseSettings ):
419
+ model_config = SettingsConfigDict(enable_decoding = False )
420
+
421
+ numbers: List[int ]
422
+
423
+ @field_validator (' numbers' , mode = ' before' )
424
+ @ classmethod
425
+ def decode_numbers (cls , v : str ) -> List[int ]:
426
+ return [int (x) for x in v.split(' ,' )]
427
+
428
+
429
+ os.environ[' numbers' ] = ' 1,2,3'
430
+ print (Settings().model_dump())
431
+ # > {'numbers': [1, 2, 3]}
432
+ ```
433
+
434
+ You can force JSON parsing for a field by annotating it with [ ` ForceDecode ` ] ( ../api/pydantic_settings.md#pydantic_settings.ForceDecode ) .
435
+ This will bypass the ` enable_decoding ` config setting:
436
+
437
+ ``` py
438
+ import os
439
+ from typing import List
440
+
441
+ from pydantic import field_validator
442
+ from typing_extensions import Annotated
443
+
444
+ from pydantic_settings import BaseSettings, ForceDecode, SettingsConfigDict
445
+
446
+
447
+ class Settings (BaseSettings ):
448
+ model_config = SettingsConfigDict(enable_decoding = False )
449
+
450
+ numbers: Annotated[List[int ], ForceDecode]
451
+ numbers1: List[int ] # (1)!
452
+
453
+ @field_validator (' numbers1' , mode = ' before' )
454
+ @ classmethod
455
+ def decode_numbers1 (cls , v : str ) -> List[int ]:
456
+ return [int (x) for x in v.split(' ,' )]
457
+
458
+
459
+ os.environ[' numbers' ] = ' ["1","2","3"]'
460
+ os.environ[' numbers1' ] = ' 1,2,3'
461
+ print (Settings().model_dump())
462
+ # > {'numbers': [1, 2, 3], 'numbers1': [1, 2, 3]}
463
+ ```
464
+
465
+ 1 . The ` numbers1 ` field is not annotated with ` ForceDecode ` , so it will not be parsed as JSON.
466
+ and we have to provide a custom validator to parse the value.
467
+
374
468
## Nested model default partial updates
375
469
376
470
By default, Pydantic settings does not allow partial updates to nested model default objects. This behavior can be
0 commit comments