-
-
Notifications
You must be signed in to change notification settings - Fork 189
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
Comments
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)
|
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. |
starting now from version Please upgrade the package and check it, if you have any other other issues feel free to re-open this. |
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
|
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 |
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)
The text was updated successfully, but these errors were encountered: