Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseSettings fails to load nested models overridden by ENV with case_senitivity=False #277

Open
chuckyblack opened this issue Apr 23, 2024 · 3 comments

Comments

@chuckyblack
Copy link

chuckyblack commented Apr 23, 2024

How to reproduce:

requirements.txt

pydantic==2.7.0
pydantic-settings==2.2.1

config.py

import os
from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict


os.environ['TEST_nested_nestedNested_var1'] = 'test'


class NestedNested(BaseModel):
    var1: str


class Nested(BaseModel):
    nestedNested: NestedNested


class Config(BaseSettings):
    model_config = SettingsConfigDict(
        case_sensitive=False,
        env_prefix='TEST_',
        env_nested_delimiter='_'
    )

    # optional, it should be None if no env is set
    nested: Nested | None = None


if __name__ == '__main__':
    config = Config()

This throws error:

Traceback (most recent call last):
  File "/pydanticTest/src/config.py", line 28, in <module>
    config = Config()
             ^^^^^^^^
  File "/pydanticTest/venv/lib/python3.12/site-packages/pydantic_settings/main.py", line 84, in __init__
    super().__init__(
  File "/pydanticTest/venv/lib/python3.12/site-packages/pydantic/main.py", line 175, in __init__
    self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Config
nested.nestedNested
  Field required [type=missing, input_value={'nestednested': {'var1': 'test'}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.7/v/missing

Process finished with exit code 1

If I change case_sensitivity=True, it works fine.

@hramezani
Copy link
Member

Thanks for reporting this.

pydantic-settings convert all the env variables to lowercase if case_sensitivity=False(the default). that's why it can't find value for the field nestedNested.

This was the default behavior on V1 and we keep it on V2. So, we can't change it in V2

@chuckyblack
Copy link
Author

Is there any reason why this can't be fixed?

What about solution where the field name nestedNested would be coverted to lowercase too during matching?

@hramezani
Copy link
Member

Is there any reason why this can't be fixed?

I think it needs a big change in pydantic-settings and it can't be done in V2 because it will introduce some breaking changes.

What about solution where the field name nestedNested would be coverted to lowercase too during matching?

This is also happening. That's why you see the lowercase fieldname in error input_value={'nestednested': {'var1': 'test'}} but pydantic needs the real field name nestedNested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants