Skip to content

Commit 4d355e9

Browse files
committed
Pickle exceptions with custom reduce
1 parent 650be39 commit 4d355e9

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/openai/_exceptions.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from typing import TYPE_CHECKING, Any, Optional, cast
6-
from typing_extensions import Literal
6+
from typing_extensions import Literal, Self
77

88
import httpx
99

@@ -66,6 +66,13 @@ def __init__(self, message: str, request: httpx.Request, *, body: object | None)
6666
self.param = None
6767
self.type = None
6868

69+
@classmethod
70+
def _reconstruct(cls, message: str, request: httpx.Request, body: object | None) -> Self:
71+
return cls(message, request, body=body)
72+
73+
def __reduce__(self) -> tuple[type[APIError], tuple[str, httpx.Request, object | None]]:
74+
return (self.__class__._reconstruct, (self.message, self.request, self.body))
75+
6976

7077
class APIResponseValidationError(APIError):
7178
response: httpx.Response
@@ -76,6 +83,13 @@ def __init__(self, response: httpx.Response, body: object | None, *, message: st
7683
self.response = response
7784
self.status_code = response.status_code
7885

86+
@classmethod
87+
def _reconstruct(cls, response: httpx.Response, body: object | None, message: str | None) -> Self:
88+
return cls(response, body, message=message)
89+
90+
def __reduce__(self) -> tuple[type[APIResponseValidationError], tuple[httpx.Response, object | None, str | None]]:
91+
return (self.__class__._reconstruct, (self.response, self.body, self.message))
92+
7993

8094
class APIStatusError(APIError):
8195
"""Raised when an API response has a status code of 4xx or 5xx."""
@@ -90,11 +104,25 @@ def __init__(self, message: str, *, response: httpx.Response, body: object | Non
90104
self.status_code = response.status_code
91105
self.request_id = response.headers.get("x-request-id")
92106

107+
@classmethod
108+
def _reconstruct(cls, message: str, response: httpx.Response, body: object | None) -> Self:
109+
return cls(message, response=response, body=body)
110+
111+
def __reduce__(self) -> tuple[type[APIStatusError], tuple[str, httpx.Response, object | None]]:
112+
return (self.__class__._reconstruct, (self.message, self.response, self.body))
113+
93114

94115
class APIConnectionError(APIError):
95116
def __init__(self, *, message: str = "Connection error.", request: httpx.Request) -> None:
96117
super().__init__(message, request, body=None)
97118

119+
@classmethod
120+
def _reconstruct(cls, message: str, request: httpx.Request) -> Self:
121+
return cls(message=message, request=request)
122+
123+
def __reduce__(self) -> tuple[type[APIConnectionError], tuple[str, httpx.Request]]:
124+
return (self.__class__._reconstruct, (self.message, self.request))
125+
98126

99127
class APITimeoutError(APIConnectionError):
100128
def __init__(self, request: httpx.Request) -> None:
@@ -149,6 +177,13 @@ def __init__(self, *, completion: ChatCompletion) -> None:
149177
super().__init__(msg)
150178
self.completion = completion
151179

180+
@classmethod
181+
def _reconstruct(cls, completion: ChatCompletion) -> Self:
182+
return cls(completion=completion)
183+
184+
def __reduce__(self) -> tuple[type[LengthFinishReasonError], tuple[ChatCompletion]]:
185+
return (self.__class__._reconstruct, (self.completion,))
186+
152187

153188
class ContentFilterFinishReasonError(OpenAIError):
154189
def __init__(self) -> None:

0 commit comments

Comments
 (0)