import os
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
nested_field: dict[str, str]
model_config = SettingsConfigDict(env_nested_delimiter="__")
# This works
os.environ["nested_field"] = '{"key": "some value"}'
print(Settings()) # nested_field={'key': 'some value'}
# This fails in v2.13
os.environ["nested_field__key"] = "some value"
print(Settings()) # AttributeError
Traceback (most recent call last):
File ".../script.py", line 20, in <module>
print(Settings())
^^^^^^^^^^
File ".../python3.12/site-packages/pydantic_settings/main.py", line 242, in __init__
super().__init__(**__pydantic_self__.__class__._settings_build_values(sources, init_kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../python3.12/site-packages/pydantic_settings/main.py", line 465, in _settings_build_values
source_state = source()
^^^^^^^^
File ".../python3.12/site-packages/pydantic_settings/sources/base.py", line 550, in __call__
field_value = self.prepare_field_value(field_name, field, field_value, value_is_complex)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../python3.12/site-packages/pydantic_settings/sources/providers/env.py", line 121, in prepare_field_value
env_val_built = self.explode_env_vars(field_name, field, self.env_vars)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../python3.12/site-packages/pydantic_settings/sources/providers/env.py", line 257, in explode_env_vars
is_complex, allow_json_failure = self._field_is_complex(target_field)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../python3.12/site-packages/pydantic_settings/sources/providers/env.py", line 144, in _field_is_complex
if self.field_is_complex(field):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../python3.12/site-packages/pydantic_settings/sources/base.py", line 157, in field_is_complex
return _annotation_is_complex(field.annotation, field.metadata)
^^^^^^^^^^^^^^^^
AttributeError: type object 'str' has no attribute 'annotation'
The regression appeared with version 2.13.0, same code executed with version 2.12.0 works fine.
When using
env_nested_delimiterto parse nested environment variables into adict[str, str]field, pydantic-settings 2.13 raises anAttributeError: type object 'str' has no attribute 'annotation'.Steps to reproduce:
Error traceback:
Environment:
The regression appeared with version 2.13.0, same code executed with version 2.12.0 works fine.