From b27264dcfa1e6b1e2241cdf0cadec790c3c9ddcd Mon Sep 17 00:00:00 2001 From: ssw Date: Tue, 14 Jul 2026 18:40:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20SswLLMClient=20OpenAI=20=E5=93=8D?= =?UTF-8?q?=E5=BA=94=20schema=20null-check=20(P0=20fastjson=20NPE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 背景: --ai 路径失败时, 错误信息显示 Cannot invoke 'com.alibaba.fastjson.JSONArray.getJSONObject(int)' because the return value of 'com.alibaba.fastjson.JSONObject.getJSONArray(String)' is null 根因: SswLLMClient.java:160 链式调用 respJson.getJSONArray('choices').getJSONObject(0).getJSONObject('message').getString('content') LLM 限流/超时/错误响应可能只有 {error:...} 无 choices, fastjson 返回 null 后链式调用炸. 修复: 完整 null-check, 任一层为 null 抛出明确错误信息 (含响应体), 让上层能 fallback 重试. 实测: --ai 跑 5 次, 4/5 完整成功, 0 NPE. --- .../java/ssw/common/util/SswLLMClient.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java b/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java index 13e376c..53958bb 100644 --- a/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java +++ b/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java @@ -156,10 +156,24 @@ public class SswLLMClient { usage.getInteger("output_tokens"), maxTokens); } } else { - content = respJson.getJSONArray("choices") - .getJSONObject(0) - .getJSONObject("message") - .getString("content"); + // v0.20+ P0 修复: OpenAI 响应 schema 完整 null-check, 防 fastjson NPE + // 背景: LLM 限流/超时/错误响应可能只有 {"error":...} 而无 choices, 链式调用炸 + JSONArray choicesArr = respJson.getJSONArray("choices"); + if (choicesArr == null || choicesArr.isEmpty()) { + throw new RuntimeException("OpenAI 响应无 choices: " + respJson.toJSONString()); + } + JSONObject choice0 = choicesArr.getJSONObject(0); + if (choice0 == null) { + throw new RuntimeException("OpenAI choices[0] 为空: " + respJson.toJSONString()); + } + JSONObject message = choice0.getJSONObject("message"); + if (message == null) { + throw new RuntimeException("OpenAI choices[0].message 为空: " + respJson.toJSONString()); + } + content = message.getString("content"); + if (content == null) { + throw new RuntimeException("OpenAI choices[0].message.content 为空: " + respJson.toJSONString()); + } JSONObject usage = respJson.getJSONObject("usage"); if (usage != null) { log.info("[SswLLMClient] usage: prompt={} completion={}/{}",