Spaces:
Running
Running
Better handle rate limit errors that come as 413 or BadRequestError.
Browse filesAlso detect rate limits from response body type.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
53 unmodified lines54555657585960616263646519 unmodified lines85868788899091929394959697989910010110210310410510610710810953 unmodified lines logger.info("map_error: mapped to RateLimitError") return RateLimitError(message, raw_error=str(e)) if isinstance(e, openai.BadRequestError): # Check if it's actually a rate limit in disguise raw = str(e).lower() if "too_many_requests" in raw or "rate_limit" in raw or "quota" in raw: limiter.set_blocked(60) logger.info("map_error: BadRequestError actually rate limit") return RateLimitError(message, raw_error=str(e)) logger.info("map_error: mapped to InvalidRequestError") return InvalidRequestError(message, raw_error=str(e)) if isinstance(e, openai.InternalServerError):19 unmodified lines limiter.set_blocked(60) logger.info("map_error: mapped to RateLimitError (httpx)") return RateLimitError(message, raw_error=str(e)) if status == 413: # "Request too large" - often actually a rate limit or quota issue limiter.set_blocked(60) logger.info("map_error: mapped to RateLimitError (413)") return RateLimitError(message, raw_error=str(e)) if status == 400: logger.info("map_error: mapped to InvalidRequestError (httpx)") return InvalidRequestError(message, raw_error=str(e)) # Check response body for rate limit indicators try: body = e.response.json() if body and isinstance(body, dict): err_type = body.get("type", "") if "too_many_requests" in err_type or "rate_limit" in err_type.lower(): limiter.set_blocked(60) logger.info("map_error: detected rate limit from response body") return RateLimitError(message, raw_error=str(e)) except Exception: pass if status >= 500: if status in (502, 503, 504): logger.info("map_error: mapped to OverloadedError (httpx)")providers/error_mapping.py+22