-
|
I have the following in .env file: export A_IDS=1228and a: list[int]but I am getting a
Input should be a valid list [type=list_type, input_value=1228, input_type=int]
For further information visit https://errors.pydantic.dev/2.12/v/list_typeI looked at
but I didn't find anything related to my query. There is even a related example: os.environ['numbers'] = '1,2,3'
print(Settings().model_dump())
#> {'numbers': [1, 2, 3]}my only difference is: for now, I have only one value. I tried to cheat with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
hi @stdedos u can try: result: hope it ll solve ur problem |
Beta Was this translation helpful? Give feedback.
-
|
Hey @stdedos! The doc example uses The cleanest way is to change your .env to: export A_IDS="[1228]"This makes pydantic_settings treat it as a JSON array with one element. The response above with But if you really need to keep the format as from pydantic import BaseModel, field_validator
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
a_ids: list[int]
@field_validator('a_ids', mode='before')
@classmethod
def parse_single_to_list(cls, v):
if isinstance(v, str):
# Check if it's already a bracketed list like "[123]"
if v.startswith('['):
return v
# Otherwise wrap single values in brackets
return f"[{v}]"
return v
settings = Settings()That way Let me know if it works! |
Beta Was this translation helpful? Give feedback.
Hey @stdedos! The doc example uses
1,2,3because pydantic_settings splits by comma by default. When you have only one value likeA_IDS=1228, you need to wrap it in brackets so it parses as a list.The cleanest way is to change your .env to:
This makes pydantic_settings treat it as a JSON array with one element. The response above with
"[123]"is on the right track.But if you really need to keep the format as
A_IDS=1228without brackets, you can use a field validator: