Skip to content

fix: filter empty messages without altering text#975

Open
corvo-007 wants to merge 1 commit into
flutter:mainfrom
corvo-007:ai-text-concatenation-fix
Open

fix: filter empty messages without altering text#975
corvo-007 wants to merge 1 commit into
flutter:mainfrom
corvo-007:ai-text-concatenation-fix

Conversation

@corvo-007

Copy link
Copy Markdown

Description

Modify A2uiTransportAdapter to preserve whitespaces in AI text responses while still discarding empty text parts.
Trimming all messages causes inconsistent concatenation issues.

Fixes #906

BEFORE:
Screenshot From 2026-06-17 03-54-35

AFTER:
Screenshot From 2026-06-17 03-58-48

Pre-launch Checklist

  • I read the [Flutter Style Guide] recently, and have followed its advice.
  • I signed the [CLA].
  • I read the [Contributors Guide].
  • I have added sample code updates to the [changelog].
  • I updated/added relevant documentation (doc comments with ///).
  • If my PR is a fork PR, I've checked that [e2e tests] passed.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies the incomingText stream in A2uiTransportAdapter to map the raw text without trimming, but filters out strings that are empty after trimming. The reviewer correctly points out that using text.trim().isNotEmpty will discard chunks consisting entirely of whitespace (such as spaces or newlines), which are common in streaming LLM responses and necessary to prevent words from running together. The reviewer suggests using text.isNotEmpty instead to preserve these whitespaces.

Comment on lines +68 to +69
.map((e) => e.text)
.where((text) => text.trim().isNotEmpty);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using text.trim().isNotEmpty will discard chunks that consist entirely of whitespace, such as a single space (" ") or a newline ("\n"). In streaming LLM responses, it is extremely common for whitespace or newlines to be emitted as separate chunks. Discarding them will cause words to run together (e.g., producing "Helloworld" instead of "Hello world").

To preserve all valid whitespaces while filtering out truly empty strings, you should use text.isNotEmpty instead of text.trim().isNotEmpty.

Suggested change
.map((e) => e.text)
.where((text) => text.trim().isNotEmpty);
.map((e) => e.text)
.where((text) => text.isNotEmpty);

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

Successfully merging this pull request may close these issues.

Simple chat: error concatenating response.

1 participant