From 4ab831b2596f92fc247c8d788f584ae82d63a88d Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 6 Mar 2026 15:42:13 -0600 Subject: [PATCH] refac --- backend/open_webui/utils/middleware.py | 35 +++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 53d1a54859..27323742a4 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -4163,25 +4163,26 @@ async def streaming_chat_response_handler(response, ctx): 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_args) - except Exception as e: - log.debug(e) - # Fallback to JSON parsing + if tool_args and tool_args.strip(): try: - tool_function_params = json.loads(tool_args) + # json.loads cannot be used because some models do not produce valid JSON + tool_function_params = ast.literal_eval(tool_args) except Exception as e: - log.error( - f"Error parsing tool call arguments: {tool_args}" - ) - results.append( - { - "tool_call_id": tool_call_id, - "content": f"Error: Tool call arguments could not be parsed. The model generated malformed or incomplete JSON for `{tool_function_name}`. Please try again.", - } - ) - continue + log.debug(e) + # Fallback to JSON parsing + try: + tool_function_params = json.loads(tool_args) + except Exception as e: + log.error( + f"Error parsing tool call arguments: {tool_args}" + ) + results.append( + { + "tool_call_id": tool_call_id, + "content": f"Error: Tool call arguments could not be parsed. The model generated malformed or incomplete JSON for `{tool_function_name}`. Please try again.", + } + ) + continue # Ensure arguments are valid JSON for downstream LLM integrations log.debug(