Repository navigation
Conversation
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)
There was a problem hiding this comment.
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.
| 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; | ||
| } |
There was a problem hiding this comment.
The current implementation of _extractVerifiedText has potential runtime nullability issues:
res.candidatesis a nullable list (List<Candidate>?) in the Gemini API response. Callingres.candidates.isEmptydirectly without a null check can throw aNoSuchMethodErrorat runtime.candidate.content?.partsis also nullable. Ifcontentis non-null butpartsis null, calling.wheredirectly on it will throw aNoSuchMethodErrorbecause the null-aware chain only short-circuits if the receiver of?.(i.e.,content) is null, not if a subsequent property is null.- Using
.where((text) => text != null)keeps the iterable type asIterable<String?>. Using.whereType<String>()is the idiomatic Dart way to filter out nulls and cast the iterable to non-nullableStrings 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;
}There was a problem hiding this comment.
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)
| } on YamlException { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
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.
| } on YamlException { | |
| return false; | |
| } | |
| } on Object { | |
| return false; | |
| } |
There was a problem hiding this comment.
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)
Naive
indexOf('---')stripping inGeminiService.cleanContenttruncated Markdown horizontal rules, whileMAX_TOKENSor frontmatter-only responses bypassed retry checks and overwroteSKILL.mdwith 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-STOPfinishReasonand empty cleaned bodies inside retry loops, and addclose()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)