Pydantic Settings YAML
raw JSON → 0.2.0 verified Mon Apr 27 auth: no python
Adds YAML support to pydantic-settings, enabling loading settings from YAML files via the BaseSettings model. Currently at version 0.2.0, requires Python >=3.8. Released irregularly as a small utility package.
pip install pydantic-settings-yaml Common errors
error ImportError: cannot import name 'YamlSettingsSource' from 'pydantic_settings_yaml' ↓
cause The class was renamed to YamlConfigSettingsSource in v0.2.0.
fix
Use: from pydantic_settings_yaml import YamlConfigSettingsSource
error pydantic_settings.sources.SettingsError: Unknown settings source type: <class '...'> ↓
cause The custom settings source is not correctly registered or imported.
fix
Ensure YamlConfigSettingsSource is imported and passed to settings_customise_sources if needed.
Warnings
breaking In version 0.2.0, the class was renamed from YamlSettingsSource to YamlConfigSettingsSource. Existing code using the old name will break. ↓
fix Update imports: from pydantic_settings_yaml import YamlConfigSettingsSource
gotcha The YAML file is loaded only during initialization. If the file changes later, the settings are not refreshed automatically. ↓
fix Call Settings() again or implement a reload mechanism.
gotcha Settings are case-sensitive. YAML keys must exactly match the model field names. ↓
fix Ensure field names in model and YAML keys match exactly.
Imports
- YamlConfigSettingsSource wrong
from pydantic_settings_yaml import YamlSettingsSourcecorrectfrom pydantic_settings_yaml import YamlConfigSettingsSource
Quickstart
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic_settings_yaml import YamlConfigSettingsSource
class Settings(BaseSettings):
model_config = SettingsConfigDict(yaml_file='config.yaml')
app_name: str = "MyApp"
debug: bool = False
settings = Settings()
print(settings.app_name)
print(settings.debug)