원클릭으로
pydantic-settings
// Add or modify application configuration settings. Use when: adding new environment variables, settings fields, understanding settings conventions, working with secrets, or configuring optional vs required settings.
// Add or modify application configuration settings. Use when: adding new environment variables, settings fields, understanding settings conventions, working with secrets, or configuring optional vs required settings.
Configure or use the aiocache caching layer. Use when: adding cache reads/writes, configuring cache backends, working with TTLs, enabling/disabling caching, or understanding the NoOpCache fallback pattern.
Create or modify Celery tasks and periodic task configuration. Use when: adding new background tasks, setting up periodic/scheduled tasks, configuring Celery workers, or understanding the Celery app setup.
Work with the Docker Compose development environment. Use when: starting or stopping services, inspecting logs, opening a shell in a container, resetting the database, or understanding the service topology.
Create or modify FastAPI routes. Use when: adding new API endpoints, creating Pydantic request/response models, registering routers, designing REST APIs, or following route conventions for this project.
Create or modify Jinja2 templates. Use when: adding new HTML templates, configuring the Jinja2 environment, adding custom filters or globals, rendering templates outside FastAPI, or working with template inheritance.
Complete reference for all make targets in the project. Use when: looking up the right make command for any task — setup, testing, linting, formatting, database, packaging, or cleanup.
| name | pydantic-settings |
| description | Add or modify application configuration settings. Use when: adding new environment variables, settings fields, understanding settings conventions, working with secrets, or configuring optional vs required settings. |
context7: If the
context7tools are available, resolve and load the fullpydantic-settingsdocumentation before adding or modifying any settings:context7_resolve-library-id: "pydantic-settings" context7_query-docs: <resolved-id> "your query"
All application configuration is managed through the pydantic-settings library. Settings are defined in a single class and loaded from the environment and .env file.
Settings class lives at {{cookiecutter.__package_slug}}/conf/settings.py. Always update this existing class — never create a new settings class.SecretStr or SecretBytes: Any value that is sensitive (passwords, tokens, API keys) must be wrapped in one of these types.None: Never use empty strings as a sentinel for "not set".Field(): Include a description= for every field so operators know what it does.# {{cookiecutter.__package_slug}}/conf/settings.py
from pydantic import Field, SecretStr
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
extra="ignore",
)
# Regular setting with a default
project_name: str = Field(default="{{ cookiecutter.__package_slug }}", description="Human-readable project name")
# Optional sensitive setting — defaults to None, not empty string
api_key: SecretStr | None = Field(
default=None,
description="API key for external service. Leave unset to disable.",
)
# Optional non-sensitive setting
max_connections: int = Field(
default=10,
description="Maximum number of connections",
)
SecretStr wraps the value to prevent accidental logging. Access the actual value explicitly when needed:
settings.api_key.get_secret_value() if settings.api_key else None
| Pattern | Behavior |
|---|---|
field: str = Field(...) | Required — raises ValidationError if not set |
field: str = Field(default=x) | Optional with default |
field: str | None = Field(default=None) | Optional, absent means None |
Never use "" as a default for "not configured":
# Bad
smtp_host: str = Field(default="")
# Good
smtp_host: str | None = Field(default=None, description="SMTP server hostname. None disables email.")
pydantic-settings maps field names to environment variable names automatically using the field name in uppercase:
project_name → PROJECT_NAME
database_url → DATABASE_URL
{{cookiecutter.__package_slug}}/conf/settings.py.Settings class with Field(description=...).SecretStr if the value is sensitive.None (not "") if the setting is optional..env.example with the new variable name and a placeholder value or explanation..env in the project root (gitignored)..env.example is the template for new developers — keep it updated with every new setting.