flutter_gemini icon indicating copy to clipboard operation
flutter_gemini copied to clipboard

Missing spaces in streamChat, streamGenerateContent, and promptStream output in gemini_implement.dart

Open nhao2003 opened this issue 1 year ago • 4 comments

Description of the Issue:

When using the methods streamChat, streamGenerateContent, and promptStream, the output is missing spaces between words, causing words to be concatenated incorrectly.

Example:

  1. Expected output: "Hello World"
  2. Actual output: "HelloWorld"

Location of the Issue: File: gemini_implement.dart

Current code:


res = res.trim();

if (index == 0 && res.startsWith("[")) {
  res = res.replaceFirst('[', '');
}
if (res.startsWith(',')) {
  res = res.replaceFirst(',', '');
}
if (res.endsWith(']')) {
  res = res.substring(0, res.length - 1);
}

res = res.trim();

Proposed Fix: Use a separate variable (trimmedRes) to check conditions before modifying res, preventing unintended modifications that could cause words to be concatenated incorrectly:

String trimmedRes = res.trim();

if (index == 0 && trimmedRes.startsWith("[")) {
  res = res.replaceFirst('[', '');
}
if (trimmedRes.startsWith(',')) {
  res = res.replaceFirst(',', '');
}
if (trimmedRes.endsWith(']')) {
  res = res.substring(0, res.length - 1);
}

nhao2003 avatar Mar 06 '25 09:03 nhao2003

Same thing happening over here.

Klerith avatar Mar 27 '25 16:03 Klerith

Same here, this issue unfortunately makes streaming unusable for me.

matthiasn avatar Apr 17 '25 23:04 matthiasn

@nhao2003 I'm using the OpenAI client from openai_dart instead of this library now and it's working just fine, including with streaming, and without weirdly putting words together. Also see https://ai.google.dev/gemini-api/docs/openai. The only thing to note is that the default didn't work and I had set frequencyPenalty: null on the CreateChatCompletionRequest.

matthiasn avatar Apr 19 '25 21:04 matthiasn

I'm experiencing the same problem

Etiene avatar May 03 '25 11:05 Etiene