@@ -62,6 +62,16 @@ async function getResponsePromise(
62
62
) ;
63
63
return enhancedResponse ;
64
64
}
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 ( part => part . text !== '' ) ;
72
+ }
73
+ } ) ;
74
+ }
65
75
allResponses . push ( value ) ;
66
76
}
67
77
}
@@ -76,6 +86,15 @@ async function* generateResponseSequence(
76
86
break ;
77
87
}
78
88
89
+ // The backend can send empty text parts, but if they are sent back (e.g. in a chat history) there
90
+ // will be an error. To prevent this, filter out the empty text part from responses.
91
+ if ( value . candidates && value . candidates . length > 0 ) {
92
+ value . candidates . forEach ( candidate => {
93
+ if ( candidate . content ) {
94
+ candidate . content . parts = candidate . content . parts . filter ( part => part . text !== '' ) ;
95
+ }
96
+ } ) ;
97
+ }
79
98
const enhancedResponse = createEnhancedContentResponse ( value ) ;
80
99
yield enhancedResponse ;
81
100
}
@@ -203,3 +222,13 @@ export function aggregateResponses(
203
222
}
204
223
return aggregatedResponse ;
205
224
}
225
+
226
+ /**
227
+ * The backend can send empty text parts, but if they are sent back (e.g. in a chat history) there
228
+ * will be an error. To prevent this, filter out the empty text part from responses.
229
+ *
230
+ * @internal
231
+ */
232
+ export function filterEmptyTextParts ( parts : Part [ ] ) : Part [ ] {
233
+ return parts ?. filter ( part => part . text !== '' ) ;
234
+ }
0 commit comments