From b413c2ac5eb2c3c23362f43ee6d583eadeab62da Mon Sep 17 00:00:00 2001 From: sama Pyb Date: Tue, 1 Dec 2026 19:13:50 +0800 Subject: [PATCH] Skip empty CRLF lines in JSONLines --- Sources/AnyLanguageModel/Shared/JSONLines.swift | 12 ++++++++---- .../HTTPTransportTests.swift | 16 ++++++++++++++++ Tests/AnyLanguageModelTests/JSONLinesTests.swift | 6 ++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Sources/AnyLanguageModel/Shared/JSONLines.swift b/Sources/AnyLanguageModel/Shared/JSONLines.swift index 38815d6b..f46490be 100644 --- a/Sources/AnyLanguageModel/Shared/JSONLines.swift +++ b/Sources/AnyLanguageModel/Shared/JSONLines.swift @@ -1,6 +1,6 @@ /// Splits a JSON Lines (NDJSON) body into its lines as the bytes arrive: each line ends with `\n`, -/// and the last one may not. Empty lines are skipped. A `\r` before the `\n` stays in the line, -/// where a JSON decoder reads it as whitespace. +/// and the last one may not. Empty lines (including `\r\n`) are skipped. A `\r` before the `\n` +/// stays in a non-empty line, where a JSON decoder reads it as whitespace. struct JSONLines { private var line: [UInt8] = [] @@ -11,7 +11,7 @@ struct JSONLines { return nil } defer { line.removeAll(keepingCapacity: true) } - return line.isEmpty ? nil : line + return hasContent ? line : nil } /// The lines `bytes` complete. @@ -22,6 +22,10 @@ struct JSONLines { /// The last line, once the body has ended, if it had no `\n`. mutating func finish() -> [UInt8]? { defer { line = [] } - return line.isEmpty ? nil : line + return hasContent ? line : nil + } + + private var hasContent: Bool { + line.contains { $0 != UInt8(ascii: "\r") } } } diff --git a/Tests/AnyLanguageModelTests/HTTPTransportTests.swift b/Tests/AnyLanguageModelTests/HTTPTransportTests.swift index f4a305f6..09d794af 100644 --- a/Tests/AnyLanguageModelTests/HTTPTransportTests.swift +++ b/Tests/AnyLanguageModelTests/HTTPTransportTests.swift @@ -47,6 +47,22 @@ struct HTTPTransportTests { #expect(response.content == "Hello") } + @Test func streamedEmptyCRLFLinesAreSkipped() async throws { + let transport = FakeTransport(replies: [ + .init(chunks: [ + "\r\n" + + #"{"model":"m","created_at":"2026-10-18T10:00:00.000Z","message":{"role":"assistant","content":"Hel"},"done":false}"# + + "\r\n\r\n", + #"{"model":"m","created_at":"2026-10-18T10:00:00.000Z","message":{"role":"assistant","content":"lo"},"done":true}"# + + "\r\n\r\n", + ]) + ]) + let model = OllamaLanguageModel(baseURL: Self.ollama, model: "m", transport: transport) + let response = try await LanguageModelSession(model: model).streamResponse(to: "Hi").collect() + + #expect(response.content == "Hello") + } + @Test(arguments: [false, true]) func incompleteOllamaTextThrows(streaming: Bool) async throws { let partial = diff --git a/Tests/AnyLanguageModelTests/JSONLinesTests.swift b/Tests/AnyLanguageModelTests/JSONLinesTests.swift index eac0ad5a..2febe10a 100644 --- a/Tests/AnyLanguageModelTests/JSONLinesTests.swift +++ b/Tests/AnyLanguageModelTests/JSONLinesTests.swift @@ -31,4 +31,10 @@ struct JSONLinesTests { struct Line: Decodable { let a: Int } #expect(try JSONDecoder().decode(Line.self, from: Data(lines[0].utf8)).a == 1) } + + @Test func emptyCRLFLinesAreSkipped() { + #expect(split(["\r\n{\"a\":1}\r\n\r\n"]) == ["{\"a\":1}\r"]) + #expect(split(["\r", "\n{\"a\":1}\r", "\n\r", "\n"]) == ["{\"a\":1}\r"]) + #expect(split(["{\"a\":1}\r\n\r"]) == ["{\"a\":1}\r"]) + } }