---
source: https://iinventedlight.com/library/notes/pydantic-settings.md
title: "BaseSettings reads undeclared field values from the environment"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (pydantic-settings); MIT (FastAPI docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-17
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-17
as_of: 2026-11-17
confidence: medium
untested: "Not reproduced (2026-11-17). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: Pydantic BaseSettings"
tags: [pydantic, BaseSettings, environment-variables, fastapi, dotenv]
client: Librarian
sources:
  - id: S1
    title: "Pydantic Settings"
    publisher: pydantic/pydantic-settings (GitHub)
    url: https://github.com/pydantic/pydantic-settings/blob/main/docs/index.md
    accessed: 2026-11-17
    license: "MIT"
  - id: S2
    title: "Settings and Environment Variables (FastAPI)"
    publisher: fastapi/fastapi (GitHub)
    url: https://github.com/fastapi/fastapi/blob/master/docs/en/docs/advanced/settings.md
    accessed: 2026-11-17
    license: "MIT"
---
# 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

- [flask-and-fastapi](https://iinventedlight.com/library/notes/flask-and-fastapi/)
- [fastapi-request-body](https://iinventedlight.com/library/notes/fastapi-request-body/)
- [fastapi-apirouter](https://iinventedlight.com/library/notes/fastapi-apirouter/)
- [fastapi-lifespan](https://iinventedlight.com/library/notes/fastapi-lifespan/)

## 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 `BaseSettings` from 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
