← All notes

BaseSettings reads undeclared field values from the environment

Answer: A model that inherits from BaseSettings still accepts keyword arguments, but for any field you do not pass, the initializer tries to read a matching environment variable. Install the pydantic-settings package and import BaseSettings from it. Default values are used when the variable is unset.

Names, prefixes, and dotenv

By default the environment variable name is the field name. Set env_prefix in SettingsConfigDict (or pass _env_prefix) to prefix every name. A field can also use an alias. Besides env settings, env_prefix also applies to dotenv files, secrets, and other sources.

Unlike plain BaseModel, BaseSettings validates default values unless you set validate_default=False.

Use in FastAPI

An application can need external settings or configurations, for example secret keys or database credentials. Import BaseSettings from Pydantic and create a sub-class. When you create an instance of that Settings class, Pydantic will read the environment variables, then convert and validate the data. You can also read settings from a .env file.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: pydantic/pydantic-settings (GitHub), Pydantic Settings, MIT (https://raw.githubusercontent.com/pydantic/pydantic-settings/main/LICENSE). Copyright (c) 2022 Samuel Colvin and other contributors; fastapi/fastapi (GitHub), Settings and Environment Variables (FastAPI), MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright (c) 2018 Sebastián Ramírez. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: pydantic/pydantic-settings (GitHub), Pydantic Settings, MIT (https://raw.githubusercontent.com/pydantic/pydantic-settings/main/LICENSE). Copyright (c) 2022 Samuel Colvin and other contributors; fastapi/fastapi (GitHub), Settings and Environment Variables (FastAPI), MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright (c) 2018 Sebastián Ramírez.

Cite this

Permanent ID: iil:note:e09n4wwa

iinventedlight. 2026. "BaseSettings reads undeclared field values from the environment." iinventedlight Library, iil:note:e09n4wwa, version of 2026-11-17. https://iinventedlight.com/library/notes/pydantic-settings/. License: CC-BY-4.0.

@misc{iil_note_e09n4wwa,
  author = {{iinventedlight}},
  title = {{BaseSettings reads undeclared field values from the environment}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-17},
  url = {https://iinventedlight.com/library/notes/pydantic-settings/},
  note = {Permanent ID iil:note:e09n4wwa. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/pydantic-settings.md.