fix: simplify gemini stream handler, remove broken retry loop
Browse files400 unmodified lines40140240340440540640712 unmodified lines420421422423424425426427428159 unmodified lines588589590591592593594595596597598599600400 unmodified lineslast_rate_exc = Nonebreakexcept genai_errors.APIError as exc:if exc.status_code == 429:last_rate_exc = exclogger.warning("Gemini rate limit attempt %d/3", attempt + 1, extra=extra)else:12 unmodified linesexcept genai_errors.APIError as exc:_decrement_quota(uid)if exc.status_code in (401, 403):raise HTTPException(status_code=502, detail="Error de autenticación con el proveedor") from exclogger.error("Gemini API error %s: %s", exc.status_code, exc, extra=extra)raise HTTPException(status_code=502, detail="Error del proveedor") from excexcept HTTPException:raise159 unmodified lineslogger.info("Gemini stream OK model=%s", model_name, extra=extra)except genai_errors.APIError as exc:_decrement_quota(uid)code = getattr(exc, "status_code", 0)if code == 429:logger.warning("Gemini stream rate limit", extra=extra)yield _sse({"error": "upstream_rate_limit", "retryable": True})else:logger.error("Gemini stream API error %s: %s", code, exc, extra=extra)yield _sse({"error": "upstream_error", "message": str(exc)})except asyncio.TimeoutError:_decrement_quota(uid)logger.warning("Gemini stream timeout", extra=extra)400 unmodified lines40140240340440540640712 unmodified lines420421422423424425426427428429159 unmodified lines589590591592593594595596597598599600601602400 unmodified lineslast_rate_exc = Nonebreakexcept genai_errors.APIError as exc:if "429" in str(exc) or "RESOURCE_EXHAUSTED" in str(exc):last_rate_exc = exclogger.warning("Gemini rate limit attempt %d/3", attempt + 1, extra=extra)else:12 unmodified linesexcept genai_errors.APIError as exc:_decrement_quota(uid)exc_str = str(exc)if "401" in exc_str or "403" in exc_str or "UNAUTHENTICATED" in exc_str or "PERMISSION_DENIED" in exc_str:raise HTTPException(status_code=502, detail="Error de autenticación con el proveedor") from exclogger.error("Gemini API error: %s", exc_str[:200], extra=extra)raise HTTPException(status_code=502, detail="Error del proveedor") from excexcept HTTPException:raise159 unmodified lineslogger.info("Gemini stream OK model=%s", model_name, extra=extra)except genai_errors.APIError as exc:_decrement_quota(uid)exc_str = str(exc)is_429 = "429" in exc_str or "RESOURCE_EXHAUSTED" in exc_strif is_429:logger.warning("Gemini stream rate limit/quota: %s", exc_str[:120], extra=extra)yield _sse({"error": "upstream_rate_limit", "retryable": True})else:logger.error("Gemini stream API error: %s", exc_str[:200], extra=extra)yield _sse({"error": "upstream_error", "message": exc_str})except asyncio.TimeoutError:_decrement_quota(uid)logger.warning("Gemini stream timeout", extra=extra)
main.py+10-8