@@ -464,6 +464,72 @@ class Settings(BaseSettings):
464464 So if you provide extra values in a dotenv file, whether they start with ` env_prefix ` or not,
465465 a ` ValidationError ` will be raised.
466466
467+ ## Parsing default objects
468+
469+ Pydantic settings uses copy-by-reference when merging default (` BaseModel ` ) objects from different sources. This ensures
470+ the original object reference is maintained in the final object instantiation. However, due to an internal limitation,
471+ it is not possible to partially update a nested sub model field in a default object when using copy-by-reference; the
472+ entirety of the sub model must be provided.
473+
474+ This behavior can be overriden by setting the ` default_objects_copy_by_value ` flag to ` True ` , which will allow partial
475+ updates to sub model fields. Note of course the original default object reference will not be retained.
476+
477+ ``` py
478+ import os
479+
480+ from pydantic import BaseModel, ValidationError
481+
482+ from pydantic_settings import BaseSettings, SettingsConfigDict
483+
484+
485+ class SubModel (BaseModel ):
486+ val: int = 0
487+ flag: bool = False
488+
489+
490+ ORIGINAL_OBJECT = SubModel()
491+
492+
493+ class SettingsCopyByReference (BaseSettings ):
494+ model_config = SettingsConfigDict(env_nested_delimiter = ' __' )
495+
496+ default_object: SubModel = ORIGINAL_OBJECT
497+
498+
499+ class SettingsCopyByValue (BaseSettings ):
500+ model_config = SettingsConfigDict(
501+ env_nested_delimiter = ' __' , default_objects_copy_by_value = True
502+ )
503+
504+ default_object: SubModel = ORIGINAL_OBJECT
505+
506+
507+ by_ref = SettingsCopyByReference()
508+ assert by_ref.default_object is ORIGINAL_OBJECT
509+
510+ # Apply a partial update to the default object using environment variables
511+ os.environ[' DEFAULT_OBJECT__FLAG' ] = ' True'
512+
513+ try :
514+ # Copy by reference will fail
515+ SettingsCopyByReference()
516+ except ValidationError as err:
517+ print (err)
518+ """
519+ 1 validation error for SettingsCopyByReference
520+ nested.val
521+ Field required [type=missing, input_value={'flag': 'TRUE'}, input_type=dict]
522+ For further information visit https://errors.pydantic.dev/2/v/missing
523+ """
524+
525+ # Copy by value will pass
526+ by_val = SettingsCopyByValue()
527+ assert by_val.default_object is not ORIGINAL_OBJECT
528+
529+ print (by_val.model_dump())
530+ # > {'default_object': {'val': 0, 'flag': True}}
531+ ```
532+
467533## Command Line Support
468534
469535Pydantic settings provides integrated CLI support, making it easy to quickly define CLI applications using Pydantic
0 commit comments