-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy patherrors.py
More file actions
420 lines (336 loc) · 15.8 KB
/
errors.py
File metadata and controls
420 lines (336 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import logging
from collections.abc import Callable
from functools import wraps
from typing import Any, NoReturn
from pydantic import BaseModel
from data_designer.engine.errors import DataDesignerError
from data_designer.engine.models.clients.errors import ProviderError, ProviderErrorKind, SyncClientUnavailableError
logger = logging.getLogger(__name__)
def _normalize_error_detail(detail: str | None) -> str | None:
if detail is None:
return None
normalized = " ".join(detail.split()).strip()
return normalized or None
def get_exception_primary_cause(exception: BaseException) -> BaseException:
"""Returns the primary cause of an exception by walking backwards.
This recursive walkback halts when it arrives at an exception which
has no provided __cause__ (e.g. __cause__ is None).
Args:
exception (Exception): An exception to start from.
Raises:
RecursionError: if for some reason exceptions have circular
dependencies (seems impossible in practice).
"""
if exception.__cause__ is None:
return exception
return get_exception_primary_cause(exception.__cause__)
class GenerationValidationFailureError(Exception):
summary: str
detail: str | None
failure_kind: str
def __init__(
self,
summary: str,
*,
detail: str | None = None,
failure_kind: str = "validation_error",
) -> None:
self.summary = summary.strip()
self.detail = _normalize_error_detail(detail)
self.failure_kind = failure_kind
message = self.summary
if self.detail is not None:
message = f"{message} Validation detail: {self.detail}"
super().__init__(message)
class ModelRateLimitError(DataDesignerError): ...
class ModelQuotaExceededError(DataDesignerError): ...
class ModelTimeoutError(DataDesignerError): ...
class ModelContextWindowExceededError(DataDesignerError): ...
class ModelAuthenticationError(DataDesignerError): ...
class ModelPermissionDeniedError(DataDesignerError): ...
class ModelNotFoundError(DataDesignerError): ...
class ModelUnsupportedParamsError(DataDesignerError): ...
class ModelBadRequestError(DataDesignerError): ...
class ModelInternalServerError(DataDesignerError): ...
class ModelUnsupportedCapabilityError(DataDesignerError): ...
class ModelAPIError(DataDesignerError): ...
class ModelUnprocessableEntityError(DataDesignerError): ...
class ModelAPIConnectionError(DataDesignerError): ...
class ModelStructuredOutputError(DataDesignerError): ...
class ModelGenerationValidationFailureError(DataDesignerError):
detail: str | None
failure_kind: str | None
def __init__(
self,
message: object | None = None,
*,
detail: str | None = None,
failure_kind: str | None = None,
) -> None:
if message is None:
super().__init__()
else:
super().__init__(message)
self.detail = _normalize_error_detail(detail)
self.failure_kind = failure_kind
class ImageGenerationError(DataDesignerError): ...
class FormattedLLMErrorMessage(BaseModel):
cause: str
solution: str
provider_message: str | None = None
def __str__(self) -> str:
lines = [" |----------"]
if self.provider_message is not None:
lines.append(f" | Provider message: {self.provider_message}")
lines.append(f" | Cause: {self.cause}")
lines.extend(
[
f" | Solution: {self.solution}",
" |----------",
]
)
return "\n".join(lines)
def _attach_provider_message(
formatted_message: FormattedLLMErrorMessage,
exception: ProviderError,
) -> FormattedLLMErrorMessage:
if exception.status_code != 400:
return formatted_message
normalized = _normalize_error_detail(exception.message)
if normalized is None:
return formatted_message
return formatted_message.model_copy(update={"provider_message": normalized})
def handle_llm_exceptions(
exception: Exception, model_name: str, model_provider_name: str, purpose: str | None = None
) -> None:
"""Handle LLM-related exceptions and convert them to appropriate DataDesignerError errors.
This method centralizes the exception handling logic for LLM operations,
making it reusable across different contexts.
Args:
exception: The exception that was raised
model_name: Name of the model that was being used
model_provider_name: Name of the model provider that was being used
purpose: The purpose of the model usage to show as context in the error message
Raises:
DataDesignerError: A more user-friendly error with appropriate error type and message
"""
purpose = purpose or "running generation"
authentication_error = FormattedLLMErrorMessage(
cause=f"The API key provided for model {model_name!r} was found to be invalid or expired while {purpose}.",
solution=f"Verify your API key for model provider and update it in your settings for model provider {model_provider_name!r}.",
)
match exception:
# Let SyncClientUnavailableError propagate so the async bridge proxy can catch it
case SyncClientUnavailableError():
raise
# Canonical ProviderError from the client adapter layer
case ProviderError():
_raise_from_provider_error(
exception,
exception.kind,
model_name,
model_provider_name,
purpose,
authentication_error,
)
case GenerationValidationFailureError():
detail_text = exception.detail.rstrip(".") if exception.detail is not None else None
validation_detail = f" Validation detail: {detail_text}." if detail_text is not None else ""
raise ModelGenerationValidationFailureError(
FormattedLLMErrorMessage(
cause=(
f"The model output from {model_name!r} could not be parsed into the requested format "
f"while {purpose}.{validation_detail}"
),
solution="This is most likely temporary as we make additional attempts. If you continue to see more of this, simplify or modify the output schema for structured output and try again. If you are attempting token-intensive tasks like generations with high-reasoning effort, ensure that max_tokens in the model config is high enough to reach completion.",
),
detail=exception.detail,
failure_kind=exception.failure_kind,
) from None
case DataDesignerError():
raise exception from None
case _:
raise DataDesignerError(
FormattedLLMErrorMessage(
cause=f"An unexpected error occurred while {purpose}.",
solution=f"Review the stack trace for more details: {exception}",
)
) from exception
def catch_llm_exceptions(func: Callable) -> Callable:
"""This decorator should be used on any `ModelFacade` method that could potentially raise
exceptions that should turn into upstream user-facing errors.
"""
@wraps(func)
def wrapper(model_facade: Any, *args: Any, **kwargs: Any) -> Any:
try:
return func(model_facade, *args, **kwargs)
except Exception as e:
logger.debug(
"\n".join(
[
"",
"|----------",
f"| Caught an exception downstream of type {type(e)!r}. Re-raising it below as a custom error with more context.",
"|----------",
]
),
exc_info=True,
stack_info=True,
)
handle_llm_exceptions(
e, model_facade.model_name, model_facade.model_provider_name, purpose=kwargs.get("purpose")
)
return wrapper
def acatch_llm_exceptions(func: Callable) -> Callable:
@wraps(func)
async def wrapper(model_facade: Any, *args: Any, **kwargs: Any) -> Any:
try:
return await func(model_facade, *args, **kwargs)
except Exception as e:
logger.debug(
"\n".join(
[
"",
"|----------",
f"| Caught an exception downstream of type {type(e)!r}. Re-raising it below as a custom error with more context.",
"|----------",
]
),
exc_info=True,
stack_info=True,
)
handle_llm_exceptions(
e, model_facade.model_name, model_facade.model_provider_name, purpose=kwargs.get("purpose")
)
return wrapper
def _raise_from_provider_error(
exception: ProviderError,
kind: ProviderErrorKind,
model_name: str,
model_provider_name: str,
purpose: str,
authentication_error: FormattedLLMErrorMessage,
) -> NoReturn:
"""Map a canonical ProviderError to the appropriate DataDesignerError subclass."""
_KIND_MAP: dict[ProviderErrorKind, type[DataDesignerError]] = {
ProviderErrorKind.RATE_LIMIT: ModelRateLimitError,
ProviderErrorKind.QUOTA_EXCEEDED: ModelQuotaExceededError,
ProviderErrorKind.TIMEOUT: ModelTimeoutError,
ProviderErrorKind.NOT_FOUND: ModelNotFoundError,
ProviderErrorKind.PERMISSION_DENIED: ModelPermissionDeniedError,
ProviderErrorKind.UNSUPPORTED_PARAMS: ModelUnsupportedParamsError,
ProviderErrorKind.INTERNAL_SERVER: ModelInternalServerError,
ProviderErrorKind.UNPROCESSABLE_ENTITY: ModelUnprocessableEntityError,
ProviderErrorKind.API_CONNECTION: ModelAPIConnectionError,
}
_MESSAGES: dict[ProviderErrorKind, tuple[str, str]] = {
ProviderErrorKind.RATE_LIMIT: (
f"You have exceeded the rate limit for model {model_name!r} while {purpose}.",
"Wait and try again in a few moments.",
),
ProviderErrorKind.TIMEOUT: (
f"The request to model {model_name!r} timed out while {purpose}.",
"Check your connection and try again. You may need to increase the timeout setting for the model.",
),
ProviderErrorKind.NOT_FOUND: (
f"The specified model {model_name!r} could not be found while {purpose}.",
f"Check that the model name is correct and supported by your model provider {model_provider_name!r} and try again.",
),
ProviderErrorKind.PERMISSION_DENIED: (
f"Your API key was found to lack the necessary permissions to use model {model_name!r} while {purpose}.",
f"Use an API key that has the right permissions for the model or use a model the API key in use has access to in model provider {model_provider_name!r}.",
),
ProviderErrorKind.UNSUPPORTED_PARAMS: (
f"One or more of the parameters you provided were found to be unsupported by model {model_name!r} while {purpose}.",
f"Review the documentation for model provider {model_provider_name!r} and adjust your request.",
),
ProviderErrorKind.INTERNAL_SERVER: (
f"Model {model_name!r} is currently experiencing internal server issues while {purpose}.",
f"Try again in a few moments. Check with your model provider {model_provider_name!r} if the issue persists.",
),
ProviderErrorKind.UNPROCESSABLE_ENTITY: (
f"The request to model {model_name!r} failed despite correct request format while {purpose}.",
"This is most likely temporary. Try again in a few moments.",
),
ProviderErrorKind.API_CONNECTION: (
f"Connection to model {model_name!r} hosted on model provider {model_provider_name!r} failed while {purpose}.",
"Check your network/proxy/firewall settings.",
),
}
if kind == ProviderErrorKind.AUTHENTICATION:
raise ModelAuthenticationError(authentication_error) from None
if kind == ProviderErrorKind.CONTEXT_WINDOW_EXCEEDED:
cause = (
f"The input data for model '{model_name}' was found to exceed its supported context width while {purpose}."
)
context_detail = _extract_context_window_detail(str(exception))
if context_detail:
cause = f"{cause} {context_detail}"
raise ModelContextWindowExceededError(
FormattedLLMErrorMessage(
cause=cause,
solution="Check the model's supported max context width. Adjust the length of your input along with completions and try again.",
)
) from None
if kind == ProviderErrorKind.QUOTA_EXCEEDED:
raise ModelQuotaExceededError(
FormattedLLMErrorMessage(
cause=(
f"Model provider {model_provider_name!r} reported insufficient credits or quota for model "
f"{model_name!r} while {purpose}."
),
solution=f"Add credits or increase quota/billing for model provider {model_provider_name!r} and try again.",
)
) from None
if kind == ProviderErrorKind.BAD_REQUEST:
err_msg = FormattedLLMErrorMessage(
cause=f"The request for model {model_name!r} was found to be malformed or missing required parameters while {purpose}.",
solution="Check your request parameters and try again.",
)
if "is not a multimodal model" in str(exception):
err_msg = FormattedLLMErrorMessage(
cause=f"Model {model_name!r} is not a multimodal model, but it looks like you are trying to provide multimodal context while {purpose}.",
solution="Check your request parameters and try again.",
)
raise ModelBadRequestError(_attach_provider_message(err_msg, exception)) from None
if kind in _KIND_MAP and kind in _MESSAGES:
error_cls = _KIND_MAP[kind]
cause_str, solution_str = _MESSAGES[kind]
raise error_cls(
_attach_provider_message(
FormattedLLMErrorMessage(cause=cause_str, solution=solution_str),
exception,
)
) from None
if kind == ProviderErrorKind.UNSUPPORTED_CAPABILITY:
raise ModelUnsupportedCapabilityError(
FormattedLLMErrorMessage(
cause=f"{exception.message.rstrip('.')} while {purpose}.",
solution=(
f"Use a model provider that supports this operation, or switch to a different model on "
f"{model_provider_name!r} that supports it."
),
)
) from None
# Fallback for API_ERROR and other unhandled kinds
raise ModelAPIError(
_attach_provider_message(
FormattedLLMErrorMessage(
cause=f"An unexpected API error occurred with model {model_name!r} while {purpose}.",
solution=f"Try again in a few moments. Check with your model provider {model_provider_name!r} if the issue persists.",
),
exception,
)
) from None
def _extract_context_window_detail(error_text: str) -> str | None:
"""Extract the specific token-count detail from an OpenAI-style context window error."""
marker = "this model's maximum context length is "
lower_text = error_text.lower()
if marker in lower_text:
start = lower_text.index(marker)
detail = error_text[start + len(marker) :].split("\n")[0].split(" Please reduce ")[0]
return f"This model's maximum context length is {detail}"
return None