Add ``__reduce__`` method to Exception types
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 31.6k
- Forks
- 5.7k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 96
Description
Confirm this is a feature request for the Python library and not the underlying OpenAI API.
- This is a feature request for the Python library
Describe the feature or improvement you're requesting
When trying to use the openai-python library in an environment that requires pickling, like Ray, OpenAI exceptions cause failures like:
7m [39;49;00m[33m"traceback: Traceback (most recent call last):"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" File \"/opt/conda/lib/python3.10/site-packages/ray/exceptions.py\", line 51, in from_ray_exception"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" return pickle.loads(ray_exception.serialized_exception)"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" File \"/opt/conda/lib/python3.10/site-packages/openai/_exceptions.py\", line 101, in __init__"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" super().__init__(message=\"Request timed out.\", request=request)"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m"TypeError: super(type, obj): obj must be an instance or subtype of type"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m""[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m"The above exception was the direct cause of the following exception:"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m""[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m"Traceback (most recent call last):"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" File \"/opt/conda/lib/python3.10/site-packages/ray/_private/serialization.py\", line 460, in deserialize_objects"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" obj = self._deserialize_object(data, metadata, object_ref)"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" File \"/opt/conda/lib/python3.10/site-packages/ray/_private/serialization.py\", line 342, in _deserialize_object"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" return RayError.from_bytes(obj)"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" File \"/opt/conda/lib/python3.10/site-packages/ray/exceptions.py\", line 45, in from_bytes"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" return RayError.from_ray_exception(ray_exception)"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" File \"/opt/conda/lib/python3.10/site-packages/ray/exceptions.py\", line 54, in from_ray_exception"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m" raise RuntimeError(msg) from e"[39;49;00m,[37m[39;49;00m
[37m [39;49;00m[33m"RuntimeError: Failed to unpickle serialized exception"[39;49;00m,[37m[39;49;00m
This is because there is no __reduce__ method defined on the openai exceptions.
See Ray documentation on serialization here: https://docs.ray.io/en/latest/ray-core/objects/serialization.html#customized-serialization, im sure this would affect other runtime environments too
Can a __reduce__ method be added to exceptions so runtimes, like Ray, do not fail when there is an OpenAI exception?
Additional context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with openai/_exceptions.py, especially the exception constructor shown at line 101, and inspect how the exception types are defined. Add coverage for pickling and unpickling the affected exceptions, then verify that reconstructed exceptions no longer fail in serialization environments such as Ray.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100