From 6a5aac43dfb682d6ce713de0a3d8a291b2881132 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 16 Jun 2025 15:29:40 +0400 Subject: [PATCH] refac --- backend/open_webui/utils/middleware.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index ce3bfd041..1c906d7b3 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -2065,33 +2065,33 @@ async def process_chat_response( for tool_call in response_tool_calls: tool_call_id = tool_call.get("id", "") tool_name = tool_call.get("function", {}).get("name", "") - tool_call_args = tool_call.get("function", {}).get("arguments", "{}") + tool_args = tool_call.get("function", {}).get("arguments", "{}") tool_function_params = {} try: # json.loads cannot be used because some models do not produce valid JSON - tool_function_params = ast.literal_eval( - tool_call_args - ) + tool_function_params = ast.literal_eval(tool_args) except Exception as e: log.debug(e) # Fallback to JSON parsing try: - tool_function_params = json.loads( - tool_call_args - ) + tool_function_params = json.loads(tool_args) except Exception as e: log.error( - f"Error parsing tool call arguments: {tool_call_args}" + f"Error parsing tool call arguments: {tool_args}" ) # Mutate the original tool call response params as they are passed back to the passed - # back to the LLM via the content blocks. If they are in a json block and are invalid json, + # back to the LLM via the content blocks. If they are in a json block and are invalid json, # this can cause downstream LLM integrations to fail (e.g. bedrock gateway) where response # params are not valid json. # Main case so far is no args = "" = invalid json. - log.debug(f"Parsed args from {tool_call_args} to {tool_function_params}") - tool_call.setdefault("function", {})["arguments"] = json.dumps(tool_function_params) + log.debug( + f"Parsed args from {tool_args} to {tool_function_params}" + ) + tool_call.setdefault("function", {})["arguments"] = json.dumps( + tool_function_params + ) tool_result = None