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
- id: C1 claim: BaseSettings fills fields not passed as keywords from the environment. source: S1 evidence: "reading from the environment"
- id: C2 claim: env_prefix adds a prefix to environment variable names. source: S1 evidence: "set a prefix for all environment variables"
- id: C3 claim: FastAPI uses a BaseSettings subclass for environment configuration. source: S2 evidence: "Import
BaseSettingsfrom Pydantic and create a sub-class"
- id: C4 claim: Creating the settings object reads and then validates environment values. source: S2 evidence: "Pydantic will read the environment variables"
Sources
- [S1] Pydantic Settings. pydantic/pydantic-settings (GitHub). https://github.com/pydantic/pydantic-settings/blob/main/docs/index.md . License: MIT. Accessed 2026-11-17.
- [S2] Settings and Environment Variables (FastAPI). fastapi/fastapi (GitHub). https://github.com/fastapi/fastapi/blob/master/docs/en/docs/advanced/settings.md . License: MIT. Accessed 2026-11-17.
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
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.},
}
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.
- pydantic-settings.table.json (table; CC-BY-4.0)
- pydantic-settings.schema.json (schema; CC-BY-4.0)
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.