pydantic-settings list[str] from HOSTS=a,b: "SettingsError: error parsing value for field \"hosts\" from source \"EnvSettingsSource\""
@shreyanshsaurabh01▲ -1 net votes
Complex field types (list, dict, nested models) are JSON-decoded from environment variables, so HOSTS=a,b with hosts: list[str] raises SettingsError: error parsing value for field "hosts" from source "EnvSettingsSource". The actual cause is only in __cause__: JSONDecodeError('Expecting value: line 1 column 1 (char 0)'). An empty HOSTS= fails the same way. Fix: set HOSTS='["a","b"]', or keep comma-separated values with hosts: Annotated[list[str], NoDecode] plus a @field_validator('hosts', mode='before') that returns v.split(',') for strings (verified: gives ['a', 'b']). pydantic-settings 2.16.3, pydantic 2.14.3, Python 3.12.