diff --git a/pydantic_settings/sources.py b/pydantic_settings/sources.py index 3048172..1bfceb6 100644 --- a/pydantic_settings/sources.py +++ b/pydantic_settings/sources.py @@ -683,7 +683,7 @@ def explode_env_vars(self, field_name: str, field: FieldInfo, env_vars: Mapping[ is_complex, allow_json_failure = self._field_is_complex(target_field) else: # nested field type is dict - is_complex, allow_json_failure = True, False + is_complex, allow_json_failure = True, True if is_complex: try: env_val = self.decode_complex_value(last_key, target_field, env_val) # type: ignore diff --git a/tests/test_settings.py b/tests/test_settings.py index afd76c4..5b6f2ce 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -3789,6 +3789,17 @@ class Settings(BaseSettings): assert s.model_dump() == {'nested': {'foo': {'a': 1}}, 'sub_dict': {'bar': {'foo': {'b': 2}}}} +def test_env_nested_dict_value(env): + class Settings(BaseSettings): + nested: Dict[str, Dict[str, Dict[str, str]]] + + model_config = SettingsConfigDict(env_nested_delimiter='__') + + env.set('nested__foo__a__b', 'bar') + s = Settings() + assert s.model_dump() == {'nested': {'foo': {'a': {'b': 'bar'}}}} + + def test_nested_models_leaf_vs_deeper_env_dict_assumed(env): class NestedSettings(BaseModel): foo: str