Skip to main content

What?

A simple tool for loading YAML and JSON configuration/settings using pydantic2.

There is also a version for pydantic1, see release/v1. Major versions of this package will match the major version of the respective pydantic release.

Why?

This project can be helpful for projects that have large configuration files, nested configuration files, or for those of us who don’t like writing large .env files. It is also worth noting that due to the backwards compatability between YAML and JSON that this will also parse JSON configuration.

This can also be helpful when writing out application settings in kubernetes /helm, where most configuration is written as YAML. In such a case we may want to validate/store our settings as YAML as writing JSON and JSON strings can be compersome due to syntax error in larger documents.

Installation

Install using pip:

pip install yaml-settings-pydantic

Examples

Additional information

First, it is worth reading the pydantic_settings docs about additional sources: https://docs.pydantic.dev/latest/usage/pydantic_settings/

Additionally see the example in ./tests/examples/__init__.py. It is gaurenteed to work as its contents are tested. It contains information on how to write nested configurations.

Tools

There are three classes worth knowing about:

  • YamlSettingsConfigDict – pydantic_settings.SetttingsConfigDict extended to include the fields used by CreateYamlSettings.

  • CreateYamlSettings – The pydantic PydanticBaseSettingsSource that will analyze your class for the following class variables:

    1. Files to be used – under __env_yaml_files__ or model_config.yaml_files.

    2. The reload settings – under __env_yaml_reload__ or model_config.yaml_reload.

    CreateYamlSettings does not have to be used at all, but can be helpful if you don’t want to use BaseYamlSettings for any reason.

  • BaseYamlSettings – Use this directly as done in the example below. This is ‘the easy way’.

Minimal Examples

The shortest possible example is as follows:

from yaml_settings_pydantic import BaseYamlSettings

class MySettings(BaseYamlSettings):
   __env_yaml_files__ = "settings.yaml"

   setttingOne: str
   settingTwo: str
   ...

...

Note that the above example can also be written like

from yaml_settings_pydantic import BaseYamlSettings, YamlSettingsConfigDict

class MySettings(BaseYamlSettings):
   model_config = YamlSettingsConfigDict(yaml_files="settings.yaml")

   setttingOne: str
   settingTwo: str
   ...

...

which is more like pydantic v2. The ‘dunder’ specifications will take priority over their equivalent model_config specifications. These map as follows:

+-----------------------+------------------+
| dunder                | model_config     |
+-----------------------+------------------+
| __env_yaml_files__    | yaml_files       |
| __env_yaml_reload__   | yaml_reload      |
+-----------------------+------------------+

Release files for yaml-settings-pydantic 2.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yaml-settings-pydantic 2.3.2
File Size Uploaded
yaml_settings_pydantic-2.3.2.tar.gz 10.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for yaml-settings-pydantic 2.3.2
File Interpreter ABI Platform
yaml_settings_pydantic-2.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 19.8 kB

Release files / yaml_settings_pydantic-2.3.2.tar.gz

Download URL yaml_settings_pydantic-2.3.2.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4dd8df300be4e5abc8a386843a197302c820d94e33e2e44be1fb22e2a5345df6
BLAKE2b-256 checksum
How to use checksums
fdd3958d75466277d73fffd7e685653dba4221dc637f4e8a23e2c18206b3c832
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / yaml_settings_pydantic-2.3.2-py3-none-any.whl

Download URL yaml_settings_pydantic-2.3.2-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd0e214d32ae7a8ca5cb9fe23d4be087bc63664a2fa076cea39b70e227b026a6
BLAKE2b-256 checksum
How to use checksums
4793b8ad579aecf53d5805f7c4255a29ede935da4f78343176dc134506d7176a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

2.3.2 This release

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

0.1.0

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page