Skip to content

Commit ea800e0

Browse files
authored
Merge pull request #1816 from octo-patch/fix/issue-1808-1788-null-content-sse-stream
fix: handle null content in SSE stream chunks for REST and Zhipu AI
2 parents 5c8649e + c914ea2 commit ea800e0

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/rest/listener/RestAIEventSourceListener.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public void onEvent(EventSource eventSource, String id, String type, String data
6464
String text = chatCompletions.getChoices().get(0).getDelta()==null?
6565
chatCompletions.getChoices().get(0).getText()
6666
:chatCompletions.getChoices().get(0).getDelta().getContent();
67+
// Local LLMs (e.g. llama.cpp) may return null content in the first SSE chunk
68+
// (only role is present). Treat null as empty string to avoid NullPointerException.
69+
if (text == null) {
70+
text = "";
71+
}
6772
message.setContent(text);
6873
sseEmitter.send(SseEmitter.event()
6974
.id(id)

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/listener/ZhipuChatAIEventSourceListener.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public void onEvent(EventSource eventSource, String id, String type, @NotNull St
6262
String text = chatCompletions.getChoices().get(0).getDelta()==null?
6363
chatCompletions.getChoices().get(0).getText()
6464
:chatCompletions.getChoices().get(0).getDelta().getContent();
65+
// The first SSE chunk may carry only "role" with null "content".
66+
// Guard against NullPointerException from Message.setContent().
67+
if (text == null) {
68+
text = "";
69+
}
6570

6671
Message message = new Message();
6772
message.setContent(text);

0 commit comments

Comments
 (0)