-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
RootModel[str] cannot be parsed/requires double quotation. #342
Comments
Thanks @user1584 for reporting this issue.
Right now, it isn't possible to disable the json parsing. probably we need a flag to disable it. take a look at the similar issue and my comment on it. |
Thanks for the incredibly quick answer! |
It happens here. probably you can create a custom settings source inherited from EnvSettingsSource and change the behaviour for this field.
Not sure, because the |
Obviously I am mostly interested to solve my own little problem and I have next to no idea about the inner workings of pydantic/pydantic-settings. With that in mind, would it be viable solution to check whether the RootModels' attribute type is complex?
Of course, this would only be a solution for models that are based on RootModel. |
seems good. you can open a PR and change the behavior in |
I created a PR. However, the python 3.8 & 3.9 tests still fail. Do you have an idea why they fail but all other tests work? |
Fixed in 8fb9abb |
I am trying to create an extension of standard str class. My idea was to just create a RootModel based on str and then add some custom logic. However, I encountered strange problems when using the resulting class as part of the settings.
My RootModel looks like this (without custom logic):
Initializing with
CustomStr("hello world")
works as expected. The next step was to use it as part of the settings:This results in 'SettingsError: error parsing value for field "custom_str" from source "EnvSettingsSource"' (see complete traceback below).
If I switch the type of custom_str from CustomStr to standard str, the example works. During lots of debugging, I noticed that it works if I wrap the string in the environment variable in double quotes like
os.environ["custom_str"] = '"hello world"'
since this can be parsed by json.loads. Note: The order of single and double quotes matters!So, the question is: Is there any way to let pydantic/pydantic_settings handle the custom model like regular str class?
I am using pydantic version '2.7.1' and pydantic_settings version '2.2.1'.
Here's the complete traceback:
The text was updated successfully, but these errors were encountered: