@@ -371,6 +371,85 @@ print(Settings().model_dump())
371371# > {'numbers': [1, 2, 3]}
372372```
373373
374+ ### Disabling JSON parsing
375+
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 ` :
378+
379+ ``` py
380+ import os
381+
382+ from pydantic import field_validator
383+ from typing_extensions import Annotated
384+
385+ from pydantic_settings import BaseSettings, NoDecode
386+
387+
388+ class Settings (BaseSettings ):
389+ numbers: Annotated[list[int ], NoDecode] # (1)!
390+
391+ @field_validator (' numbers' , mode = ' before' )
392+ @ classmethod
393+ def decode_numbers (cls , v : str ) -> list[int ]:
394+ return [int (x) for x in v.split(' ,' )]
395+
396+
397+ os.environ[' numbers' ] = ' 1,2,3'
398+ print (Settings().model_dump())
399+ # > {'numbers': [1, 2, 3]}
400+ ```
401+
402+ 1 . The ` NoDecode ` annotation disables JSON parsing for the ` numbers ` field. The ` decode_numbers ` method will be called
403+ to parse the value.
404+
405+ You can also disable JSON parsing for all fields by setting the ` enable_decoding ` config setting to ` False ` :
406+
407+ ``` py
408+ import os
409+
410+ from pydantic import field_validator
411+
412+ from pydantic_settings import BaseSettings, SettingsConfigDict
413+
414+
415+ class Settings (BaseSettings ):
416+ model_config = SettingsConfigDict(enable_decoding = False )
417+
418+ numbers: list[int ]
419+
420+ @field_validator (' numbers' , mode = ' before' )
421+ @ classmethod
422+ def decode_numbers (cls , v : str ) -> list[int ]:
423+ return [int (x) for x in v.split(' ,' )]
424+
425+
426+ os.environ[' numbers' ] = ' 1,2,3'
427+ print (Settings().model_dump())
428+ # > {'numbers': [1, 2, 3]}
429+ ```
430+
431+ You can force JSON parsing for a field by annotating it with ` ForceDecode ` . This will bypass
432+ the the ` enable_decoding ` config setting:
433+
434+ ``` py
435+ import os
436+
437+ from typing_extensions import Annotated
438+
439+ from pydantic_settings import BaseSettings, ForceDecode, SettingsConfigDict
440+
441+
442+ class Settings (BaseSettings ):
443+ model_config = SettingsConfigDict(enable_decoding = False )
444+
445+ numbers: Annotated[list[int ], ForceDecode]
446+
447+
448+ os.environ[' numbers' ] = ' ["1","2","3"]'
449+ print (Settings().model_dump())
450+ # > {'numbers': [1, 2, 3]}
451+ ```
452+
374453## Nested model default partial updates
375454
376455By default, Pydantic settings does not allow partial updates to nested model default objects. This behavior can be
0 commit comments