google-genai 1.33.0__py3-none-any.whl → 1.53.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
google/genai/errors.py CHANGED
@@ -29,7 +29,7 @@ if TYPE_CHECKING:
29
29
  class APIError(Exception):
30
30
  """General errors raised by the GenAI API."""
31
31
  code: int
32
- response: Union['ReplayResponse', httpx.Response]
32
+ response: Union['ReplayResponse', httpx.Response, 'aiohttp.ClientResponse']
33
33
 
34
34
  status: Optional[str] = None
35
35
  message: Optional[str] = None
@@ -42,6 +42,9 @@ class APIError(Exception):
42
42
  Union['ReplayResponse', httpx.Response, 'aiohttp.ClientResponse']
43
43
  ] = None,
44
44
  ):
45
+ if isinstance(response_json, list) and len(response_json) == 1:
46
+ response_json = response_json[0]
47
+
45
48
  self.response = response
46
49
  self.details = response_json
47
50
  self.message = self._get_message(response_json)
@@ -100,7 +103,30 @@ class APIError(Exception):
100
103
  else:
101
104
  response_json = response.body_segments[0].get('error', {})
102
105
 
103
- status_code = response.status_code
106
+ cls.raise_error(response.status_code, response_json, response)
107
+
108
+ @classmethod
109
+ def raise_error(
110
+ cls,
111
+ status_code: int,
112
+ response_json: Any,
113
+ response: Optional[
114
+ Union['ReplayResponse', httpx.Response, 'aiohttp.ClientResponse']
115
+ ],
116
+ ) -> None:
117
+ """Raises an appropriate APIError subclass based on the status code.
118
+
119
+ Args:
120
+ status_code: The HTTP status code of the response.
121
+ response_json: The JSON body of the response, or a dict containing error
122
+ details.
123
+ response: The original response object.
124
+
125
+ Raises:
126
+ ClientError: If the status code is in the 4xx range.
127
+ ServerError: If the status code is in the 5xx range.
128
+ APIError: For other error status codes.
129
+ """
104
130
  if 400 <= status_code < 500:
105
131
  raise ClientError(status_code, response_json, response)
106
132
  elif 500 <= status_code < 600:
@@ -131,6 +157,13 @@ class APIError(Exception):
131
157
  'status': response.reason_phrase,
132
158
  }
133
159
  status_code = response.status_code
160
+ elif hasattr(response, 'body_segments') and hasattr(
161
+ response, 'status_code'
162
+ ):
163
+ if response.status_code == 200:
164
+ return
165
+ response_json = response.body_segments[0].get('error', {})
166
+ status_code = response.status_code
134
167
  else:
135
168
  try:
136
169
  import aiohttp # pylint: disable=g-import-not-at-top
@@ -148,10 +181,31 @@ class APIError(Exception):
148
181
  }
149
182
  status_code = response.status
150
183
  else:
151
- response_json = response.body_segments[0].get('error', {})
184
+ raise ValueError(f'Unsupported response type: {type(response)}')
152
185
  except ImportError:
153
- response_json = response.body_segments[0].get('error', {})
186
+ raise ValueError(f'Unsupported response type: {type(response)}')
187
+
188
+ await cls.raise_error_async(status_code, response_json, response)
154
189
 
190
+ @classmethod
191
+ async def raise_error_async(
192
+ cls, status_code: int, response_json: Any, response: Optional[
193
+ Union['ReplayResponse', httpx.Response, 'aiohttp.ClientResponse']
194
+ ]
195
+ ) -> None:
196
+ """Raises an appropriate APIError subclass based on the status code.
197
+
198
+ Args:
199
+ status_code: The HTTP status code of the response.
200
+ response_json: The JSON body of the response, or a dict containing error
201
+ details.
202
+ response: The original response object.
203
+
204
+ Raises:
205
+ ClientError: If the status code is in the 4xx range.
206
+ ServerError: If the status code is in the 5xx range.
207
+ APIError: For other error status codes.
208
+ """
155
209
  if 400 <= status_code < 500:
156
210
  raise ClientError(status_code, response_json, response)
157
211
  elif 500 <= status_code < 600: