Skip to content

fix: handle null content in SSE stream chunks for REST and Zhipu AI#1816

Open
octo-patch wants to merge 1 commit intoCodePhiliaX:mainfrom
octo-patch:fix/issue-1808-1788-null-content-sse-stream
Open

fix: handle null content in SSE stream chunks for REST and Zhipu AI#1816
octo-patch wants to merge 1 commit intoCodePhiliaX:mainfrom
octo-patch:fix/issue-1808-1788-null-content-sse-stream

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #1808
Fixes #1788

Problem

When using local LLMs (e.g. llama.cpp) with the custom REST AI endpoint, or when using Zhipu AI, the SSE stream's first chunk typically contains only the role field with "content": null. The two affected listeners passed this null value directly to Message.setContent(), which throws a NullPointerException (caused by a @NonNull enforcement in Lombok-generated code), making the entire AI chat session fail immediately.

Affected files:

Solution

Added a null guard after extracting text from the delta: if text is null, it is replaced with an empty string before calling message.setContent(text). This matches the existing behaviour of FastChatAIEventSourceListener, which already handles this case correctly.

// Before (broken)
String text = ... getDelta().getContent();  // may be null
message.setContent(text);                   // NPE if null

// After (fixed)
String text = ... getDelta().getContent();
if (text == null) {
    text = "";
}
message.setContent(text);  // safe

Testing

  • Manually verified the diff aligns with the pattern already used in FastChatAIEventSourceListener.
  • The fix is minimal and confined to the two affected listener classes.

…isteners

When local LLMs (e.g. llama.cpp) or Zhipu AI return SSE stream responses,
the first chunk may contain only a "role" field with "content": null. This
caused a NullPointerException in Message.setContent() due to @nonnull
enforcement, making the entire AI chat session fail.

Treat null content as an empty string (matching the behaviour of
FastChatAIEventSourceListener) so the stream continues gracefully.

Fixes CodePhiliaX#1808
Fixes CodePhiliaX#1788
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Bug: zhipu api response can't be recorgnized

1 participant