HTTPException is not picklable
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
import pickle
import unittest
from fastapi import HTTPException
def pickle_reduce(self):
return (HTTPException, (self.status_code, self.detail))
#uncomment to fix
#HTTPException.__reduce__ = pickle_reduce
class TestModelStats(unittest.TestCase):
def test_pickle_exc_http(self): # this will fail
pbytes = pickle.dumps(
HTTPException(status_code = 404, detail="Not found")
)
obj = pickle.loads(pbytes)
print(type(obj), obj)
Description
This can be worked around using the above
see: # make this unpicklable # python - How to make a custom exception class with multiple init args pickleable - Stack Overflow # noqa # https://stackoverflow.com/questions/16244923/how-to-make-a-custom-exception-class-with-multiple-init-args-pickleable/36342588#36342588
----------------------------------------------------------------------
Traceback (most recent call last): File “test.py”, line 30, in test_pickle_exc_http obj = pickle.loads(pbytes) TypeError: init() missing 1 required positional argument: ‘status_code’
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.85.0
Python Version
Python 3.8.10
Additional Context
see also another related complaint online
Issue Analytics
- State:
- Created a year ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
python 3.x - TypeError: cannot pickle 'module' object in fastapi
I'm trying to use OAuth2 in my fastapi back-end project. and I. This belows is my code snippet from fastapi import APIRouter, HTTPException, ......
Read more >pickle — Python object serialization — Python 3.11.1 ...
The pickle module keeps track of the objects it has already serialized, so that later references to the same object won't be serialized...
Read more >TypeError: cannot pickle 'module' object in fastapi
I'm trying to use OAuth2 in my fastapi back-end project. and I. This belows is my code snippet from fastapi import APIRouter, HTTPException,...
Read more >The Python pickle Module: How to Persist Objects in Python
So, you have several different ways to serialize and deserialize objects in Python. But which one should you use? The short answer is...
Read more >HTTP Exceptions — Werkzeug Documentation (2.2.x)
However, they are not Werkzeug response objects. You can get a response object by calling get_response() on a HTTP exception. Keep in mind...
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 FreeTop 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
Top GitHub Comments
I checked, there is no need for a PR. (A PR on this was already rejected.)
You can check for a solution here: https://github.com/encode/starlette/discussions/1669
i discovered this while throwing httpexception from multiprocessing worker