13 KiB
name, description, x-personal-mcp
| name | description | x-personal-mcp | |||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| pydantic-settings | Practical guide for implementing typed application configuration with pydantic-settings. Use when designing BaseSettings models, choosing nested or independent settings boundaries, managing settings lifecycles, configuring dotenv or secrets, and customizing source priority safely. |
|
Pydantic Settings Implementation Guide
Use this skill to implement robust, typed application configuration with pydantic-settings in production Python services.
When to Use
- You need a single typed configuration model for app settings.
- You are migrating from ad-hoc
os.getenv(...)calls. - You need predictable precedence across init args, env vars, dotenv files, and secrets.
- You need nested settings models and reliable parsing behavior.
- You need to choose between one nested application settings object and independently owned settings objects.
- You need a deliberate construction, caching, or reload lifecycle.
- You need to customize settings sources or source order safely.
Procedure
1. Baseline Model
Create a single settings model for the service boundary:
from pydantic import BaseModel, Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class DatabaseSettings(BaseModel):
host: str = "localhost"
port: int = 5432
user: str
password: str
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_prefix="APP_",
env_file=".env",
env_file_encoding="utf-8",
extra="ignore",
frozen=True,
)
debug: bool = False
log_level: str = "info"
database: DatabaseSettings
api_key: str = Field(validation_alias="MY_API_KEY")
Quality gate:
- Required fields fail fast when missing.
- Defaults are intentional and safe.
2. Pick Env Naming Rules
- Choose one prefix and apply it consistently.
- Use aliases only for compatibility or external contracts.
- Document whether env names are case-sensitive.
Quality gate:
- Team can derive env variable names without guessing.
- Legacy names are supported only where needed.
3. Decide Nested Parsing
For nested models via env vars, configure delimiters intentionally:
model_config = SettingsConfigDict(
env_prefix="APP_",
env_nested_delimiter="__",
env_nested_max_split=1,
)
Typical vars:
APP_DATABASE={"host": "db", "port": 5432, "user": "svc", "password": "pw"}APP_DATABASE__HOST=db.internal
Quality gate:
- Nested overrides behave as expected.
- Delimiter choice does not collide with field names.
4. Confirm Source Priority
Default priority (higher first):
- CLI args (if enabled)
- init kwargs
- env vars
- dotenv
- secrets dir
- defaults
Only customize when required:
from pydantic_settings import PydanticBaseSettingsSource
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return (init_settings, env_settings, dotenv_settings, file_secret_settings)
Quality gate:
- Priority order is explicit in code.
- Tests verify conflict resolution.
5. Add Secrets Strategy
- In local development, dotenv is acceptable for non-production values.
- In deployed environments, prefer env vars or secret managers.
- For file-mounted secrets, use
secrets_dir.
Example:
model_config = SettingsConfigDict(
env_prefix="APP_",
env_file=".env",
secrets_dir="/run/secrets",
)
Quality gate:
- No secret literals in repository code.
- Missing secrets behavior is understood per environment.
6. Choose Nested Or Independent Settings Boundaries
Prefer one root BaseSettings object with nested BaseModel sections when the configuration belongs to one application lifecycle:
from pydantic import BaseModel, Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class DatabaseSettings(BaseModel):
host: str = "localhost"
port: int = 5432
class ObservabilitySettings(BaseModel):
log_level: str = "INFO"
json_logs: bool = True
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_prefix="APP_",
env_nested_delimiter="__",
frozen=True,
)
database: DatabaseSettings = Field(default_factory=DatabaseSettings)
observability: ObservabilitySettings = Field(
default_factory=ObservabilitySettings
)
This produces names such as APP_DATABASE__HOST and gives the application one validated, atomic configuration snapshot. Nested sections should normally inherit from BaseModel, not BaseSettings; otherwise each nested settings model can collect sources independently and produce surprising results.
Use independent BaseSettings classes when the objects have genuinely independent ownership:
- Different packages or deployable components own the schemas.
- Each object needs its own env prefix or source policy.
- A component is optional or loaded lazily.
- Components need different reload lifecycles.
- The same component must run outside the application.
Construct independent objects explicitly at the composition root and inject each dependency. Do not nest one BaseSettings class inside another merely to reuse its fields. Extract a shared BaseModel schema when models need common structure.
Quality gate:
- Nested sections share one source policy and lifecycle.
- Independent settings have distinct owners, prefixes, or lifecycles.
- The application does not repeatedly scan the same sources through accidental nested
BaseSettingsconstruction.
7. Own The Settings Lifecycle
For most applications, construct settings once at the composition root and pass the validated object to services:
def main() -> None:
settings = Settings()
application = Application(settings=settings)
application.run()
This makes ownership, startup failure, and test overrides explicit. Treat the object as a snapshot: environment variables and files changing later do not update an existing instance. Prefer frozen=True for shared settings so consumers cannot silently mutate process-wide configuration.
Use functools.cache only when process-lifetime singleton access is intentional and explicit injection is awkward, such as a framework dependency provider:
from functools import cache
@cache
def get_settings() -> Settings:
return Settings()
Keep the cached factory argument-free. Passing override kwargs creates one cached instance per argument combination, retains those values for the process lifetime, and obscures which configuration is active. In tests, instantiate Settings(...) directly or override the dependency; when a test must exercise the cached getter, isolate environment changes with get_settings.cache_clear() before and after the assertion.
cache is process-local. Every worker process gets its own instance, and concurrent first calls can construct more than one instance before the cache is populated. Settings construction must therefore be side-effect free; create engines, clients, and sessions in their own lifecycle-managed providers.
Quality gate:
- Settings are created once per intended application or worker lifecycle.
- Cached factories are argument-free and side-effect free.
- Tests do not leak cached settings or environment changes.
- Resource construction is separate from configuration parsing.
8. Reload Deliberately
Static service configuration should normally require a process restart. If runtime reload is a real requirement, construct a fresh settings instance and atomically replace the owned reference. Do not call __init__() on a shared instance: readers can observe mutation in progress, and resources derived from old values may remain alive.
Settings sources are synchronous. In an async application, construction or reload that reads dotenv, secrets, JSON, TOML, or YAML files should run in a worker thread:
import asyncio
async def load_settings() -> Settings:
return await asyncio.to_thread(Settings)
Clearing get_settings is sufficient for controlled tests or single-threaded administration, but it is not an atomic live-reload protocol. Concurrent applications should own the current reference behind an application-specific lock or lifecycle manager, swap in a fully validated replacement, and then rebuild dependent resources.
Quality gate:
- Reload creates and validates a replacement before publication.
- Readers cannot observe a partially mutated object.
- Dependent resources are recreated after the settings reference changes.
- File-backed source reads do not block an async event loop.
9. Add Focused Lifecycle Tests
Do not add tests that re-validate baseline pydantic-settings functionality unless custom behavior is layered on top. Test the application-owned behavior instead:
- Repeated cached getter calls return the same instance.
- Cache clearing after an environment change returns a newly validated instance.
- Explicitly injected settings bypass global cached state.
- Reload swaps the settings snapshot and rebuilds dependent resources, when reload is supported.
Suggested invocation:
uv run pytest -q
Completion Checks
- Settings ownership matches the application or component lifecycle.
- Source precedence is documented and tested.
- Env naming conventions and aliases are explicit and stable.
- Nested parsing behavior is tested when custom parsing behavior is added.
- Secrets and dotenv usage are environment-appropriate and do not leak sensitive defaults.
- Validation errors are actionable and fail fast for required values.
- Cached factories are argument-free, process-local, and cleared deliberately in tests.
- Nested models share one source policy; independent settings have an explicit ownership reason.
- Runtime reload, if supported, replaces a validated snapshot and rebuilds dependent resources.
Output Contract
When this skill is applied, return:
- Which references were consulted.
- The chosen source-precedence model and why.
- The exact parsing and alias decisions made.
- Any deferred choices and their risk.
- The validation commands or tests run to confirm behavior.
Use these upstream docs when implementing or reviewing pydantic-settings behavior.
Source Docs
Primary
Core Concepts
Priority And Sources
Environment And Parsing
- Environment variable names and prefix behavior
- Case sensitivity behavior
- Parsing environment variable values
- Nested model default partial updates