@@ -181,10 +181,13 @@ impl DeltaAggregator {
181
181
. collect ( ) ;
182
182
183
183
// 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
+ }
188
191
}
189
192
}
190
193
@@ -358,7 +361,7 @@ mod tests {
358
361
tool_calls. map ( |tool_calls| serde_json:: from_str ( tool_calls) . unwrap ( ) ) ;
359
362
360
363
let tool_call_chunks = if let Some ( tool_calls) = tool_calls {
361
- vec ! [
364
+ Some ( vec ! [
362
365
dynamo_async_openai:: types:: ChatCompletionMessageToolCallChunk {
363
366
index: 0 ,
364
367
id: Some ( "test_id" . to_string( ) ) ,
@@ -368,22 +371,15 @@ mod tests {
368
371
arguments: Some ( serde_json:: to_string( & tool_calls[ "arguments" ] ) . unwrap( ) ) ,
369
372
} ) ,
370
373
} ,
371
- ]
374
+ ] )
372
375
} 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
381
377
} ;
382
378
383
379
let delta = dynamo_async_openai:: types:: ChatCompletionStreamResponseDelta {
384
380
content : Some ( text. to_string ( ) ) ,
385
381
function_call : None ,
386
- tool_calls : Some ( tool_call_chunks) ,
382
+ tool_calls : tool_call_chunks,
387
383
role,
388
384
refusal : None ,
389
385
reasoning_content : None ,
0 commit comments