Peter Sampson
Peter Sampson

Reputation: 11

Pydantic using a list from .env file with BaseSettings

After updating pydantic from 1.9 to 2.6, my lists in my .env file no longer work. I am wondering if there is a new syntax or other setting I need to enable to get the same behavior.

This is what I had before

`my_list: list[str]`

With the the .env file as MY_LIST=["a","b"]

Which would work as expected in version 1.9, now I get the error

  Input should be a valid list [type=list_type, input_value='["a","b"]', input_type=str]
    For further information visit https://errors.pydantic.dev/2.6/v/list_type

I haven't been able to find any permutation for it not to interpret the input as a str. Other non-list variables work fine

In summary, the following code works in 1.9

from pydantic import BaseSettings, Field
import os
import logging
import sys

class Settings(BaseSettings):
    my_list: list[str] = Field(..., env="MY_LIST")

    class Config:
        env_file = f'{os.environ.get("ENV")}.env'
        env_file_encoding = 'utf-8'
        env_prefix = f'{os.environ.get("ENV")}_'
        extra='ignore'

try:
    settings = Settings()
    print(settings)
except Exception as e:
    logging.critical(f"required settings not provided, shutting down - {e}")
    sys.exit(1)

result - my_list=['a', 'b']

Update to 2.6 and use pydantic_settings and this gets an error

from pydantic import Field
from pydantic_settings import BaseSettings
import os
import logging
import sys

class Settings(BaseSettings):
    my_list: list[str] = Field(..., env="MY_LIST")

    class Config:
        env_file = f'{os.environ.get("ENV")}.env'
        env_file_encoding = 'utf-8'
        env_prefix = f'{os.environ.get("ENV")}_'
        extra='ignore'

try:
    settings = Settings()
    print(settings)
except Exception as e:
    logging.critical(f"required settings not provided, shutting down - {e}")
    sys.exit(1)

CRITICAL:root:required settings not provided, shutting down - 1 validation error for Settings
my_list
  Input should be a valid list [type=list_type, input_value='["a","b"]', input_type=str]
    For further information visit https://errors.pydantic.dev/2.6/v/list_type

Is there any way to get the original behavior back?

Upvotes: 1

Views: 1087

Answers (2)

Oliver
Oliver

Reputation: 1

As of version 2 of pydantic, the configuration of a model is no longer done by a Config class, but via a ConfigDict.

The correct config should be:

model_config = SettingsConfigDict(env_file = '.env', env_file_encoding = 'utf-8')

Upvotes: 0

Peter Sampson
Peter Sampson

Reputation: 11

I was able to fix this by changing env="MY_LIST" to alias="MY_LIST". Oddly, the env_prefix (for other variables) was interfering with it.

Upvotes: 0

Related Questions