I'm trying to load settings without defining a class with every property listed (there is one but not accessible by the code I'm writing)
The code for loading the settings manages env vars and json sources, with env vars being the priority and no properties defined, is the following:
from pydantic_settings import BaseSettings as PydanticBaseSettings, PydanticBaseSettingsSource, SettingsConfigDict, JsonConfigSettingsSource
class BaseConfiguration(PydanticBaseSettings):
model_config = SettingsConfigDict(
env_prefix="PREFIX_",
env_nested_delimiter='__',
json_file=["/path/to/file.json"],
extra="allow"
)
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[PydanticBaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return env_settings, JsonConfigSettingsSource(settings_cls)
Having the following json at /path/to/file.json:
{
"random_prop_json": "aaaaa"
}
And having the following env var with a value saved correctly in the os (using devcontainer, saved using launch.json in VS Code but could be saved with any other method):
"PREFIX_random_prop_env": "bbbbb"
Loads only the properties of the json when calling the ctor BaseConfiguration(), resulting only in:
{
"random_prop_json": "aaaaa"
}
When I would expect the instance of BaseConfiguration() to contain something more like:
{
"random_prop_json": "aaaaa",
"random_prop_env": "bbbbb"
}
Tested with the following packages versions:
- pydantic==2.12.5
- pydantic-settings==2.13.1
- pydantic_core==2.41.5
I'm trying to load settings without defining a class with every property listed (there is one but not accessible by the code I'm writing)
The code for loading the settings manages env vars and json sources, with env vars being the priority and no properties defined, is the following:
Having the following json at /path/to/file.json:
And having the following env var with a value saved correctly in the os (using devcontainer, saved using launch.json in VS Code but could be saved with any other method):
Loads only the properties of the json when calling the ctor BaseConfiguration(), resulting only in:
When I would expect the instance of BaseConfiguration() to contain something more like:
Tested with the following packages versions: