From d612241790d0ab95dd598d2ab779b020ed5eeef2 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 9 Nov 2026 08:05:51 +0900 Subject: [PATCH] refac --- backend/open_webui/utils/ask_user.py | 9 ++++++--- backend/open_webui/utils/misc.py | 6 +++--- backend/open_webui/utils/tool_calling.py | 8 ++++---- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/backend/open_webui/utils/ask_user.py b/backend/open_webui/utils/ask_user.py index 94bc648ef2..fda57119cb 100644 --- a/backend/open_webui/utils/ask_user.py +++ b/backend/open_webui/utils/ask_user.py @@ -93,12 +93,13 @@ def stage_ask_user_tool_calls( for tool_call in ask_user_calls: call_id = tool_call.get('id') or make_output_id('fc') - raw_arguments = tool_call.get('function', {}).get('arguments', '{}') - arguments = raw_arguments + function = tool_call.get('function', {}) + arguments = function.get('arguments', '{}') + original_arguments = function.get('original_arguments', arguments) if not error: try: - parsed_arguments = JSONCodec.loads(raw_arguments or '{}') + parsed_arguments = JSONCodec.loads(arguments or '{}') if not isinstance(parsed_arguments, dict): raise ValueError('ask_user arguments must be an object.') arguments = JSONCodec.dumps(normalize_ask_user_request(parsed_arguments)) @@ -131,6 +132,8 @@ def stage_ask_user_tool_calls( ), None, ) + if arguments != original_arguments: + item['original_arguments'] = (existing_item or {}).get('original_arguments', original_arguments) if existing_item: existing_item.update(item) else: diff --git a/backend/open_webui/utils/misc.py b/backend/open_webui/utils/misc.py index 9801b9eb58..25561c0f5b 100644 --- a/backend/open_webui/utils/misc.py +++ b/backend/open_webui/utils/misc.py @@ -349,14 +349,14 @@ def get_provider_function(item: dict) -> dict: function = dict(function) if 'original_arguments' in function: function['arguments'] = function['original_arguments'] - raw_arguments = function.get('arguments') + original_arguments = function.get('arguments') if error := prepare_tool_call(function): function['arguments'] = JSONCodec.dumps({'error': error}) else: try: # A valid JSON object goes back as the model wrote it, so its cached output still matches. - if isinstance(JSONCodec.loads(raw_arguments), dict): - function['arguments'] = raw_arguments + if isinstance(JSONCodec.loads(original_arguments), dict): + function['arguments'] = original_arguments except (TypeError, ValueError): pass return { diff --git a/backend/open_webui/utils/tool_calling.py b/backend/open_webui/utils/tool_calling.py index 33fd7f52ba..44ea110a81 100644 --- a/backend/open_webui/utils/tool_calling.py +++ b/backend/open_webui/utils/tool_calling.py @@ -293,9 +293,9 @@ def tool_repetition_error(call, previous_calls): def prepare_tool_call(call, schema=None, *, display_labels=False, approval_review=False): """Normalize a call before approval; return an actionable error for the model.""" - raw = call.get('arguments', '{}') + original_arguments = call.get('arguments', '{}') try: - arguments = dict(parse_arguments(raw)) + arguments = dict(parse_arguments(original_arguments)) if display_labels and '__labels__' in arguments: labels = arguments.pop('__labels__') call.pop('display_labels', None) @@ -312,8 +312,8 @@ def prepare_tool_call(call, schema=None, *, display_labels=False, approval_revie except ValueError as exc: return f'Error: {exc} Correct the arguments before retrying.' normalized = json.dumps(arguments, ensure_ascii=False) - if normalized != raw: - call.setdefault('original_arguments', raw) + if normalized != original_arguments: + call.setdefault('original_arguments', original_arguments) call['arguments'] = normalized return None