From be038ab87814a93db238d9b0ebbdfe5e7c6a6317 Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Sat, 27 Apr 2024 19:07:41 +0100 Subject: [PATCH] fix: handle carriage returns in OpenAI streams --- src/lib/apis/streaming/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/apis/streaming/index.ts b/src/lib/apis/streaming/index.ts index c3c1d23a..19e4d8fc 100644 --- a/src/lib/apis/streaming/index.ts +++ b/src/lib/apis/streaming/index.ts @@ -26,7 +26,11 @@ async function* openAIStreamToIterator( break; } const lines = value.split('\n'); - for (const line of lines) { + for (let line of lines) { + if (line.endsWith('\r')) { + // Remove trailing \r + line = line.slice(0, -1); + } if (line !== '') { console.log(line); if (line === 'data: [DONE]') {