Skip to content

Commit 91baa07

Browse files
authored
fix(firebase_ai): add validation for PromptFeedback parsing and handle empty cases (#17753)
1 parent ef9162e commit 91baa07

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

packages/firebase_ai/firebase_ai/lib/src/api.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,12 @@ Candidate _parseCandidate(Object? jsonObject) {
13721372
}
13731373

13741374
PromptFeedback _parsePromptFeedback(Object jsonObject) {
1375+
if (jsonObject is! Map) {
1376+
throw unhandledFormat('PromptFeedback', jsonObject);
1377+
}
1378+
if (jsonObject.isEmpty) {
1379+
return PromptFeedback(null, null, []);
1380+
}
13751381
return switch (jsonObject) {
13761382
{
13771383
'safetyRatings': final List<Object?> safetyRatings,

packages/firebase_ai/firebase_ai/test/response_parsing_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,32 @@ void main() {
6767
);
6868
});
6969

70+
test('with empty promptFeedback', () {
71+
const response = '''
72+
{
73+
"candidates": [
74+
{
75+
"content": {
76+
"parts": [
77+
{
78+
"text": "Mountain View, California, United States"
79+
}
80+
],
81+
"role": "model"
82+
},
83+
"index": 0
84+
}
85+
],
86+
"promptFeedback": {}
87+
}
88+
''';
89+
final decoded = jsonDecode(response) as Object;
90+
expect(
91+
VertexSerialization().parseGenerateContentResponse(decoded),
92+
isA<GenerateContentResponse>(),
93+
);
94+
});
95+
7096
test('with a blocked prompt', () {
7197
const response = '''
7298
{

0 commit comments

Comments
 (0)