@@ -436,15 +436,37 @@ def get_response(
436436
437437 # Handle tool calls
438438 if tool_calls and execute_tool_fn :
439+ # Convert tool_calls to a serializable format for all providers
440+ serializable_tool_calls = []
441+ for tc in tool_calls :
442+ if isinstance (tc , dict ):
443+ serializable_tool_calls .append (tc ) # Already a dict
444+ else :
445+ # Convert object to dict
446+ serializable_tool_calls .append ({
447+ "id" : tc .id ,
448+ "type" : getattr (tc , 'type' , "function" ),
449+ "function" : {
450+ "name" : tc .function .name ,
451+ "arguments" : tc .function .arguments
452+ }
453+ })
439454 messages .append ({
440455 "role" : "assistant" ,
441456 "content" : response_text ,
442- "tool_calls" : tool_calls
457+ "tool_calls" : serializable_tool_calls
443458 })
444459
445460 for tool_call in tool_calls :
446- function_name = tool_call ["function" ]["name" ]
447- arguments = json .loads (tool_call ["function" ]["arguments" ])
461+ # Handle both object and dict access patterns
462+ if isinstance (tool_call , dict ):
463+ function_name = tool_call ["function" ]["name" ]
464+ arguments = json .loads (tool_call ["function" ]["arguments" ])
465+ tool_call_id = tool_call ["id" ]
466+ else :
467+ function_name = tool_call .function .name
468+ arguments = json .loads (tool_call .function .arguments )
469+ tool_call_id = tool_call .id
448470
449471 logging .debug (f"[TOOL_EXEC_DEBUG] About to execute tool { function_name } with args: { arguments } " )
450472 tool_result = execute_tool_fn (function_name , arguments )
@@ -462,18 +484,11 @@ def get_response(
462484 logging .debug (f"[TOOL_EXEC_DEBUG] About to display tool call with message: { display_message } " )
463485 display_tool_call (display_message , console = console )
464486
465- messages .append ({
466- "role" : "tool" ,
467- "tool_call_id" : tool_call ["id" ],
468- "content" : json .dumps (tool_result )
469- })
470- else :
471- logging .debug ("[TOOL_EXEC_DEBUG] Verbose mode off, not displaying tool call" )
472- messages .append ({
473- "role" : "tool" ,
474- "tool_call_id" : tool_call ["id" ],
475- "content" : "Function returned an empty output"
476- })
487+ messages .append ({
488+ "role" : "tool" ,
489+ "tool_call_id" : tool_call_id ,
490+ "content" : json .dumps (tool_result ) if tool_result is not None else "Function returned an empty output"
491+ })
477492
478493 # If reasoning_steps is True, do a single non-streaming call
479494 if reasoning_steps :
@@ -930,15 +945,37 @@ async def get_response_async(
930945 tool_calls = tool_response .choices [0 ].message .get ("tool_calls" )
931946
932947 if tool_calls :
948+ # Convert tool_calls to a serializable format for all providers
949+ serializable_tool_calls = []
950+ for tc in tool_calls :
951+ if isinstance (tc , dict ):
952+ serializable_tool_calls .append (tc ) # Already a dict
953+ else :
954+ # Convert object to dict
955+ serializable_tool_calls .append ({
956+ "id" : tc .id ,
957+ "type" : getattr (tc , 'type' , "function" ),
958+ "function" : {
959+ "name" : tc .function .name ,
960+ "arguments" : tc .function .arguments
961+ }
962+ })
933963 messages .append ({
934964 "role" : "assistant" ,
935965 "content" : response_text ,
936- "tool_calls" : tool_calls
966+ "tool_calls" : serializable_tool_calls
937967 })
938968
939969 for tool_call in tool_calls :
940- function_name = tool_call .function .name
941- arguments = json .loads (tool_call .function .arguments )
970+ # Handle both object and dict access patterns
971+ if isinstance (tool_call , dict ):
972+ function_name = tool_call ["function" ]["name" ]
973+ arguments = json .loads (tool_call ["function" ]["arguments" ])
974+ tool_call_id = tool_call ["id" ]
975+ else :
976+ function_name = tool_call .function .name
977+ arguments = json .loads (tool_call .function .arguments )
978+ tool_call_id = tool_call .id
942979
943980 tool_result = await execute_tool_fn (function_name , arguments )
944981
@@ -949,17 +986,11 @@ async def get_response_async(
949986 else :
950987 display_message += "Function returned no output"
951988 display_tool_call (display_message , console = console )
952- messages .append ({
953- "role" : "tool" ,
954- "tool_call_id" : tool_call .id ,
955- "content" : json .dumps (tool_result )
956- })
957- else :
958- messages .append ({
959- "role" : "tool" ,
960- "tool_call_id" : tool_call .id ,
961- "content" : "Function returned an empty output"
962- })
989+ messages .append ({
990+ "role" : "tool" ,
991+ "tool_call_id" : tool_call_id ,
992+ "content" : json .dumps (tool_result ) if tool_result is not None else "Function returned an empty output"
993+ })
963994
964995 # Get response after tool calls
965996 response_text = ""
0 commit comments