Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Filter by Categories
About Article
Analyze Data
Archive
Best Practices
Better Outputs
Blog
Code Optimization
Code Quality
Command Line
Daily tips
Dashboard
Data Analysis & Manipulation
Data Engineer
Data Visualization
DataFrame
Delta Lake
DevOps
DuckDB
Environment Management
Feature Engineer
Git
Jupyter Notebook
LLM
LLM
Machine Learning
Machine Learning
Machine Learning & AI
Manage Data
MLOps
Natural Language Processing
NumPy
Pandas
Polars
PySpark
Python Tips
Python Utilities
Python Utilities
Scrape Data
SQL
Testing
Time Series
Tools
Visualization
Visualization & Reporting
Workflow & Automation
Workflow Automation

Pydantic-settings: Type-Safe Config Management

Table of Contents

Pydantic-settings: Type-Safe Config Management

Motivation

Type validation and conversion are essential aspects of configuration management in Python applications, ensuring that settings are not only loaded but also conform to expected data types and formats.

Without proper type validation, developers need to manually convert and validate configuration values, which can lead to runtime errors and inconsistent behavior.

import os

DATABASE_URL = os.getenv('DATABASE_URL', 'postgresql://localhost:5432/db')
MAX_CONNECTIONS = os.getenv('MAX_CONNECTIONS', '10')  # Need manual conversion to string
DEBUG = bool(os.getenv('DEBUG', 'False'))  # Need manual conversion to boolean

print(f"Database URL: {DATABASE_URL}")
print(f"Max Connections: {MAX_CONNECTIONS}")
print(f"Debug Mode: {DEBUG}")

Output:

Database URL: 'postgresql://localhost:5432/db'
Max Connections: 10
Debug Mode: False

Introduction to pydantic-settings

Pydantic-settings is a powerful settings management library that builds on Pydantic’s validation capabilities to provide secure, type-safe configuration management. Install it using:

pip install "pydantic-settings"

Settings Management

Pydantic-settings provides type validation, environment variable support, and secure configuration management through:

from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import PostgresDsn
from typing import Optional

class DatabaseSettings(BaseSettings):
    model_config = SettingsConfigDict(env_prefix='DB_')
    
    url: PostgresDsn = PostgresDsn('postgresql://localhost:5432/db')
    max_connections: int = 10
    debug: bool = False

Set environment variables on your terminal:

export DB_URL=postgresql://localhost:5432/production
export DB_MAX_CONNECTIONS=20
export DB_DEBUG=True

Access settings:

# Load settings from environment variables
settings = DatabaseSettings()

print(f"Database URL: {settings.url}")
print(f"Max Connections: {settings.max_connections}")
print(f"Debug Mode: {settings.debug}")

Output:

Database URL: postgresql://localhost:5432/production
Max Connections: 20
Debug Mode: True

Pydantic-settings vs Python-dotenv

Python-dotenv, covered in this article, provides basic environment variable loading, while pydantic-settings offers additional features:

Python-dotenv approach:

from dotenv import load_dotenv
import os

load_dotenv()
database_url = os.getenv('DATABASE_URL')  # No type validation
debug = bool(os.getenv('DEBUG'))  # Returns string, manual conversion needed

Pydantic-settings advantages:

  • Automatic type validation and conversion
  • URL format validation with PostgresDsn
  • Environment variable prefix support (DB_)
  • Default values with proper types

Conclusion

Pydantic-settings provides a type-safe, validated way to manage application configuration. It eliminates common configuration-related bugs and reduces boilerplate code while providing excellent developer experience through type hints and documentation.

Link to pydantic-settings.

Leave a Comment

Your email address will not be published. Required fields are marked *

0
    0
    Your Cart
    Your cart is empty
    Scroll to Top

    Work with Khuyen Tran

    Work with Khuyen Tran