Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variable parsing exception #132

Closed
YongJie-Xie opened this issue Jul 24, 2023 · 4 comments
Closed

Environment variable parsing exception #132

YongJie-Xie opened this issue Jul 24, 2023 · 4 comments
Assignees

Comments

@YongJie-Xie
Copy link

YongJie-Xie commented Jul 24, 2023

Description

When I set the variable type to str | None, it uses json.loads() to parse the environment variable, so the variable type will change from str to int type, and this exception is thrown.

Although setting to str type can avoid the problem, I think this is a bug that can be fixed.

Thx. ^_^

Example code

import os

from pydantic_settings import BaseSettings

os.environ['AUTH_KEY'] = '123456'


class Settings(BaseSettings):
    auth_key: str | None = None


settings = Settings()
print(settings.auth_key)

Exception

pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
auth_key
  Input should be a valid string [type=string_type, input_value=123456, input_type=int]
    For further information visit https://errors.pydantic.dev/2.0.3/v/string_type

Version

pydantic-settings version : 2.0.2
pydantic version          : 2.0.3
pydantic-core version     : 2.3.0 release build profile
python version            : 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]
platform                  : Windows-10-10.0.20348-SP0

Selected Assignee: @dmontagu

@hramezani
Copy link
Member

When I set the variable type to str | None, it uses json.loads() to parse the environment variable, so the variable type will change from str to int type, and this exception is thrown.

Yes, because pydantic-settings consider this field as a complex field and will parse it by json.loads().

Although setting to str type can avoid the problem, I think this is a bug that can be fixed.

With this change, it is not a complex field anymore.

I think it's not a bug. you can fix it by changing your env to a string.

import os

from pydantic_settings import BaseSettings

os.environ['AUTH_KEY'] = '"123456"'  # json.loads() will convert it to str


class Settings(BaseSettings):
    auth_key: str | None = None


settings = Settings()
print(settings.auth_key)

@dmontagu dmontagu assigned hramezani and unassigned dmontagu Jul 24, 2023
@YongJie-Xie
Copy link
Author

YongJie-Xie commented Jul 24, 2023

I don't think this is a friendly solution, a friendly solution should be to split types such as str|None, int|None, float|None into non-complex and None, and then parse them separately.

When I use ".env" file to parse, it will appear weird, for example:

# .env file
AUTH_KEY=""123456""

@hramezani
Copy link
Member

You can use the same syntax in .env file as well

# .env file
AUTH_KEY='"123456"'

BTW, we are open to accept a PR to change it

@YongJie-Xie
Copy link
Author

Okay, thank you. I'll try it out.

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

No branches or pull requests

3 participants