From be022755bf39cb9d73f000be915a5864d27dee7e Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 22 Nov 2023 11:20:48 +0800 Subject: [PATCH] fix parse exception when sse stream returns incomplete data --- lib/src/client/openai_client.dart | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/src/client/openai_client.dart b/lib/src/client/openai_client.dart index f743b26..9930d45 100644 --- a/lib/src/client/openai_client.dart +++ b/lib/src/client/openai_client.dart @@ -260,6 +260,9 @@ class OpenAIClient extends OpenAIWrapper { ) .then( (it) { + // Sometimes, the information in a response may be truncated, in which + // case it needs to be concatenated with the next one. + String tmpData = ''; it.data.stream.listen( (it) { final rawData = utf8.decode(it); @@ -276,9 +279,31 @@ class OpenAIClient extends OpenAIWrapper { return; } - controller - ..sink - ..add(complete(json.decode(data))); + try { + controller + ..sink + ..add(complete(json.decode(data))); + tmpData = ''; + } on FormatException catch (_) { + // Sometimes, the information in a response may be truncated, + // in which case it needs to be concatenated with the next one. + tmpData = data; + } + } else { + // If the response does not start with 'dataļ¼š ', it is considered + // to be truncated, and at this time it needs to be concatenated + // together with 'tmpData'. + try { + final decodeData = json.decode('$tmpData$line'); + controller + ..sink + ..add(complete(decodeData)); + } catch (e) { + // skip + log.log('unexpected exception: $e'); + log.log('tmpData=$tmpData\nline=$line'); + } + tmpData = ''; } } },