@@ -88,10 +88,7 @@ class ChatCompletionResponse(BaseModel):
8888 )
8989 model : str = Field (..., description = "The model used for the chat completion." )
9090 choices : list [Choice ] = Field (..., description = "A list of chat completion choices." )
91- context : list [dict ] | None = Field (
92- None ,
93- description = "Full conversation context including tool calls and results." ,
94- )
91+
9592 usage : dict | None = Field (
9693 None , description = "Usage statistics for the completion request."
9794 )
@@ -336,35 +333,35 @@ async def event_generator():
336333
337334 async for event in handler .stream_events ():
338335 if isinstance (event , ToolCallEvent ):
339- for tc in event .tool_calls :
340- tool_calls_delta = [
336+ tool_calls_delta = [
337+ {
338+ "index" : i ,
339+ "id" : getattr (tc , "tool_id" , "" ),
340+ "type" : "function" ,
341+ "function" : {
342+ "name" : tc .tool_name ,
343+ "arguments" : json .dumps (tc .tool_kwargs ),
344+ },
345+ }
346+ for i , tc in enumerate (event .tool_calls )
347+ ]
348+ data = {
349+ "id" : completion_id ,
350+ "object" : "chat.completion.chunk" ,
351+ "created" : created ,
352+ "model" : model_id ,
353+ "choices" : [
341354 {
342355 "index" : 0 ,
343- "id" : getattr (tc , "tool_id" , "" ),
344- "type" : "function" ,
345- "function" : {
346- "name" : tc .tool_name ,
347- "arguments" : json .dumps (tc .tool_kwargs ),
356+ "delta" : {
357+ "role" : "assistant" ,
358+ "tool_calls" : tool_calls_delta ,
348359 },
360+ "finish_reason" : None ,
349361 }
350- ]
351- data = {
352- "id" : completion_id ,
353- "object" : "chat.completion.chunk" ,
354- "created" : created ,
355- "model" : model_id ,
356- "choices" : [
357- {
358- "index" : 0 ,
359- "delta" : {
360- "role" : "assistant" ,
361- "tool_calls" : tool_calls_delta ,
362- },
363- "finish_reason" : None ,
364- }
365- ],
366- }
367- yield f"data: { json .dumps (data )} \n \n "
362+ ],
363+ }
364+ yield f"data: { json .dumps (data )} \n \n "
368365
369366 elif isinstance (event , InputEvent ):
370367 if event .input :
0 commit comments