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