[BUG] TypeError: Object of type '_GenericAlias' is not JSON serializable
See original GitHub issueDescribe the bug
I am unable to debug the issue with some of my model schemas (the traceback for this issue is not helpful). For context, I have a Base model that contains the relevant json_encoders for the non standard fields (datetime, ObjectId, etc), yet there is some pydantic field that isn’t being translated.
The schemas uses the following EXTRA fields (from pydantic):
AnyUrlDictSecretStrFieldUnion
Environment
- OS: Linux
- FastAPI Version 0.5.2
- Python version: 3.7.4
Traceback
...
File "/usr/local/lib/python3.7/site-packages/pydantic/schema.py", line 503, in model_type_schema
f, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix, known_models=known_models
File "/usr/local/lib/python3.7/site-packages/pydantic/schema.py", line 186, in field_schema
s['default'] = encode_default(field.default)
File "/usr/local/lib/python3.7/site-packages/pydantic/schema.py", line 725, in encode_default
return pydantic_encoder(dft)
File "/usr/local/lib/python3.7/site-packages/pydantic/json.py", line 59, in pydantic_encoder
raise TypeError(f"Object of type '{obj.__class__.__name__}' is not JSON serializable")
TypeError: Object of type '_GenericAlias' is not JSON serializable
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
FastAPI TypeError: Object of type 'ModelMetaclass' is not ...
I triggered the same error message by effectively misplacing the ... error but it was Object of type 'type' is not JSON serializable...
Read more >TypeError: Object of type method is not JSON serializable
The Python TypeError: Object of type method is not JSON serializable occurs when we try to serialize a method to JSON. To solve...
Read more >tiangolo/fastapi - Gitter
The first one gives no issue, but the second one makes the app raise TypeError: Object of type BomItemWrite is not JSON serializable...
Read more >Error "Object of type _DynamicStructure is not JSON ...
Using the pyraven library, I'm having difficulty in serializing an ... TypeError: Object of type _DynamicStructure is not JSON serializable. Cause is:.
Read more >TypeError: Object of type is not JSON serializable - Medium
Python flask-sqlalchemy model objects is not json serializable. If your code returns query result in rest method, this error is raised..
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
In looking at the error again, I did a search for _GenericAlias and it looks like that used in the internals of the typing module. So my guess is that somewhere you’re assigning a type to a field in Pydantic instead of using a type annotation (e.g.
field = Listinstead offield: List).That doesn’t help you narrow down where the error is just what it might be.
When does this exception happen? When you start up the app or when you call a certain endpoint?
If on startup you could try commenting out some end points (or even better, whole routers if you use them) to narrow down the issue.
You could also navigate to the Pyndantic source in your environment and put in a breakpoint (if you’re using an IDE) or print statement.
File “/usr/local/lib/python3.7/site-packages/pydantic/schema.py”, line 186 maybe?
The issue is using a generic from the
typingmodule as a value instead of just a type hint. Specificallyinvitees_in: List[InviteePatch] = List[InviteePatch]is saying “assign this generic type to my variable if a value is not provided”.There are multiple ways the fix it but I assume you want an empty list as a default so you would do something like
invitees_in: List[InviteePatch] = Body([]).If you don’t want the default and can make it a required param then just do
invitees_in: List[InviteePatch]