From d727ee4d1febb2b72d5f6c26668c562eadca54f1 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 27 Jul 2026 02:38:33 -0400 Subject: [PATCH] refac --- backend/open_webui/utils/tools.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/backend/open_webui/utils/tools.py b/backend/open_webui/utils/tools.py index 1930837af8..e871f41583 100644 --- a/backend/open_webui/utils/tools.py +++ b/backend/open_webui/utils/tools.py @@ -8,7 +8,7 @@ import json import logging import os import re -from functools import partial, update_wrapper +from functools import cache, partial, update_wrapper from typing import ( Any, Awaitable, @@ -781,10 +781,7 @@ async def get_builtin_tools( }, ) - # Generate spec from function - pydantic_model = convert_function_to_pydantic_model(func) - spec = convert_pydantic_model_to_openai_function_spec(pydantic_model) - spec = clean_openai_tool_schema(spec) + spec = get_builtin_tool_spec(func) if func.__name__ == 'delegate_task' and not config.get('subagents.background_enabled'): parameters = spec.get('parameters', {}) parameters.get('properties', {}).pop('background', None) @@ -935,6 +932,17 @@ def clean_openai_tool_schema(spec: dict) -> dict: return cleaned_spec +@cache +def build_builtin_tool_spec(func: Callable) -> dict: + pydantic_model = convert_function_to_pydantic_model(func) + spec = convert_pydantic_model_to_openai_function_spec(pydantic_model) + return clean_openai_tool_schema(spec) + + +def get_builtin_tool_spec(func: Callable) -> dict: + return copy.deepcopy(build_builtin_tool_spec(func)) + + def get_functions_from_tool(tool: object) -> list[Callable]: return [ getattr(tool, func)