From c95da8e236d389b9422453dede08bc8eaa97cbba Mon Sep 17 00:00:00 2001 From: oBlank Date: Sun, 28 Jan 2024 23:09:32 +0800 Subject: [PATCH] fix: fix json decode error when jsonContent is emtpy --- Sources/OpenAI/Private/StreamingSession.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/OpenAI/Private/StreamingSession.swift b/Sources/OpenAI/Private/StreamingSession.swift index a69e46cf..5120a6d9 100644 --- a/Sources/OpenAI/Private/StreamingSession.swift +++ b/Sources/OpenAI/Private/StreamingSession.swift @@ -57,6 +57,9 @@ final class StreamingSession: NSObject, Identifiable, URLSe extension StreamingSession { private func processJSON(from stringContent: String) { + if stringContent.isEmpty { + return + } let jsonObjects = "\(previousChunkBuffer)\(stringContent)" .components(separatedBy: "data:") .filter { $0.isEmpty == false } @@ -68,7 +71,7 @@ extension StreamingSession { return } jsonObjects.enumerated().forEach { (index, jsonContent) in - guard jsonContent != streamingCompletionMarker else { + guard jsonContent != streamingCompletionMarker && !jsonContent.isEmpty else { return } guard let jsonData = jsonContent.data(using: .utf8) else {