Pydantic Series: Managing Application Settings

In the first part of this series, we introduced Pydantic and its core functionalities, focusing on data validation and parsing. In this second part, we’ll explore how Pydantic can be used to manage application settings, ensuring consistency and correctness across configurations.

Why Use Pydantic for Application Settings?

Managing application settings manually can be error-prone, especially when dealing with multiple environments (e.g., development, staging, production). Pydantic simplifies this process by providing structured validation, type conversion, and environment variable support.

Defining Application Settings with Pydantic

Pydantic’s BaseSettings class is designed for handling application configurations. It allows settings to be loaded from environment variables, configuration files, or defaults.

from pydantic import BaseSettings, Field

class AppSettings(BaseSettings):
    app_name: str = Field(..., env="APP_NAME")
    debug: bool = Field(False, env="DEBUG")
    database_url: str = Field(..., env="DATABASE_URL")

    class Config:
        env_file = ".env"

In this example:

  • APP_NAME, DEBUG, and DATABASE_URL are fetched from environment variables.
  • Defaults can be provided (e.g., debug defaults to False).
  • The Config class specifies an optional .env file for loading environment variables.

Using Pydantic Settings in an Application

To load and use settings in your application:

settings = AppSettings()
print(settings.app_name)
print(settings.debug)
print(settings.database_url)

If environment variables or an .env file are available, values will be loaded automatically. Otherwise, missing values will raise a validation error.

Overriding Settings for Different Environments

You can easily switch between environments by creating separate .env files:

.env (Development)

APP_NAME=MyApp-Dev
DEBUG=True
DATABASE_URL=sqlite:///dev.db

.env (Production)

APP_NAME=MyApp
DEBUG=False
DATABASE_URL=postgresql://user:password@db.example.com:5432/app_db

By changing the .env file or environment variables, you can seamlessly adapt your application to different environments.

Custom Validation for Configuration Fields

You can add custom validation for specific fields using @field_validator to enforce business logic.

from pydantic import field_validator

class AppSettings(BaseSettings):
    app_name: str
    database_url: str

    @field_validator("database_url")
    @classmethod
    def validate_db_url(cls, value):
        if not value.startswith("postgresql://") and not value.startswith("sqlite://"):
            raise ValueError("DATABASE_URL must be a PostgreSQL or SQLite URL")
        return value

This ensures that only PostgreSQL or SQLite URLs are accepted, preventing misconfigurations.

Conclusion

Pydantic’s BaseSettings is a powerful tool for managing application configurations in a structured and error-free manner. By leveraging Pydantic’s validation and environment variable support, developers can create more robust applications with flexible settings management.

In the next part of this series, we’ll explore integrating Pydantic with FastAPI to streamline request validation and response modeling. Stay tuned for Part 3!

Leave a comment