1- # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
1+ # ========= Copyright 2023-2025 @ CAMEL-AI.org. All Rights Reserved. =========
22# Licensed under the Apache License, Version 2.0 (the "License");
33# you may not use this file except in compliance with the License.
44# You may obtain a copy of the License at
1010# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111# See the License for the specific language governing permissions and
1212# limitations under the License.
13- # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
13+ # ========= Copyright 2023-2025 @ CAMEL-AI.org. All Rights Reserved. =========
1414import json
1515import os
1616import re
@@ -279,25 +279,25 @@ def _format_model_turn(self, message: OpenAIMessage) -> str:
279279 str: Formatted model turn.
280280 """
281281 content = message .get ("content" , "" ) or ""
282- tool_calls = message .get ("tool_calls" , [] )
282+ tool_calls = message .get ("tool_calls" )
283283
284284 result = f"<start_of_turn>model\n { content } "
285285
286- if tool_calls :
286+ if tool_calls and isinstance ( tool_calls , list ) :
287287 for tool_call in tool_calls :
288288 func = tool_call .get ("function" , {})
289289 func_name = func .get ("name" , "" )
290- args = func .get ("arguments" , "{}" )
291- if isinstance (args , str ):
292- args = json .loads (args )
290+ args_raw = func .get ("arguments" , "{}" )
291+ if isinstance (args_raw , str ):
292+ args : Dict [str , Any ] = json .loads (args_raw )
293+ else :
294+ args = dict (args_raw ) if args_raw else {}
293295
294296 # format arguments
295297 arg_parts = []
296298 for key , value in sorted (args .items ()):
297299 if isinstance (value , str ):
298- arg_parts .append (
299- f"{ key } :{ self ._escape_string (value )} "
300- )
300+ arg_parts .append (f"{ key } :{ self ._escape_string (value )} " )
301301 else :
302302 arg_parts .append (f"{ key } :{ json .dumps (value )} " )
303303
@@ -324,6 +324,8 @@ def _format_tool_response(self, message: OpenAIMessage) -> str:
324324
325325 # try to parse content as json for structured response
326326 try :
327+ if not isinstance (content , str ):
328+ content = str (content ) if content else ""
327329 result_data = json .loads (content )
328330 # check if it's a dict (structured response)
329331 if isinstance (result_data , dict ):
@@ -552,7 +554,7 @@ def _parse_function_args(self, args_str: str) -> Dict[str, Any]:
552554 Returns:
553555 Dict[str, Any]: Parsed arguments dictionary.
554556 """
555- args = {}
557+ args : Dict [ str , Any ] = {}
556558 if not args_str :
557559 return args
558560
@@ -567,7 +569,7 @@ def _parse_function_args(self, args_str: str) -> Dict[str, Any]:
567569 char = args_str [i ]
568570
569571 # check for <escape> tag
570- if args_str [i : i + 8 ] == "<escape>" :
572+ if args_str [i : i + 8 ] == "<escape>" :
571573 in_escape = not in_escape
572574 i += 8
573575 continue
@@ -627,7 +629,7 @@ def _parse_value(self, value: str) -> Any:
627629 # return as string
628630 return value
629631
630- def _to_chat_completion (
632+ def _to_chat_completion ( # type: ignore[override]
631633 self ,
632634 response_text : str ,
633635 model : str ,
0 commit comments