@@ -422,7 +422,7 @@ async def stream(self, request: dict[str, Any]) -> AsyncGenerator[dict[str, Any]
422422 yield {"chunk_type" : "message_start" }
423423
424424 content_started = False
425- current_tool_calls : dict [str , dict [ str , str ]] = {}
425+ tool_calls : dict [str , list [ Any ]] = {}
426426 accumulated_text = ""
427427
428428 async for chunk in stream_response :
@@ -443,24 +443,23 @@ async def stream(self, request: dict[str, Any]) -> AsyncGenerator[dict[str, Any]
443443 if hasattr (delta , "tool_calls" ) and delta .tool_calls :
444444 for tool_call in delta .tool_calls :
445445 tool_id = tool_call .id
446+ tool_calls .setdefault (tool_id , []).append (tool_call )
446447
447- if tool_id not in current_tool_calls :
448- yield {"chunk_type" : "content_start" , "data_type" : "tool" , "data" : tool_call }
449- current_tool_calls [tool_id ] = {"name" : tool_call .function .name , "arguments" : "" }
448+ if hasattr (choice , "finish_reason" ) and choice .finish_reason :
449+ if content_started :
450+ yield {"chunk_type" : "content_stop" , "data_type" : "text" }
451+
452+ for tool_deltas in tool_calls .values ():
453+ yield {"chunk_type" : "content_start" , "data_type" : "tool" , "data" : tool_deltas [0 ]}
450454
451- if hasattr ( tool_call . function , "arguments" ) :
452- current_tool_calls [ tool_id ][ "arguments" ] += tool_call . function . arguments
455+ for tool_delta in tool_deltas :
456+ if hasattr ( tool_delta . function , "arguments" ):
453457 yield {
454458 "chunk_type" : "content_delta" ,
455459 "data_type" : "tool" ,
456- "data" : tool_call .function .arguments ,
460+ "data" : tool_delta .function .arguments ,
457461 }
458462
459- if hasattr (choice , "finish_reason" ) and choice .finish_reason :
460- if content_started :
461- yield {"chunk_type" : "content_stop" , "data_type" : "text" }
462-
463- for _ in current_tool_calls :
464463 yield {"chunk_type" : "content_stop" , "data_type" : "tool" }
465464
466465 yield {"chunk_type" : "message_stop" , "data" : choice .finish_reason }
0 commit comments