anasfik / openai

Dart/Flutter SDK for ChatGPT and all OpenAI APIs (GPT, Dall-e..)
https://pub.dev/packages/dart_openai
MIT License
591 stars 184 forks source link

Unhandled Exception caused by jsonDecode(data) in postStream() #153

Closed masa8 closed 9 months ago

masa8 commented 9 months ago

Hi,

I am now using a forked version with this PR change, but if it's not a problem, please put this fix in.

Thanks Masaya

Issue

When retrieving data from openai on streaming, an error occurred when decoding JsonString to Map, final decoded = jsonDecode(data) as Map<String, dynamic>; causing an unhandled exception.

It's weird because openai guarantees to return json, but if there are problems with the network environment, etc., it may not be json when it is received. I guess.

Version

dart_openai: ^4.0.0  # Actually happened when using it dart_openai: ^5.0.0. # Reproducible by debugging

Modifications

What we actually want to do is to try-catch the process of decoding, but since synchronous and asynchronous error handling is described in a complex way, I made the following modifications

Test Results

Ensured that errors are handled without causing an unhandled exception when an error occurs during decoding.

anasfik commented 9 months ago

Cool man, will merge this.

Thank you!