Skip to content

Conversation

@Sube-py
Copy link

@Sube-py Sube-py commented Dec 2, 2025

When using environment variables to override multi-level configurations, an error will occur if the configuration is of a complex type.

pydantic version: 2.11.9
pydantic-core version: 2.33.2
pydantic-core build: profile=release pgo=false
python version: 3.12.7 (main, Oct 16 2024, 07:12:08) [Clang 18.1.8 ]
platform: macOS-26.1-arm64-arm-64bit
related packages: pydantic-settings-2.8.1 typing_extensions-4.15.0
commit: unknown

Reproduce code:

from pydantic import BaseModel, Field
from pydantic_settings import BaseSettings, SettingsConfigDict


class EmbeddingModel(BaseModel):
    model: str = 'text-embedding-3-small'
    keys: list[str] = Field(default_factory=list)


class LLM(BaseModel):
    embeddings: dict[str, EmbeddingModel] = Field(default_factory=dict)


class LLMSettings(BaseSettings):
    llm: LLM = Field(default_factory=lambda: LLM())

    model_config = SettingsConfigDict(env_prefix='my_prefix_', env_nested_delimiter='__')


# export my_prefix_llm__embeddings__openai__keys='["sk-..."]'
# export my_prefix_llm__embeddings__qwen__keys='["sk-..."]'
print(LLMSettings().model_dump()) # raise error

@hramezani
Copy link
Member

Thanks @Sube-py for the PR.
Please add the example in the PR description as a test

@Sube-py
Copy link
Author

Sube-py commented Dec 2, 2025

@hramezani plz check.

Comment on lines +7 to +8
# export my_prefix_llm__embeddings__openai__keys='["sk-..."]'
# export my_prefix_llm__embeddings__qwen__keys='["sk-..."]'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need this comment

Suggested change
# export my_prefix_llm__embeddings__openai__keys='["sk-..."]'
# export my_prefix_llm__embeddings__qwen__keys='["sk-..."]'

from pydantic_settings import BaseSettings, SettingsConfigDict


def test_env_source_when_load_multi_nested_config(env):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move the test to tests/test_settings.py. you can put it at the end of file. env related tests are there

@hramezani
Copy link
Member

@hramezani plz check.

Thanks @Sube-py please check the comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants