@@ -63,17 +63,7 @@ async function getResponsePromise(
6363 return enhancedResponse ;
6464 }
6565
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 ) ;
7767 allResponses . push ( value ) ;
7868 }
7969}
@@ -88,17 +78,7 @@ async function* generateResponseSequence(
8878 break ;
8979 }
9080
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 ) ;
10282 const enhancedResponse = createEnhancedContentResponse ( value ) ;
10383 yield enhancedResponse ;
10484 }
@@ -226,3 +206,21 @@ export function aggregateResponses(
226206 }
227207 return aggregatedResponse ;
228208}
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