question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HTTPException is not picklable

See original GitHub issue

First 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

https://pythontechworld.com/issue/encode/starlette/1663

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
iudeencommented, Oct 1, 2022

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

1reaction
davidgrohmanncommented, Sep 30, 2022

i discovered this while throwing httpexception from multiprocessing worker

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found