@@ -152,7 +152,12 @@ where
152152 continue ;
153153 } ;
154154
155- for part in & choice. content. parts {
155+ let Some ( content) = choice. content. as_ref( ) else {
156+ tracing:: debug!( finish_reason = ?choice. finish_reason, "Streaming candidate missing content" ) ;
157+ continue ;
158+ } ;
159+
160+ for part in & content. parts {
156161 match part {
157162 Part {
158163 part: PartKind :: Text ( text) ,
@@ -165,7 +170,7 @@ where
165170 part: PartKind :: Text ( text) ,
166171 ..
167172 } => {
168- text_response += text;
173+ text_response. push_str ( text) ;
169174 yield Ok ( streaming:: RawStreamingChoice :: Message ( text. clone( ) ) ) ;
170175 } ,
171176 Part {
@@ -186,7 +191,7 @@ where
186191 }
187192 }
188193
189- if choice . content. parts. is_empty( ) {
194+ if content. parts. is_empty( ) {
190195 tracing:: trace!( reason = ?choice. finish_reason, "There is no part in the streaming content" ) ;
191196 }
192197
@@ -252,12 +257,16 @@ mod tests {
252257
253258 let response: StreamGenerateContentResponse = serde_json:: from_value ( json_data) . unwrap ( ) ;
254259 assert_eq ! ( response. candidates. len( ) , 1 ) ;
255- assert_eq ! ( response. candidates[ 0 ] . content. parts. len( ) , 1 ) ;
260+ let content = response. candidates [ 0 ]
261+ . content
262+ . as_ref ( )
263+ . expect ( "candidate should contain content" ) ;
264+ assert_eq ! ( content. parts. len( ) , 1 ) ;
256265
257266 if let Part {
258267 part : PartKind :: Text ( text) ,
259268 ..
260- } = & response . candidates [ 0 ] . content . parts [ 0 ]
269+ } = & content. parts [ 0 ]
261270 {
262271 assert_eq ! ( text, "Hello, world!" ) ;
263272 } else {
@@ -289,14 +298,18 @@ mod tests {
289298
290299 let response: StreamGenerateContentResponse = serde_json:: from_value ( json_data) . unwrap ( ) ;
291300 assert_eq ! ( response. candidates. len( ) , 1 ) ;
292- assert_eq ! ( response. candidates[ 0 ] . content. parts. len( ) , 3 ) ;
301+ let content = response. candidates [ 0 ]
302+ . content
303+ . as_ref ( )
304+ . expect ( "candidate should contain content" ) ;
305+ assert_eq ! ( content. parts. len( ) , 3 ) ;
293306
294307 // Verify all three text parts are present
295308 for ( i, expected_text) in [ "Hello, " , "world!" , " How are you?" ] . iter ( ) . enumerate ( ) {
296309 if let Part {
297310 part : PartKind :: Text ( text) ,
298311 ..
299- } = & response . candidates [ 0 ] . content . parts [ i]
312+ } = & content. parts [ i]
300313 {
301314 assert_eq ! ( text, expected_text) ;
302315 } else {
@@ -337,13 +350,17 @@ mod tests {
337350 } ) ;
338351
339352 let response: StreamGenerateContentResponse = serde_json:: from_value ( json_data) . unwrap ( ) ;
340- assert_eq ! ( response. candidates[ 0 ] . content. parts. len( ) , 2 ) ;
353+ let content = response. candidates [ 0 ]
354+ . content
355+ . as_ref ( )
356+ . expect ( "candidate should contain content" ) ;
357+ assert_eq ! ( content. parts. len( ) , 2 ) ;
341358
342359 // Verify first tool call
343360 if let Part {
344361 part : PartKind :: FunctionCall ( call) ,
345362 ..
346- } = & response . candidates [ 0 ] . content . parts [ 0 ]
363+ } = & content. parts [ 0 ]
347364 {
348365 assert_eq ! ( call. name, "get_weather" ) ;
349366 } else {
@@ -354,7 +371,7 @@ mod tests {
354371 if let Part {
355372 part : PartKind :: FunctionCall ( call) ,
356373 ..
357- } = & response . candidates [ 0 ] . content . parts [ 1 ]
374+ } = & content. parts [ 1 ]
358375 {
359376 assert_eq ! ( call. name, "get_temperature" ) ;
360377 } else {
@@ -399,7 +416,11 @@ mod tests {
399416 } ) ;
400417
401418 let response: StreamGenerateContentResponse = serde_json:: from_value ( json_data) . unwrap ( ) ;
402- let parts = & response. candidates [ 0 ] . content . parts ;
419+ let content = response. candidates [ 0 ]
420+ . content
421+ . as_ref ( )
422+ . expect ( "candidate should contain content" ) ;
423+ let parts = & content. parts ;
403424 assert_eq ! ( parts. len( ) , 4 ) ;
404425
405426 // Verify reasoning (thought) part
@@ -469,7 +490,11 @@ mod tests {
469490 } ) ;
470491
471492 let response: StreamGenerateContentResponse = serde_json:: from_value ( json_data) . unwrap ( ) ;
472- assert_eq ! ( response. candidates[ 0 ] . content. parts. len( ) , 0 ) ;
493+ let content = response. candidates [ 0 ]
494+ . content
495+ . as_ref ( )
496+ . expect ( "candidate should contain content" ) ;
497+ assert_eq ! ( content. parts. len( ) , 0 ) ;
473498 }
474499
475500 #[ test]
0 commit comments