@@ -322,6 +322,7 @@ def append_or_extend(
322322 contents .append (content_cls (parts = part ))
323323
324324 gemini_contents : list [types .Content ] = []
325+ tool_name_by_call_id : dict [str , str ] = {}
325326 for message in payloads ["messages" ]:
326327 role , content = message ["role" ], message .get ("content" )
327328
@@ -392,6 +393,11 @@ def append_or_extend(
392393
393394 if "tool_calls" in message :
394395 for tool in message ["tool_calls" ]:
396+ tool_call_id = tool .get ("id" )
397+ if isinstance (tool_call_id , str ) and tool_call_id :
398+ tool_name_by_call_id [tool_call_id ] = tool ["function" ][
399+ "name"
400+ ]
395401 part = types .Part .from_function_call (
396402 name = tool ["function" ]["name" ],
397403 args = json .loads (tool ["function" ]["arguments" ]),
@@ -415,7 +421,12 @@ def append_or_extend(
415421 append_or_extend (gemini_contents , parts , types .ModelContent )
416422
417423 elif role == "tool" :
418- func_name = message .get ("name" , message ["tool_call_id" ])
424+ tool_call_id = message ["tool_call_id" ]
425+ func_name = (
426+ message .get ("name" )
427+ or tool_name_by_call_id .get (tool_call_id )
428+ or tool_call_id
429+ )
419430 part = types .Part .from_function_response (
420431 name = func_name ,
421432 response = {
@@ -547,8 +558,13 @@ def _process_content_parts(
547558 llm_response .role = "tool"
548559 llm_response .tools_call_name .append (part .function_call .name )
549560 llm_response .tools_call_args .append (part .function_call .args )
550- # function_call.id might be None, use name as fallback
551- tool_call_id = part .function_call .id or part .function_call .name
561+ # function_call.id might be None, use a unique name-based fallback.
562+ base_tool_call_id = part .function_call .id or part .function_call .name
563+ tool_call_id = base_tool_call_id
564+ duplicate_index = 2
565+ while tool_call_id in llm_response .tools_call_ids :
566+ tool_call_id = f"{ base_tool_call_id } __astrbot_{ duplicate_index } "
567+ duplicate_index += 1
552568 llm_response .tools_call_ids .append (tool_call_id )
553569 # extra_content
554570 if part .thought_signature :
0 commit comments