diff --git a/gateway/scripts/ocg_router.py b/gateway/scripts/ocg_router.py index c8680c4..78337ea 100644 --- a/gateway/scripts/ocg_router.py +++ b/gateway/scripts/ocg_router.py @@ -584,8 +584,19 @@ class RouterHandler(BaseHTTPRequestHandler): err_body = resp.read().decode("utf-8", errors="replace")[:500] return {"ok": False, "status": status, "error": f"HTTP {status}: {err_body}"} - # 透传响应 + # 透传响应 + 空输出检测(2026-08-14 老莫:空输出自动换 key) resp_body = resp.read() + if not is_stream: + # 非流式:解析 JSON 检测空输出 + try: + import json as _json + d = _json.loads(resp_body.decode("utf-8", errors="replace")) + content = d.get("choices", [{}])[0].get("message", {}).get("content", "") + if not content or not str(content).strip(): + log.warning("empty content detected via key, retrying next key") + return {"ok": False, "status": 0, "error": "empty_content"} + except Exception: + pass # 解析失败不阻断(正常透传) self._send_response(status, dict(resp.headers), resp_body) return {"ok": True}