From c205d3a35378b6f7784b62408e45e7c58c014177 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 16 Jun 2026 23:48:01 +0200 Subject: [PATCH] refac --- backend/open_webui/utils/middleware.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 576afe3b24..a0eb1ad8e0 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -2617,7 +2617,7 @@ async def process_chat_payload(request, form_data, user, metadata, model): # If the original caller provided tools, use them as-is (skip resolution). # Otherwise, save any tools that filter inlets added for merging later. - inlet_filter_tools = None if payload_tools else form_data.get('tools', None) + inlet_filter_tools = None if payload_tools is not None else form_data.get('tools', None) # Skills — extract IDs from message content (<$skillId|label> tags) so # persisted chats work without relying on the frontend to send skill_ids. @@ -2704,10 +2704,10 @@ async def process_chat_payload(request, form_data, user, metadata, model): } form_data['metadata'] = metadata - # When the caller provides an explicit OpenAI-style `tools` array in the - # request body, skip all server-side tool resolution and pass the caller's - # tools through to the model unchanged. - if not payload_tools: + # When the caller provides an explicit `tools` key in the request body, + # skip all server-side tool resolution and pass the caller's tools through + # unchanged. Sending `tools: []` explicitly opts out of builtin injection. + if payload_tools is None: # Server side tools tool_ids = metadata.get('tool_ids', None) # Client side tools @@ -2838,12 +2838,14 @@ async def process_chat_payload(request, form_data, user, metadata, model): if mcp_clients: metadata['mcp_clients'] = mcp_clients - # Inject builtin tools for native function calling based on enabled features and model capability - # Check if builtin_tools capability is enabled for this model (defaults to True if not specified) + # Inject builtin tools for native function calling based on enabled features and model capability. + # Only inject when the request originates from the UI (identified by session_id). + # API callers don't expect hidden tools; they can explicitly request tools via tool_ids. builtin_tools_enabled = (model.get('info', {}).get('meta', {}).get('capabilities') or {}).get( 'builtin_tools', True ) - if metadata.get('params', {}).get('function_calling') != 'legacy' and builtin_tools_enabled: + has_session = bool(metadata.get('session_id')) + if has_session and metadata.get('params', {}).get('function_calling') != 'legacy' and builtin_tools_enabled: # Add file context to user messages chat_id = metadata.get('chat_id') form_data['messages'] = await add_file_context(form_data.get('messages', []), chat_id, user)