Skip to content

Commit

Permalink
Correctly handle partial JSON at the end of the chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
PallavAg authored Oct 13, 2023
1 parent c45f332 commit 80e9160
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Sources/OpenAI/Private/StreamingSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class StreamingSession<ResultType: Codable>: NSObject, Identifiable, URLSe
return session
}()

private var prevChunkBuffer = ""

init(urlRequest: URLRequest) {
self.urlRequest = urlRequest
}
Expand All @@ -47,14 +49,16 @@ final class StreamingSession<ResultType: Codable>: NSObject, Identifiable, URLSe
onProcessingError?(self, StreamingError.unknownContent)
return
}
let jsonObjects = stringContent
let jsonObjects = "\(prevChunkBuffer)\(stringContent)"
.components(separatedBy: "data:")
.filter { $0.isEmpty == false }
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
prevChunkBuffer = ""

guard jsonObjects.isEmpty == false, jsonObjects.first != streamingCompletionMarker else {
return
}
jsonObjects.forEach { jsonContent in
jsonObjects.enumerated().forEach { (index, jsonContent) in
guard jsonContent != streamingCompletionMarker else {
return
}
Expand All @@ -77,7 +81,12 @@ final class StreamingSession<ResultType: Codable>: NSObject, Identifiable, URLSe
let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: jsonData)
onProcessingError?(self, decoded)
} catch {
onProcessingError?(self, apiError)
if index == jsonObjects.count - 1 {
// Chunk ends in a partial JSON
prevChunkBuffer = "data: \(jsonContent)"
} else {
onProcessingError?(self, apiError)
}
}
}
}
Expand Down

0 comments on commit 80e9160

Please sign in to comment.