Skip to content

Commit 3016234

Browse files
committed
fix: drop output for removed tool call blocks
1 parent 412e391 commit 3016234

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

backend/open_webui/routers/chats.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from fastapi.responses import StreamingResponse
77

88

9-
from open_webui.utils.misc import get_message_list
9+
from open_webui.utils.misc import get_message_list, filter_output_by_content
1010
from open_webui.socket.main import get_event_emitter
1111
from open_webui.models.chats import (
1212
ChatForm,
@@ -1031,12 +1031,21 @@ async def update_chat_message_by_id(
10311031
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
10321032
)
10331033

1034+
existing_message = (
1035+
chat.chat.get("history", {}).get("messages", {}).get(message_id, {})
1036+
)
1037+
existing_output = existing_message.get("output")
1038+
1039+
update_data = {"content": form_data.content}
1040+
if existing_output is not None:
1041+
update_data["output"] = filter_output_by_content(
1042+
existing_output, form_data.content
1043+
)
1044+
10341045
chat = Chats.upsert_message_to_chat_by_id_and_message_id(
10351046
id,
10361047
message_id,
1037-
{
1038-
"content": form_data.content,
1039-
},
1048+
update_data,
10401049
db=db,
10411050
)
10421051

backend/open_webui/utils/misc.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,22 @@ def flush_pending():
262262
return messages
263263

264264

265+
def filter_output_by_content(output: list, content: str) -> list:
266+
"""
267+
Drop function_call and function_call_output items from output for any
268+
tool call block removed from the message content.
269+
"""
270+
present_call_ids = set(
271+
re.findall(r'<details\s[^>]*\btype="tool_calls"[^>]*\bid="([^"]+)"', content)
272+
)
273+
return [
274+
item
275+
for item in output
276+
if item.get("type") not in ("function_call", "function_call_output")
277+
or item.get("call_id", "") in present_call_ids
278+
]
279+
280+
265281
def get_last_user_message(messages: list[dict]) -> Optional[str]:
266282
message = get_last_user_message_item(messages)
267283
if message is None:

0 commit comments

Comments
 (0)