You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importosfrompydanticimportBaseModelfrompydantic_settingsimportBaseSettings, SettingsConfigDictos.environ['TEST_nested_nestedNested_var1'] ='test'classNestedNested(BaseModel):
var1: strclassNested(BaseModel):
nestedNested: NestedNestedclassConfig(BaseSettings):
model_config=SettingsConfigDict(
case_sensitive=False,
env_prefix='TEST_',
env_nested_delimiter='_'
)
# optional, it should be None if no env is setnested: Nested|None=Noneif__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.
The text was updated successfully, but these errors were encountered:
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
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
How to reproduce:
requirements.txt
config.py
This throws error:
If I change
case_sensitivity=True
, it works fine.The text was updated successfully, but these errors were encountered: