Skip to content

AzureKeyVaultSettingsSource ignoring SettingsSource Priority #412

@DavidRR-F

Description

@DavidRR-F

I am trying to use the AzureKeyVaultSettingSource in my BaseSettings class configuration as specified in the docs.

I am running into an issue were, if I have an environment variable referenced in the class whose key is defined with an underscore FOO_BAR, a SettingsError as thrown by AzureKeyVaultSetttingSource rather than the key value being initialized by the env_settings which is configured to have a higher priority.

I know azure keyvault does not support underscores in secret names so this is most likely the source of the error, but I figured that source priority would initialize the environment variables first so the azure source would not try and grab that value.

Example:

python==3.10
pydantic-settings==2.5.2
pydantic==2.9.2
azure-identity==1.17.1

from pydantic_settings import (
    AzureKeyVaultSettingsSource,
    BaseSettings,
    PydanticBaseSettingsSource
)
from pydantic import Field, computed_field
from azure.identity import DefaultAzureCredential
from typing import Tuple, Type
import os

class Settings(BaseSettings):
    FOO: str # azure key vault value
    FOO_BAR: str # environment variable

    @classmethod
    def settings_customise_sources(
        cls,
        settings_cls: Type[BaseSettings],
        init_settings: PydanticBaseSettingsSource,
        env_settings: PydanticBaseSettingsSource,
        dotenv_settings: PydanticBaseSettingsSource,
        file_secret_settings: PydanticBaseSettingsSource,
    ) -> Tuple[PydanticBaseSettingsSource, ...]:
        az_key_vault_settings = AzureKeyVaultSettingsSource(
            settings_cls,
            os.environ.get("AZURE_KEY_VAULT_URL"),
            DefaultAzureCredential(),
        )
        return (
            init_settings,
            env_settings,
            dotenv_settings,
            file_secret_settings,
            az_key_vault_settings,
        )

settings = Settings()

print(setting.FOO, setting.FOO_BAR)

Error

Settings Error: pydantic_settings.sources.SettingsError: error getting value for field "FOO_BAR" from source "AzureKeyVaultSettingsSource"

Have I misconfigured/misunderstood something?

Does each setting iterate through each key independantly regardless of priority?

Thanks in advance! 😊

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions