@@ -373,8 +373,9 @@ print(Settings().model_dump())
373373
374374### Disabling JSON parsing
375375
376- pydatnic-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 by your own, you can annotate the field with ` NoDecode ` :
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 ) :
378379
379380``` py
380381import os
@@ -430,13 +431,14 @@ print(Settings().model_dump())
430431# > {'numbers': [1, 2, 3]}
431432```
432433
433- You can force JSON parsing for a field by annotating it with ` ForceDecode ` . This will bypass
434- the the ` enable_decoding ` config setting:
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 the ` enable_decoding ` config setting:
435436
436437``` py
437438import os
438439from typing import List
439440
441+ from pydantic import field_validator
440442from typing_extensions import Annotated
441443
442444from pydantic_settings import BaseSettings, ForceDecode, SettingsConfigDict
@@ -446,13 +448,23 @@ class Settings(BaseSettings):
446448 model_config = SettingsConfigDict(enable_decoding = False )
447449
448450 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(' ,' )]
449457
450458
451459os.environ[' numbers' ] = ' ["1","2","3"]'
460+ os.environ[' numbers1' ] = ' 1,2,3'
452461print (Settings().model_dump())
453- # > {'numbers': [1, 2, 3]}
462+ # > {'numbers': [1, 2, 3], 'numbers1': [1, 2, 3] }
454463```
455464
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+
456468## Nested model default partial updates
457469
458470By default, Pydantic settings does not allow partial updates to nested model default objects. This behavior can be
0 commit comments