diff --git a/pipeline_service/llm_bridge.py b/pipeline_service/llm_bridge.py index 37124c7..f333856 100644 --- a/pipeline_service/llm_bridge.py +++ b/pipeline_service/llm_bridge.py @@ -110,7 +110,17 @@ async def _post_chat_completion(url: str, headers: dict, payload: dict) -> dict: await asyncio.sleep(2 * (attempt + 1)) continue raise err - return await resp.json() + data = await resp.json() + # 网关偶发返回 200 但内容无 choices(错误 JSON),视为可重试的瞬时异常; + # 不处理会导致上层 llm_call_msgs_native 的 data["choices"] 抛 KeyError。 + if not isinstance(data, dict) or "choices" not in data: + err = ValueError("LLM 响应缺 choices: %s" % json.dumps(data, ensure_ascii=False)[:300]) + if attempt < _LLM_MAX_ATTEMPTS - 1: + last_exc = err + await asyncio.sleep(2 * (attempt + 1)) + continue + raise err + return data except (asyncio.TimeoutError, aiohttp.ClientError) as e: last_exc = e if attempt < _LLM_MAX_ATTEMPTS - 1: