From 3becd8ea08064b99f314535457dd3014df8d51e5 Mon Sep 17 00:00:00 2001 From: hmo Date: Sat, 15 Aug 2026 16:56:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20empty=5Fcontent=20false-positive=20on=20?= =?UTF-8?q?reasoning=20models=20=E2=80=94=20deepseek-v4-flash/pro=20return?= =?UTF-8?q?=20content=20in=20reasoning=5Fcontent;=20only=20treat=20as=20em?= =?UTF-8?q?pty=20when=20no=20content=20AND=20no=20reasoning=20AND=20not=20?= =?UTF-8?q?finish=5Freason=3Dlength?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gateway/scripts/ocg_router.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/gateway/scripts/ocg_router.py b/gateway/scripts/ocg_router.py index dbea9e0..741320e 100644 --- a/gateway/scripts/ocg_router.py +++ b/gateway/scripts/ocg_router.py @@ -612,10 +612,21 @@ class RouterHandler(BaseHTTPRequestHandler): 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"} + msg = d.get("choices", [{}])[0].get("message", {}) or {} + content = msg.get("content", "") + # 推理模型(deepseek-v4-flash/pro 等)回复在 reasoning_content, + # content 可能为空(尤其 max_tokens 小时 token 全用于推理)—— + # 有 reasoning_content 或 finish_reason=length 都视为正常,不算空输出 + reasoning = msg.get("reasoning_content", "") + finish_reason = d.get("choices", [{}])[0].get("finish_reason", "") + has_reasoning = bool(reasoning and str(reasoning).strip()) + if (not content or not str(content).strip()) and not has_reasoning: + if finish_reason == "length": + # max_tokens 截断,content 可能为空但属于正常响应 + log.warning("content empty but finish_reason=length (truncated), passing through") + else: + 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)