Skip to content

AttributeError with nested environment variables in version 2.13.0 #785

@alesanfra

Description

@alesanfra

When using env_nested_delimiter to parse nested environment variables into a dict[str, str] field, pydantic-settings 2.13 raises an AttributeError: type object 'str' has no attribute 'annotation'.

Steps to reproduce:

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

Error traceback:

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'

Environment:

  • pydantic-settings: 2.13
  • Python: 3.12
  • OS: macOS 15.7.3 (Apple Silicon)

The regression appeared with version 2.13.0, same code executed with version 2.12.0 works fine.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions