Skip to content
tezvyn:

Pydantic BaseSettings: Typed, Layered Configuration

Source: pydantic.devMediumHow cards are made

Pydantic BaseSettings: Typed, Layered Configuration

Pydantic's BaseSettings treats configuration as typed data, not just strings. It automatically loads and validates settings from environment variables, .env files, and secrets stores into a Python object.

Why it exists

Applications need configuration like API keys and database URLs. Managing these as simple strings across different environments (dev, staging, prod) is error-prone and lacks validation. Hardcoding is inflexible, and basic environment variable parsing doesn't offer structure or type safety.

The mental model

Think of BaseSettings as a contract for your application's configuration. You define a Python class with typed attributes (e.g., api_key: str, port: int). Pydantic then acts as a broker, automatically finding, parsing, and validating values for these attributes from a prioritized list of sources.

How it works

You create a class that inherits from pydantic_settings.BaseSettings. For each field in your class, like db_host, Pydantic attempts to find a value. By default, it first checks for an environment variable named DB_HOST (case-insensitively). If not found, it might check a loaded .env file, and finally fall back to a default value if one is defined in the class. If a value is found, it's parsed and validated against the field's type, converting the string "8080" to the integer 8080 automatically. You can also define nested models for structured settings.

When to use it

Use it for any Python application requiring configuration, especially web services built with FastAPI or Flask. It is invaluable when deploying to containerized environments like Docker or Kubernetes, where environment variables are standard. It also simplifies local development by seamlessly integrating with .env files.

When not to use it

For extremely simple scripts with only one or two configuration values, it might be overkill; Python's os.getenv() could suffice. It is not a replacement for dynamic configuration systems like etcd or Consul if you need to change settings in a running application without a restart, as BaseSettings loads configuration at initialization time.

One canonical example

Imagine a Settings class inheriting from BaseSettings with three fields: app_name: str = "Default Name", database_url: str, and max_connections: int = 10. If you create an instance config = Settings(), Pydantic will look for sources. If you have a .env file containing DATABASE_URL="postgres://...", config.database_url will be populated and validated. If you then set an environment variable export MAX_CONNECTIONS=20, config.max_connections will become 20, overriding the default 10 because environment variables have higher priority than defaults.

Interview question

What is a primary advantage of using Pydantic BaseSettings for application configuration?

  • a.It automatically converts all configuration values into universal string formats.
  • b.It provides a structured, type-safe way to load and validate settings from multiple prioritized sources.Correct
  • c.It simplifies configuration by requiring all settings to be hardcoded within the application.
  • d.It allows configuration values to be updated dynamically in a running application.
Why?

BaseSettings excels at providing a typed contract for configuration, automatically loading and validating values from sources like environment variables and .env files with a defined priority. Option D is incorrect because BaseSettings loads configuration at initialization time and is not designed for dynamic runtime changes.

Just read this? Test yourself on what you have been reading.

Read the original → pydantic.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.

See open roles