@@ -181,10 +181,13 @@ impl DeltaAggregator {
181181 . collect ( ) ;
182182
183183 // Initialize and push the converted tool calls to state_choice.tool_calls
184- if let Some ( existing_tool_calls) = & mut state_choice. tool_calls {
185- existing_tool_calls. extend ( converted_tool_calls) ;
186- } else {
187- state_choice. tool_calls = Some ( converted_tool_calls) ;
184+ // Only set tool_calls to Some if there are actual tool calls
185+ if !converted_tool_calls. is_empty ( ) {
186+ if let Some ( existing_tool_calls) = & mut state_choice. tool_calls {
187+ existing_tool_calls. extend ( converted_tool_calls) ;
188+ } else {
189+ state_choice. tool_calls = Some ( converted_tool_calls) ;
190+ }
188191 }
189192 }
190193
@@ -358,7 +361,7 @@ mod tests {
358361 tool_calls. map ( |tool_calls| serde_json:: from_str ( tool_calls) . unwrap ( ) ) ;
359362
360363 let tool_call_chunks = if let Some ( tool_calls) = tool_calls {
361- vec ! [
364+ Some ( vec ! [
362365 dynamo_async_openai:: types:: ChatCompletionMessageToolCallChunk {
363366 index: 0 ,
364367 id: Some ( "test_id" . to_string( ) ) ,
@@ -368,22 +371,15 @@ mod tests {
368371 arguments: Some ( serde_json:: to_string( & tool_calls[ "arguments" ] ) . unwrap( ) ) ,
369372 } ) ,
370373 } ,
371- ]
374+ ] )
372375 } else {
373- vec ! [
374- dynamo_async_openai:: types:: ChatCompletionMessageToolCallChunk {
375- index: 0 ,
376- id: None ,
377- r#type: None ,
378- function: None ,
379- } ,
380- ]
376+ None
381377 } ;
382378
383379 let delta = dynamo_async_openai:: types:: ChatCompletionStreamResponseDelta {
384380 content : Some ( text. to_string ( ) ) ,
385381 function_call : None ,
386- tool_calls : Some ( tool_call_chunks) ,
382+ tool_calls : tool_call_chunks,
387383 role,
388384 refusal : None ,
389385 reasoning_content : None ,
0 commit comments