Skip to main content

Support for Pydantic settings configuration file loading

Project description

Pydantic Config

Pydantic Config extends Pydantic’s BaseSettings to support loading configuration from files like JSON, INI, TOML, and YAML — in addition to environment variables.

PyPI Conda License


Table of Contents


Why Pydantic Config?

✅ Load settings from .json, .ini, .toml, .yaml, and .env files.
✅ Support for multiple files with override/merge strategies.
✅ Compatible with Pydantic v2 BaseSettings.
✅ Lightweight and simple integration.


Installation

Install with pip:

pip install pydantic-config

Or with conda (via the conda-forge channel):

conda install pydantic-config -c conda-forge

Optional Dependencies

To enable additional file formats, you can install optional dependencies:

Format Install Command
YAML pip install pydantic-config[yaml]
TOML (for Python < 3.11) pip install pydantic-config[toml]

To install all optional dependencies:

pip install pydantic-config[all]

Quick Start

Load settings from a config file:

config.toml

app_name = "Python Application"
description = "Test application description"

settings.py

from pydantic_config import SettingsModel, SettingsConfig

class Settings(SettingsModel):
    app_id: str = 1
    app_name: str = None
    description: str = None
    log_level: str = 'INFO'
    
    model_config = SettingsConfig(
        config_file='config.toml',
    )

settings = Settings()
print(settings)
# app_id='1' app_name='Python Application' description='Test application description' log_level='INFO'

Loading Multiple Config Files

Multiple config files can be loaded by passing a list of file names. Files will be loaded in the order they are listed. Meaning later files in the list will take priority over earlier files.

config.toml

app_name = "Python Application"
description = "Test application description"

config.json

{
  "description": "Description from JSON file",
  "log_level": "WARNING"
}

settings.py

from pydantic_config import SettingsModel, SettingsConfig

class Settings(SettingsModel):
    app_id: str = 1
    app_name: str = 'App Name'
    description: str = None
    log_level: str = 'INFO'
    
    model_config = SettingsConfig(
        config_file=['config.toml', 'config.json']  # The config.json file will take priority over config.toml
    )

settings = Settings()
print(settings)
# app_id='1' app_name='Python Application' description='Description from JSON file' log_level='WARNING'

Supported File Formats

Currently, the following file formats are supported:

  • .yaml Requires pyyaml package
  • .toml Requires tomli package for python<3.11
  • .json
  • .ini

Using .env files

Since Pydantic natively supports dotenv files, you can combine a .env file with config files easily.
Values from the .env file take precedence over config files.

from pydantic_config import SettingsModel, SettingsConfig

class Settings(SettingsModel):
  app_name: str = None
  description: str = None
    
    model_config = SettingsConfig(
        env_file='.env',
        config_file='config.toml',
    )

Requiring Config Files to Exist

By default, missing config files are ignored (no error is raised). This may be useful if you want to specify config files that may not always exist. For example, you might have different config files for per environment: config-prod.toml and config-dev.toml.

To enforce that config files must exist, set config_file_required=True. This will cause an error to be raised if the specified config file(s) does not exist.

model_config = SettingsConfig(
    config_file='config.toml',
    config_file_required=True,
)

⚠️ When config_file_required=True, config_file cannot be None or an empty list [].


Merging

By default, when merging multiple config files:

  • dict values are merged (keys combined).
  • list values are merged (items combined).

To disable merging and prefer overwriting entirely, set config_merge=False.

Example:

config.toml

[foo]
item1 = "value1"

config2.toml

[foo]
item2 = "value2"

settings.py

from pydantic_config import SettingsModel, SettingsConfig

class Settings(SettingsModel):
    foo: dict = {}

    model_config = SettingsConfig(
        config_file=['config.toml', 'config2.toml'],
        config_merge=True,
    )

settings = Settings()
print(settings)
# foo={'item1': 'value1', 'item2': 'value2'}

If config_merge=False, the second file would overwrite the first:

foo={'item2': 'value2'}

Handling Duplicates in Merged Lists

By default, merged lists include all items, even duplicates.

To ensure list items are unique during merge, set config_merge_unique=True.

model_config = SettingsConfig(
    config_file=['config1.toml', 'config2.toml'],
    config_merge=True,
    config_merge_unique=True,
)

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_config-0.4.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_config-0.4.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_config-0.4.0.tar.gz.

File metadata

  • Download URL: pydantic_config-0.4.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for pydantic_config-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ea839a9dbdb56b883529093a5a9b186c8a6393dbb5620d4a629b3948897f2a03
MD5 be9c761b90e25b273f5a3d243944a659
BLAKE2b-256 e21abc50b223c6525676f6a65675e007ac773625a5b42d2605bdfff41db29b4e

See more details on using hashes here.

File details

Details for the file pydantic_config-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_config-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f57dfbab35f9ae311f0c0711d2dc737cf3f5d3c509fa8ab4b5209eaeff4952e
MD5 9e55cead1d403f34284fed5db8cb138c
BLAKE2b-256 36b0dbec38c44503e0e4512e847c2d0ea29db60155c41a05ba22496e3a07469b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page