Skip to content

generator: Fix frontmatter stripping, truncation, and client close - #269

Open
jwren wants to merge 1 commit into
flutter:mainfrom
jwren:gemini-4-follow-up-2
Open

jwren wants to merge 1 commit into
flutter:mainfrom
jwren:gemini-4-follow-up-2

Conversation

@jwren

@jwren jwren commented Nov 19, 2026

Copy link
Copy Markdown
Member

Naive indexOf('---') stripping in GeminiService.cleanContent truncated Markdown horizontal rules, while MAX_TOKENS or frontmatter-only responses bypassed retry checks and overwrote SKILL.md with incomplete or body-less output.

Parse leading YAML frontmatter via package:yaml (including camelCase keys and block scalars), strip 3+/4-backtick outer code fences, reject non-STOP finishReason and empty cleaned bodies inside retry loops, and add close() to release internally owned HTTP clients.

Refs: #267 (Gemini 4 generator follow-up, part 2 of 3)
Tested: dart format, dart analyze --fatal-infos --fatal-warnings, dart test (125 passed)

Naive indexOf('---') stripping truncated Markdown horizontal rules,
while MAX_TOKENS or frontmatter-only responses bypassed retry checks
and overwrote SKILL.md with incomplete or body-less output.

Parse leading YAML frontmatter via package:yaml (including camelCase
keys and block scalars), strip 3+/4-backtick outer fences, reject
non-STOP finishReason and empty cleaned bodies inside retry loops,
and add close() to release internally owned HTTP clients.

Refs: flutter#267 (Gemini 4 generator follow-up)
Tested: dart format, dart analyze --fatal-infos --fatal-warnings, dart test (125 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 improves the Gemini service by adding robust HTTP client lifecycle management, refining the content cleaning logic to better handle YAML frontmatter and markdown fences, and enhancing response validation. I have reviewed the changes and provided feedback on improving null safety in the response extraction logic and ensuring broader exception handling during YAML parsing.

Comment on lines +286 to +311
String _extractVerifiedText(GenerateContentResponse res) {
if (res.candidates.isEmpty) {
throw FormatException(
'No candidates returned from Gemini '
'(blockReason: ${res.promptFeedback?.blockReason}).',
);
}
final candidate = res.candidates.first;
final finishReason = candidate.finishReason;
if (finishReason != Candidate_FinishReason.stop &&
finishReason != Candidate_FinishReason.finishReasonUnspecified) {
throw FormatException(
'Incomplete Gemini response (finishReason: $finishReason).',
);
}
final text = candidate.content?.parts
.where((part) => !part.thought)
.map((part) => part.text)
.where((text) => text != null)
.join('\n');

if (text == null || text.trim().isEmpty) {
throw const FormatException('Empty response from Gemini');
}
return text;
}

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

The current implementation of _extractVerifiedText has potential runtime nullability issues:

  1. res.candidates is a nullable list (List<Candidate>?) in the Gemini API response. Calling res.candidates.isEmpty directly without a null check can throw a NoSuchMethodError at runtime.
  2. candidate.content?.parts is also nullable. If content is non-null but parts is null, calling .where directly on it will throw a NoSuchMethodError because the null-aware chain only short-circuits if the receiver of ?. (i.e., content) is null, not if a subsequent property is null.
  3. Using .where((text) => text != null) keeps the iterable type as Iterable<String?>. Using .whereType<String>() is the idiomatic Dart way to filter out nulls and cast the iterable to non-nullable Strings at the same time.
  String _extractVerifiedText(GenerateContentResponse res) {
    final candidates = res.candidates;
    if (candidates == null || candidates.isEmpty) {
      throw FormatException(
        'No candidates returned from Gemini '
        '(blockReason: ${res.promptFeedback?.blockReason}).',
      );
    }
    final candidate = candidates.first;
    final finishReason = candidate.finishReason;
    if (finishReason != Candidate_FinishReason.stop &&
        finishReason != Candidate_FinishReason.finishReasonUnspecified) {
      throw FormatException(
        'Incomplete Gemini response (finishReason: $finishReason).',
      );
    }
    final parts = candidate.content?.parts;
    if (parts == null) {
      throw const FormatException('Empty response from Gemini');
    }
    final text = parts
        .where((part) => !part.thought)
        .map((part) => part.text)
        .whereType<String>()
        .join('\n');

    if (text.trim().isEmpty) {
      throw const FormatException('Empty response from Gemini');
    }
    return text;
  }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In package:google_cloud_ai_generativelanguage_v1beta (GenerateContentResponse and Content ProtoMessage definitions), both res.candidates (final List<Candidate> candidates) and Content.parts (final List<Part> parts) are non-nullable lists defaulting to const [], and candidate.content?.parts.where(...) short-circuits the full selector chain when candidate.content is null. Adding candidates == null also triggers unnecessary_null_comparison under dart analyze --fatal-infos, so keeping the current null-safe check as-is.

(AI assisted -- human reviewed -- jwren)

Comment on lines +415 to +417
} on YamlException {
return false;
}

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.

medium

Catching only YamlException might allow other unexpected exceptions or errors (such as ArgumentError or StateError from package:yaml's internal parser) to escape and crash the generator tool. Catching any Object ensures robust error handling for any malformed input.

Suggested change
} on YamlException {
return false;
}
} on Object {
return false;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

tool/generator/analysis_options.yaml enables avoid_catching_errors: true to avoid masking Error subclasses in normal predicates. loadYaml(candidateYaml) (with recover: false and no sourceUrl) throws YamlException on malformed YAML input, and the outer generateSkillContent / updateSkillContent methods already guard the top-level generation call, so keeping on YamlException here.

(AI assisted -- human reviewed -- jwren)

@jwren
jwren requested a review from johnpryan November 19, 2026 02:30
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.

1 participant