From 90a54abc9b403c136233788933f0c66ebbcf571d Mon Sep 17 00:00:00 2001 From: alpha nerd Date: Fri, 8 May 2026 12:19:03 +0200 Subject: [PATCH] feat: correct pass through of openai.APIStatusErrors --- router.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/router.py b/router.py index ab54cc7..603387e 100644 --- a/router.py +++ b/router.py @@ -418,7 +418,16 @@ async def enforce_router_api_key(request: Request, call_next): response.headers["Access-Control-Allow-Headers"] = "Authorization, Content-Type" response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS" return response - + + +@app.exception_handler(openai.APIStatusError) +async def _openai_api_status_error_handler(request: Request, exc: openai.APIStatusError): + """Forward upstream OpenAI-SDK status errors with their original status code and body + instead of letting them bubble up as 500s.""" + body = exc.body if exc.body is not None else {"error": {"message": str(exc), "code": exc.status_code}} + return JSONResponse(status_code=exc.status_code, content=body) + + # ------------------------------------------------------------- # 3. Global state: per‑endpoint per‑model active connection counters # -------------------------------------------------------------