From 510afab37c0085ef29e6dfa21d63b17c2a060b2e Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Fri, 26 Apr 2024 18:38:25 +0100 Subject: [PATCH 1/2] fix: catch any errors parsing openai sse events --- src/lib/apis/streaming/index.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lib/apis/streaming/index.ts b/src/lib/apis/streaming/index.ts index 5b89a4668..4c05a8de0 100644 --- a/src/lib/apis/streaming/index.ts +++ b/src/lib/apis/streaming/index.ts @@ -32,10 +32,14 @@ async function* openAIStreamToIterator( if (line === 'data: [DONE]') { yield { done: true, value: '' }; } else { - const data = JSON.parse(line.replace(/^data: /, '')); - console.log(data); + try { + const data = JSON.parse(line.replace(/^data: /, '')); + console.log(data); - yield { done: false, value: data.choices[0].delta.content ?? '' }; + yield { done: false, value: data.choices[0].delta.content ?? '' }; + } catch (e) { + console.error('Error extracting delta from SSE event:', e); + } } } } From 615e9e348f2a696a170627de11b3c5a3f9c0a913 Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Fri, 26 Apr 2024 18:42:43 +0100 Subject: [PATCH 2/2] fix: do not error out if OpenAI response has no delta --- src/lib/apis/streaming/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/apis/streaming/index.ts b/src/lib/apis/streaming/index.ts index 4c05a8de0..102a68966 100644 --- a/src/lib/apis/streaming/index.ts +++ b/src/lib/apis/streaming/index.ts @@ -36,7 +36,7 @@ async function* openAIStreamToIterator( const data = JSON.parse(line.replace(/^data: /, '')); console.log(data); - yield { done: false, value: data.choices[0].delta.content ?? '' }; + yield { done: false, value: data.choices?.[0]?.delta?.content ?? '' }; } catch (e) { console.error('Error extracting delta from SSE event:', e); }