@@ -63,17 +63,7 @@ async function getResponsePromise(
63
63
return enhancedResponse ;
64
64
}
65
65
66
- // The backend can send empty text parts, but if they are sent back (e.g. in a chat history) there
67
- // will be an error. To prevent this, filter out the empty text part from responses.
68
- if ( value . candidates && value . candidates . length > 0 ) {
69
- value . candidates . forEach ( candidate => {
70
- if ( candidate . content ) {
71
- candidate . content . parts = candidate . content . parts . filter (
72
- part => part . text !== ''
73
- ) ;
74
- }
75
- } ) ;
76
- }
66
+ deleteEmptyTextParts ( value ) ;
77
67
allResponses . push ( value ) ;
78
68
}
79
69
}
@@ -88,17 +78,7 @@ async function* generateResponseSequence(
88
78
break ;
89
79
}
90
80
91
- // The backend can send empty text parts, but if they are sent back (e.g. in a chat history) there
92
- // will be an error. To prevent this, filter out the empty text part from responses.
93
- if ( value . candidates && value . candidates . length > 0 ) {
94
- value . candidates . forEach ( candidate => {
95
- if ( candidate . content ) {
96
- candidate . content . parts = candidate . content . parts . filter (
97
- part => part . text !== ''
98
- ) ;
99
- }
100
- } ) ;
101
- }
81
+ deleteEmptyTextParts ( value ) ;
102
82
const enhancedResponse = createEnhancedContentResponse ( value ) ;
103
83
yield enhancedResponse ;
104
84
}
@@ -226,3 +206,21 @@ export function aggregateResponses(
226
206
}
227
207
return aggregatedResponse ;
228
208
}
209
+
210
+ /**
211
+ * The backend can send empty text parts, but if they are sent back (e.g. in a chat history) there
212
+ * will be an error. To prevent this, filter out the empty text part from responses.
213
+ *
214
+ * See: https://github.com/firebase/firebase-js-sdk/issues/8714
215
+ */
216
+ export function deleteEmptyTextParts ( response : GenerateContentResponse ) : void {
217
+ if ( response . candidates ) {
218
+ response . candidates . forEach ( candidate => {
219
+ if ( candidate . content && candidate . content . parts ) {
220
+ candidate . content . parts = candidate . content . parts . filter (
221
+ part => part . text !== ''
222
+ ) ;
223
+ }
224
+ } ) ;
225
+ }
226
+ }
0 commit comments