Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling Unhandled Exceptions #53

Closed
jonny7737 opened this issue May 23, 2023 · 5 comments
Closed

Handling Unhandled Exceptions #53

jonny7737 opened this issue May 23, 2023 · 5 comments

Comments

@jonny7737
Copy link

jonny7737 commented May 23, 2023

This error was caused by the openai chat stream api call terminating mid-response. Do you have any recommendations on how to a) Message the user of the OpenAI issue and b) retry the request?

Two scenarios where this happens frequently are 1) when openai apis get busy and they respond with an error to try again later and 2) I exceed the total token limit. In the first case, I want to retry by sending a [Space] to nudge the AI to continue. In the second case I just want to notify the user with a dialog and provide instructions on how to continue.

Thanks again for an excelent package.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected end of input (at character 2)
{
^

#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1 _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:501:7)
#2 _parseJson (dart:convert-patch/convert_patch.dart:36:10)
#3 JsonDecoder.convert (dart:convert/json.dart:610:36)
#4 JsonCodec.decode (dart:convert/json.dart:216:41)
#5 jsonDecode (dart:convert/json.dart:155:10)
#6 OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:518:12)
#7 OpenAINetworkingClient.postStream.. (package:dart_openai/src/core/networking/client.dart:290:37)
#8 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#9 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#10 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#11 _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#12 _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#13 _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#14 _LineSplitterSink._addLines (dart:convert/line_splitter.dart:164:13)
#15 _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:131:7)
#16 StringConversionSink.add (dart:convert/string_conversion.dart:39:5)
#17 _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#18 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#19 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#20 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21 _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#22 _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#23 _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#24 _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:233:7)
#25 _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:307:20)
#26 _Utf8ConversionSink.add (dart:convert/string_conversion.dart:300:5)
#27 _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:69:18)
#28 _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#29 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#30 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#31 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#32 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#33 _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#34 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#35 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#36 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#37 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#38 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#39 _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#40 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#41 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#42 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#43 _DelayedData.perform (dart:async/stream_impl.dart:515:14)
#44 _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
#45 _PendingEvents.schedule. (dart:async/stream_impl.dart:591:7)
#46 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#47 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

@jonny7737
Copy link
Author

Here is the full list of exceptions thrown during 1 occurrence of scenario 2 above:

As you can see below, all of the exceptions are thrown from the same line of code::

#6 OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:518:12)

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected end of input (at character 2)
{
 ^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1      _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:501:7)
#2      _parseJson (dart:convert-patch/convert_patch.dart:36:10)
#3      JsonDecoder.convert (dart:convert/json.dart:610:36)
#4      JsonCodec.decode (dart:convert/json.dart:216:41)
#5      jsonDecode (dart:convert/json.dart:155:10)
#6      OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:518:12)
#7      OpenAINetworkingClient.postStream.<anonymous closure>.<anonymous closure> (package:dart_openai/src/core/networking/client.dart:290:37)
#8      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#11     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#12     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#13     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#14     _LineSplitterSink._addLines (dart:convert/line_splitter.dart:164:13)
#15     _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:131:7)
#16     StringConversionSink.add (dart:convert/string_conversion.dart:39:5)
#17     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#18     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#19     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#20     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#22     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#23     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#24     _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:233:7)
#25     _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:307:20)
#26     _Utf8ConversionSink.add (dart:convert/string_conversion.dart:300:5)
#27     _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:69:18)
#28     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#29     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#30     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#31     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#32     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#33     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#34     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#35     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#36     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#37     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#38     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#39     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#40     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#41     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#42     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#43     _DelayedData.perform (dart:async/stream_impl.dart:515:14)
#44     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
#45     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
#46     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#47     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 10)
  "error": {
         ^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:870:42)
#2      _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#3      JsonDecoder.convert (dart:convert/json.dart:610:36)
#4      JsonCodec.decode (dart:convert/json.dart:216:41)
#5      jsonDecode (dart:convert/json.dart:155:10)
#6      OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:518:12)
#7      OpenAINetworkingClient.postStream.<anonymous closure>.<anonymous closure> (package:dart_openai/src/core/networking/client.dart:290:37)
#8      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#11     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#12     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#13     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#14     _LineSplitterSink._addLines (dart:convert/line_splitter.dart:164:13)
#15     _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:131:7)
#16     StringConversionSink.add (dart:convert/string_conversion.dart:39:5)
#17     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#18     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#19     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#20     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#22     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#23     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#24     _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:233:7)
#25     _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:307:20)
#26     _Utf8ConversionSink.add (dart:convert/string_conversion.dart:300:5)
#27     _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:69:18)
#28     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#29     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#30     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#31     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#32     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#33     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#34     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#35     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#36     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#37     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#38     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#39     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#40     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#41     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#42     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#43     _DelayedData.perform (dart:async/stream_impl.dart:515:14)
#44     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
#45     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
#46     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#47     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 14)
    "message": "This model's maximum context length is 4097 tokens. However...
             ^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:870:42)
#2      _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#3      JsonDecoder.convert (dart:convert/json.dart:610:36)
#4      JsonCodec.decode (dart:convert/json.dart:216:41)
#5      jsonDecode (dart:convert/json.dart:155:10)
#6      OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:518:12)
#7      OpenAINetworkingClient.postStream.<anonymous closure>.<anonymous closure> (package:dart_openai/src/core/networking/client.dart:290:37)
#8      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#11     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#12     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#13     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#14     _LineSplitterSink._addLines (dart:convert/line_splitter.dart:164:13)
#15     _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:131:7)
#16     StringConversionSink.add (dart:convert/string_conversion.dart:39:5)
#17     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#18     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#19     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#20     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#22     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#23     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#24     _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:233:7)
#25     _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:307:20)
#26     _Utf8ConversionSink.add (dart:convert/string_conversion.dart:300:5)
#27     _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:69:18)
#28     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#29     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#30     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#31     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#32     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#33     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#34     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#35     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#36     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#37     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#38     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#39     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#40     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#41     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#42     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#43     _DelayedData.perform (dart:async/stream_impl.dart:515:14)
#44     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
#45     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
#46     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#47     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 11)
    "type": "invalid_request_error",
          ^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:870:42)
#2      _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#3      JsonDecoder.convert (dart:convert/json.dart:610:36)
#4      JsonCodec.decode (dart:convert/json.dart:216:41)
#5      jsonDecode (dart:convert/json.dart:155:10)
#6      OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:518:12)
#7      OpenAINetworkingClient.postStream.<anonymous closure>.<anonymous closure> (package:dart_openai/src/core/networking/client.dart:290:37)
#8      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#11     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#12     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#13     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#14     _LineSplitterSink._addLines (dart:convert/line_splitter.dart:164:13)
#15     _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:131:7)
#16     StringConversionSink.add (dart:convert/string_conversion.dart:39:5)
#17     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#18     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#19     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#20     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#22     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#23     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#24     _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:233:7)
#25     _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:307:20)
#26     _Utf8ConversionSink.add (dart:convert/string_conversion.dart:300:5)
#27     _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:69:18)
#28     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#29     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#30     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#31     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#32     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#33     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#34     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#35     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#36     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#37     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#38     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#39     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#40     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#41     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#42     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#43     _DelayedData.perform (dart:async/stream_impl.dart:515:14)
#44     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
#45     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
#46     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#47     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 12)
    "param": "messages",
           ^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:870:42)
#2      _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#3      JsonDecoder.convert (dart:convert/json.dart:610:36)
#4      JsonCodec.decode (dart:convert/json.dart:216:41)
#5      jsonDecode (dart:convert/json.dart:155:10)
#6      OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:518:12)
#7      OpenAINetworkingClient.postStream.<anonymous closure>.<anonymous closure> (package:dart_openai/src/core/networking/client.dart:290:37)
#8      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#11     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#12     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#13     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#14     _LineSplitterSink._addLines (dart:convert/line_splitter.dart:164:13)
#15     _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:131:7)
#16     StringConversionSink.add (dart:convert/string_conversion.dart:39:5)
#17     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#18     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#19     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#20     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#22     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#23     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#24     _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:233:7)
#25     _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:307:20)
#26     _Utf8ConversionSink.add (dart:convert/string_conversion.dart:300:5)
#27     _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:69:18)
#28     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#29     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#30     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#31     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#32     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#33     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#34     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#35     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#36     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#37     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#38     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#39     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#40     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#41     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#42     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#43     _DelayedData.perform (dart:async/stream_impl.dart:515:14)
#44     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
#45     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
#46     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#47     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 11)
    "code": "context_length_exceeded"
          ^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:870:42)
#2      _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#3      JsonDecoder.convert (dart:convert/json.dart:610:36)
#4      JsonCodec.decode (dart:convert/json.dart:216:41)
#5      jsonDecode (dart:convert/json.dart:155:10)
#6      OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:518:12)
#7      OpenAINetworkingClient.postStream.<anonymous closure>.<anonymous closure> (package:dart_openai/src/core/networking/client.dart:290:37)
#8      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#11     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#12     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#13     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#14     _LineSplitterSink._addLines (dart:convert/line_splitter.dart:164:13)
#15     _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:131:7)
#16     StringConversionSink.add (dart:convert/string_conversion.dart:39:5)
#17     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#18     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#19     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#20     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#22     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#23     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#24     _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:233:7)
#25     _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:307:20)
#26     _Utf8ConversionSink.add (dart:convert/string_conversion.dart:300:5)
#27     _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:69:18)
#28     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#29     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#30     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#31     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#32     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#33     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#34     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#35     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#36     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#37     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#38     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#39     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#40     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#41     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#42     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#43     _DelayedData.perform (dart:async/stream_impl.dart:515:14)
#44     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
#45     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
#46     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#47     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 3)
  }
  ^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:907:13)
#2      _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#3      JsonDecoder.convert (dart:convert/json.dart:610:36)
#4      JsonCodec.decode (dart:convert/json.dart:216:41)
#5      jsonDecode (dart:convert/json.dart:155:10)
#6      OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:518:12)
#7      OpenAINetworkingClient.postStream.<anonymous closure>.<anonymous closure> (package:dart_openai/src/core/networking/client.dart:290:37)
#8      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#11     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#12     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#13     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#14     _LineSplitterSink._addLines (dart:convert/line_splitter.dart:164:13)
#15     _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:131:7)
#16     StringConversionSink.add (dart:convert/string_conversion.dart:39:5)
#17     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#18     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#19     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#20     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#22     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#23     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#24     _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:233:7)
#25     _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:307:20)
#26     _Utf8ConversionSink.add (dart:convert/string_conversion.dart:300:5)
#27     _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:69:18)
#28     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#29     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#30     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#31     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#32     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#33     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#34     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#35     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#36     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#37     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#38     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#39     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#40     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#41     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#42     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#43     _DelayedData.perform (dart:async/stream_impl.dart:515:14)
#44     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
#45     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
#46     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#47     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 1)
}
^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:907:13)
#2      _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#3      JsonDecoder.convert (dart:convert/json.dart:610:36)
#4      JsonCodec.decode (dart:convert/json.dart:216:41)
#5      jsonDecode (dart:convert/json.dart:155:10)
#6      OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:518:12)
#7      OpenAINetworkingClient.postStream.<anonymous closure>.<anonymous closure> (package:dart_openai/src/core/networking/client.dart:290:37)
#8      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#11     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#12     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#13     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#14     _LineSplitterSink._addLines (dart:convert/line_splitter.dart:164:13)
#15     _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:131:7)
#16     StringConversionSink.add (dart:convert/string_conversion.dart:39:5)
#17     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#18     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#19     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#20     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#22     _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#23     _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#24     _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:233:7)
#25     _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:307:20)
#26     _Utf8ConversionSink.add (dart:convert/string_conversion.dart:300:5)
#27     _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:69:18)
#28     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#29     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#30     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#31     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#32     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#33     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#34     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#35     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#36     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#37     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#38     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#39     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#40     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#41     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#42     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#43     _DelayedData.perform (dart:async/stream_impl.dart:515:14)
#44     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
#45     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
#46     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#47     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

@jonny7737
Copy link
Author

jonny7737 commented May 23, 2023

I have what seems like a dumb work around:

Wrap runApp() with runZonedGuarded() this will give you the equivalent of a try{} catch(){} on the entire app. Then you have to figure out how to handle the error(s) as they com in. In my case, the exceeded max tokens error is coming from a stream so it shows up in the 'catch' in pieces. I had to reassemble the pieces and show a dialog when the pieces stop arriving.

This is what the runZonedGuarded 'catch' looked like in the end:

(error, stackTrace) {
    timer.cancel();
    timer = Timer(const Duration(seconds: 2), () {
      showError(errorsTrimmed.join(' '));
    });
    errors.addAll(error.toString().split('\n'));
    errorsTrimmed = [];
    for (var element in errors) {
      if (!element.startsWith('FormatException') && element.trim().isNotEmpty) {
        String e = element.trimRight();
        if (e.endsWith('...')) e += '", ';
        errorsTrimmed.add(e.replaceAll('^', '').trimLeft());
      }
    }
  }

This has been tested to work for the exceeding token limit which is really only a problem if you are using a streamed response setup. Otherwise openai tells you how many tokens are used with each response.

I'm certain there are better solutions to this problem. If you have one, I'd like to see tit.

@anasfik
Copy link
Owner

anasfik commented May 29, 2023

starting now from version 2.0.0, in your case you would get the full error response printed just in your debug console so you can know exactly what happens.

Please upgrade the package and check it, if you have any other other issues feel free to re-open this.

@anasfik anasfik closed this as completed May 29, 2023
@verrannt
Copy link

verrannt commented Jun 5, 2023

Please re-open this.

I am getting the same error, and the reason seems to be because the logger parses the response line-by-line. That means that OpenAINetworkingClient.decodeToMap is called on each line of the response body, and since the response body appears to be formatted with newlines, we get Unexpected end of input like below:

Unhandled Exception: FormatException: Unexpected end of input (at character 2)
"{"

@verrannt
Copy link

verrannt commented Jun 5, 2023

Also, and maybe I need to move this to a new issue, I cannot catch these exceptions. If I do

try {
  OpenAI.instance.model.list();
} on Exception {
  print('Exception thrown');
}

it does not catch the above mentioned FormatException. I assume this somehow relates to the fact that OpenAI.instance is a singleton and the exception is not thrown in the scope where I'm executing the above code but where the singleton is defined (but I'm not sure). This is the reason why a work-around like @jonny7737 proposed above is required, which IMO shouldn't be the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants