Pydantic Settings
Settings management using Pydantic. Provides BaseSettings for loading config from environment variables, .env files, and secrets files with full type validation. Extracted from pydantic core into a separate package in V2. Current version is 2.13.1 (Feb 2026).
Warnings
- breaking 'from pydantic import BaseSettings' raises ImportError in pydantic V2. BaseSettings moved to separate pydantic-settings package.
- gotcha If env_file path does not exist, pydantic-settings silently ignores it with no error. Fields fall back to environment variables or defaults. Wrong .env path causes silent misconfiguration.
- gotcha Environment variables take precedence over .env file values. If the same variable is set in both the shell environment and .env, the shell value wins.
- gotcha env_file in 2.13.x has a regression where .env files are not loaded when using CliApp.run() without debugger attached. Known issue, tracked in pydantic-settings #795.
- gotcha Nested settings via env_nested_delimiter require sub-models to inherit from pydantic.BaseModel, not BaseSettings.
- gotcha Inner class Config: is V1 style and deprecated. Use model_config = SettingsConfigDict(...) at class level.
- deprecated Passing settings config as constructor underscore kwargs (_env_file, _env_prefix) is deprecated. Use model_config = SettingsConfigDict(...) instead.
Install
-
pip install pydantic-settings -
pip install 'pydantic-settings[toml]' -
pip install 'pydantic-settings[yaml]'
Imports
- BaseSettings
from pydantic_settings import BaseSettings, SettingsConfigDict class Settings(BaseSettings): model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8') api_key: str debug: bool = False settings = Settings() - SettingsConfigDict
from pydantic_settings import BaseSettings, SettingsConfigDict class Settings(BaseSettings): model_config = SettingsConfigDict( env_file='.env', env_prefix='APP_', case_sensitive=False ) api_key: str
Quickstart
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file='.env',
env_file_encoding='utf-8',
extra='ignore' # ignore unknown env vars
)
api_key: str
debug: bool = False
port: int = 8000
# reads from env vars and .env file
settings = Settings()
print(settings.api_key)