Skip to content

Commit a050057

Browse files
committed
Better SSE Check
1 parent 0c07277 commit a050057

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

service/src/main/java/com/theokanning/openai/service/ResponseBodyCallback.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,32 @@ public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
5858
reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
5959
String line;
6060
SSE sse = null;
61+
boolean sseException = false;
62+
String sseExceptionLine = "";
6163

6264
while (!emitter.isCancelled() && (line = reader.readLine()) != null) {
6365
if (line.startsWith("data:")) {
64-
String data = line.substring(5).trim();
65-
sse = new SSE(data);
66+
sse = new SSE(line.substring(5).trim());
67+
if (sseException) sseException = false;
6668
} else if (line.equals("") && sse != null) {
6769
if (sse.isDone()) {
6870
if (emitDone) {
6971
emitter.onNext(sse);
7072
}
7173
break;
7274
}
73-
7475
emitter.onNext(sse);
7576
sse = null;
7677
} else {
77-
throw new SSEFormatException("Invalid sse format! " + line);
78+
sseException = true;
79+
sseExceptionLine = line;
7880
}
7981
}
8082

83+
if (sseException) { // This code is because sometimes the SSE might be present after some lines
84+
throw new SSEFormatException("Invalid sse format! " + sseExceptionLine);
85+
}
86+
8187
emitter.onComplete();
8288

8389
} catch (Throwable t) {

0 commit comments

Comments
 (0)