SecretStr is not JSON serializable
See original GitHub issueBug / Feature request (not sure?)
- OS: Archlinux 5.0.5-arch1-1-ARCH SMP PREEMPT Wed Mar 27 17:53:10 UTC 2019 x86_64 GNU/Linux
- Python version
import sys; print(sys.version)
: 3.7.3 (default, Mar 26 2019, 21:43:19) [GCC 8.2.1 20181127] - Pydantic version
import pydantic; print(pydantic.VERSION)
: 0.23
from pydantic import BaseModel, SecretStr
class User(BaseModel):
username: str
password: SecretStr
user = User(username='nymous', password='my-password')
print(user)
print(user.dict())
print(user.json())
Expected result: I don’t really know, maybe a JSON with a ‘****’ field, or the field excluded.
Actual result:
Console output
User username='nymous' password=SecretStr('**********')
{'username': 'nymous', 'password': SecretStr('**********')}
Traceback (most recent call last):
File "/tmp/tmp.ZRa0tsEfhw/.venv/lib/python3.7/site-packages/pydantic/json.py", line 45, in pydantic_encoder
encoder = ENCODERS_BY_TYPE[type(obj)]
KeyError: <class 'pydantic.types.SecretStr'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 12, in <module>
print(user.json())
File "/tmp/tmp.ZRa0tsEfhw/.venv/lib/python3.7/site-packages/pydantic/main.py", line 312, in json
**dumps_kwargs,
File "/usr/lib/python3.7/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/tmp/tmp.ZRa0tsEfhw/.venv/lib/python3.7/site-packages/pydantic/json.py", line 47, in pydantic_encoder
raise TypeError(f"Object of type '{obj.__class__.__name__}' is not JSON serializable")
TypeError: Object of type 'SecretStr' is not JSON serializable
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
SecretStr is not JSON serializable · Issue #462 - GitHub
I don't really want to add more arguments to dict() and json() , but we can add an example of a custom encoder...
Read more >When I use fastapi and pydantic to build POST API, appear a ...
When I executed, I got TypeError: Object of type RolesSchema is not JSON serializable. How can I fix the program to normal operation?...
Read more >How To Resolve Pydantic Model Is Not Json Serializable
pydantic TypeError: Object of type is not JSON serializable code example. ... case of SecretStr NameEmail was left unsolved. import pydantic class MyModel....
Read more >pydantic — pydantic v0.28 documentation
The SecretStr and SecretBytes will be formatted as either '**********' or '' on conversion to json. from typing import List from pydantic import...
Read more >object of type cursor is not json serializable - You.com | The search ...
import json ; from bson import json_util ; for doc in cursor: ; json_doc = json.dumps(doc, default=json_util.default) ; json_docs.append(json_doc).
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t really want to add more arguments to
dict()
andjson()
, but we can add an example of a custom encoder that serialises secrets.Note my PR here: https://github.com/samuelcolvin/pydantic/pull/465