From e160769e2f86bf65b99a1c5bebd1f8a9132704a6 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Thu, 3 Dec 2026 00:20:46 +0100 Subject: [PATCH] fix: very large system prompts can freeze the server (#35560) A system prompt with many repeated tool approval policy or tool list markers and no closing marker took time growing with the square of its size to clean up, tens of seconds or more for half a megabyte. The tool approval cleanup runs on every chat request and blocks the server while it runs, so one oversized request stalled every user on that server process. Both cleanups now ignore the text after the last closing marker, which can never be removed anyway, so the same input takes under a millisecond and the cleaned prompt is the same as before. --- backend/open_webui/utils/tool_calling.py | 6 +++++- backend/open_webui/utils/tool_search.py | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/backend/open_webui/utils/tool_calling.py b/backend/open_webui/utils/tool_calling.py index 7a7bb9d6f1..5001a52baf 100644 --- a/backend/open_webui/utils/tool_calling.py +++ b/backend/open_webui/utils/tool_calling.py @@ -90,7 +90,11 @@ _TOOL_APPROVAL_PROMPT_RE = re.compile(r'\n?\n.*?') + return _TOOL_APPROVAL_PROMPT_RE.sub('', head + tag) + tail def set_tool_call_metadata(item, tools): diff --git a/backend/open_webui/utils/tool_search.py b/backend/open_webui/utils/tool_search.py index 3edbc5126a..8526fac27a 100644 --- a/backend/open_webui/utils/tool_search.py +++ b/backend/open_webui/utils/tool_search.py @@ -239,7 +239,10 @@ def resolve_tool_call(tool_call: dict, tools: dict[str, dict]) -> str | None: def strip_deferred_tools_manifest(system_prompt: str | None) -> str | None: """Remove the manifest from a system prompt that is reused by a request which builds its own.""" - return _MANIFEST_RE.sub('', system_prompt) if system_prompt else system_prompt + if not system_prompt: + return system_prompt + head, tag, tail = system_prompt.rpartition('') + return _MANIFEST_RE.sub('', head + tag) + tail # Letters, digits and combining marks, so vowel signs in scripts like Devanagari stay attached to their word.