auxiliary_error_shapes.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Shape readers for auxiliary-call errors: where an SDK error carries its HTTP status, and the
  2. status-less failures (HTTP-200 malformed answers, SSE ``error`` events) that still end a route.
  3. botocore's ``ClientError`` keeps its status in ``response["ResponseMetadata"]`` rather than on an
  4. attribute; the Bedrock client makes one attempt per call, so the auxiliary recovery ladder has to
  5. read it there to retry a 503 or treat a ``ThrottlingException`` as a rate limit.
  6. """
  7. from __future__ import annotations
  8. from typing import Any
  9. def _botocore_error(exc: Exception) -> tuple[Any, Any]:
  10. """``(HTTP status, error code)`` of a botocore ``ClientError``, whose ``response`` is the parsed
  11. dict; ``(None, None)`` for any other exception. The Bedrock client makes one attempt per call,
  12. so these classifiers own its retries the way they own the OpenAI SDK's (``max_retries=0``)."""
  13. response = getattr(exc, "response", None)
  14. if not isinstance(response, dict):
  15. return None, None
  16. return response.get("ResponseMetadata", {}).get("HTTPStatusCode"), response.get("Error", {}).get("Code")
  17. def _exc_http_status(exc: Exception) -> Any:
  18. """HTTP status on the exception itself, its ``response``, or a botocore error's response metadata."""
  19. return (getattr(exc, "status_code", None) or getattr(getattr(exc, "response", None), "status_code", None)
  20. or _botocore_error(exc)[0])
  21. def _is_invalid_aux_response_error(exc: Exception) -> bool:
  22. """HTTP-200 empty/malformed ChatCompletions — a capability failure routed like model incompatibility."""
  23. if not isinstance(exc, RuntimeError):
  24. return False
  25. msg = str(exc).lower()
  26. return "auxiliary " in msg and "llm returned invalid response" in msg and "choices[0].message" in msg
  27. def _is_statusless_structured_provider_error(exc: Exception) -> bool:
  28. """Detect a structured provider failure that has no HTTP status.
  29. OpenAI-compatible relays may commit SSE with status 200, then send an
  30. OpenAI-style ``error`` event. The SDK raises a status-less ``APIError`` with
  31. ``body=data["error"]`` — the INNER error object or a bare string (an
  32. ``{"error": ...}`` wrapper is accepted too). Any non-empty structured error in
  33. that status-less shape is a route failure; ordinary HTTP errors keep their
  34. existing status-based classifiers, and message text alone is insufficient.
  35. """
  36. if _exc_http_status(exc) is not None:
  37. return False
  38. body = getattr(exc, "body", None)
  39. err = body.get("error") if isinstance(body, dict) and "error" in body else body
  40. if isinstance(err, str):
  41. return bool(err.strip())
  42. return isinstance(err, dict) and any(err.get(k) for k in ("type", "code", "message"))