Skip to content

Commit cc31e35

Browse files
committed
LLMOpenAI: filter out "[DONE]" event in streamed responses
See apple/swift-openapi-generator#622
1 parent 40acb59 commit cc31e35

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Sources/SpeziLLMOpenAI/LLMOpenAISession+Generation.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ extension LLMOpenAISession {
3232
let response = try await chatGPTClient.createChatCompletion(openAIChatQuery)
3333

3434
// FIXME: does not handle the "[DONE]" message
35-
let chatStream = try response.ok.body.text_event_hyphen_stream
35+
// let chatStream = try response.ok.body.text_event_hyphen_stream
36+
// .asDecodedServerSentEventsWithJSONData(of: Components.Schemas.CreateChatCompletionStreamResponse
37+
// .self)
38+
let chatStream = try await response.ok.body.text_event_hyphen_stream.asDecodedServerSentEvents()
39+
.filter { $0.data != "[DONE]" }
40+
.asEncodedServerSentEvents()
3641
.asDecodedServerSentEventsWithJSONData(of: Components.Schemas.CreateChatCompletionStreamResponse
3742
.self)
3843

@@ -99,8 +104,6 @@ extension LLMOpenAISession {
99104
Self.logger.error("SpeziLLMOpenAI: Connectivity Issues with the OpenAI API: \(error)")
100105
await finishGenerationWithError(LLMOpenAIError.connectivityIssues(error), on: continuation)
101106
return
102-
} catch let Swift.DecodingError.dataCorrupted(context) {
103-
// FIXME: The API sends a "[DONE]" message to conclude the stream. As it does not conform to the Components.Schemas.CreateChatCompletionStreamResponse schema, it will cause a crash. This is a hacky workaround to avoid the crash.
104107
} catch {
105108
Self.logger.error("SpeziLLMOpenAI: Generation error occurred - \(error)")
106109
await finishGenerationWithError(LLMOpenAIError.generationError, on: continuation)

0 commit comments

Comments
 (0)