fix: SswLLMClient OpenAI 响应 schema null-check (P0 fastjson NPE)
背景: --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.
This commit is contained in:
1 parent
f66734d9a6
commit
b27264dcfa
1 file changed
+18
-4
@@ -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={}/{}",
|
||||
|
||||
Reference in new issue
Block a user