[Python] How to use yaml_file parameter for pydantic settings

Discussão em 'Python' iniciado por Stack, Julho 30, 2025.

  1. Stack

    Stack Membro Participativo

    Here is my example code:

    from pathlib import Path
    from pydantic_settings import BaseSettings, SettingsConfigDict

    class Settings(BaseSettings):
    model_config = SettingsConfigDict(
    yaml_file=Path('c.yaml'),
    yaml_config_section='blah',
    )

    port: int


    s = Settings()


    and my c.yaml stored in the same directory:

    blah:
    port: 123


    When I'm running python file I'm getting message:

    pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
    port
    Field required [type=missing, input_value={}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.11/v/missing


    what means that Pydantic even doesn't trying to read YAML file. I found multiple answers how read yaml and most of it is about how read yaml, convert it to dict and pass the dict as constructor arguments. But I could not find any information why this yaml_file and yaml_config_section exists and what it does?

    Is here a simple way to read yaml using these arguments?

    Continue reading...

Compartilhe esta Página