Add ``__reduce__`` method to Exception types

Author: deMarigny-ZEFRCreated Sep 12, 2025Updated Sep 25, 2026

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    "traceback: Traceback (most recent call last):",
    "  File \"/opt/conda/lib/python3.10/site-packages/ray/exceptions.py\", line 51, in from_ray_exception",
    "    return pickle.loads(ray_exception.serialized_exception)",
    "  File \"/opt/conda/lib/python3.10/site-packages/openai/_exceptions.py\", line 101, in __init__",
    "    super().__init__(message=\"Request timed out.\", request=request)",
    "TypeError: super(type, obj): obj must be an instance or subtype of type",
    "",
    "The above exception was the direct cause of the following exception:",
    "",
    "Traceback (most recent call last):",
    "  File \"/opt/conda/lib/python3.10/site-packages/ray/_private/serialization.py\", line 460, in deserialize_objects",
    "    obj = self._deserialize_object(data, metadata, object_ref)",
    "  File \"/opt/conda/lib/python3.10/site-packages/ray/_private/serialization.py\", line 342, in _deserialize_object",
    "    return RayError.from_bytes(obj)",
    "  File \"/opt/conda/lib/python3.10/site-packages/ray/exceptions.py\", line 45, in from_bytes",
    "    return RayError.from_ray_exception(ray_exception)",
    "  File \"/opt/conda/lib/python3.10/site-packages/ray/exceptions.py\", line 54, in from_ray_exception",
    "    raise RuntimeError(msg) from e",
    "RuntimeError: Failed to unpickle serialized exception",

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