Skip to content

Commit 4af4286

Browse files
committed
fix: respect content edits for assistant messages with output
1 parent 51fe951 commit 4af4286

2 files changed

Lines changed: 90 additions & 7 deletions

File tree

backend/open_webui/utils/middleware.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
convert_logit_bias_input_to_json,
9898
get_content_from_message,
9999
convert_output_to_messages,
100+
filter_output_by_content,
100101
)
101102
from open_webui.utils.tools import (
102103
get_tools,
@@ -485,9 +486,9 @@ def serialize_output(output: list) -> str:
485486
)
486487

487488
if status == "completed" or duration is not None or not is_last_item:
488-
content = f'{content}<details type="reasoning" done="true" duration="{duration or 0}">\n<summary>Thought for {duration or 0} seconds</summary>\n{display}\n</details>\n'
489+
content = f'{content}<details type="reasoning" done="true" id="{item.get("id", "")}" duration="{duration or 0}">\n<summary>Thought for {duration or 0} seconds</summary>\n{display}\n</details>\n'
489490
else:
490-
content = f'{content}<details type="reasoning" done="false">\n<summary>Thinking…</summary>\n{display}\n</details>\n'
491+
content = f'{content}<details type="reasoning" done="false" id="{item.get("id", "")}">\n<summary>Thinking…</summary>\n{display}\n</details>\n'
491492

492493
elif item_type == "open_webui:code_interpreter":
493494
content_stripped, original_whitespace = split_content_and_whitespace(
@@ -527,9 +528,9 @@ def serialize_output(output: list) -> str:
527528
output_attr = f' output="{html.escape(output_json)}"'
528529

529530
if status == "completed" or duration is not None or not is_last_item:
530-
content += f'<details type="code_interpreter" done="true" duration="{duration or 0}"{output_attr}>\n<summary>Analyzed</summary>\n{display}\n</details>\n'
531+
content += f'<details type="code_interpreter" done="true" id="{item.get("id", "")}" duration="{duration or 0}"{output_attr}>\n<summary>Analyzed</summary>\n{display}\n</details>\n'
531532
else:
532-
content += f'<details type="code_interpreter" done="false"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n'
533+
content += f'<details type="code_interpreter" done="false" id="{item.get("id", "")}"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n'
533534

534535
return content.strip()
535536

@@ -2128,11 +2129,40 @@ def process_messages_with_output(messages: list[dict]) -> list[dict]:
21282129

21292130
for message in messages:
21302131
if message.get("role") == "assistant" and message.get("output"):
2131-
# Use output items for clean OpenAI-format messages
2132-
output_messages = convert_output_to_messages(message["output"], raw=True)
2132+
# Drop output items for <details> blocks removed from content
2133+
output = filter_output_by_content(
2134+
message["output"], message.get("content", "")
2135+
)
2136+
2137+
# Use content for text (respects edits), output for structured items
2138+
content = re.sub(
2139+
r"<details\b[^>]*>.*?</details>", "",
2140+
message.get("content", ""), flags=re.S,
2141+
).strip()
2142+
non_message_items = [
2143+
i for i in output if i.get("type") != "message"
2144+
]
2145+
output_messages = convert_output_to_messages(non_message_items, raw=True)
2146+
21332147
if output_messages:
2148+
# Prepend edited text to first assistant message
2149+
for om in output_messages:
2150+
if om.get("role") == "assistant":
2151+
om["content"] = (
2152+
(content + "\n" + om["content"]).strip()
2153+
if om.get("content")
2154+
else content
2155+
)
2156+
content = ""
2157+
break
2158+
if content:
2159+
output_messages.insert(
2160+
0, {"role": "assistant", "content": content}
2161+
)
21342162
processed.extend(output_messages)
2135-
continue
2163+
elif content:
2164+
processed.append({"role": "assistant", "content": content})
2165+
continue
21362166

21372167
# Strip 'output' field before adding (LLM shouldn't see it)
21382168
clean_message = {k: v for k, v in message.items() if k != "output"}

backend/open_webui/utils/misc.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,59 @@ def flush_pending():
275275
return messages
276276

277277

278+
def filter_output_by_content(output: list, content: str) -> list:
279+
"""
280+
Drop output items whose <details> block was removed from content.
281+
Matches by id attribute for all details-backed types.
282+
For legacy content that lacks id= on a given type, all items of
283+
that type are kept (safe default).
284+
"""
285+
# All IDs present in remaining <details> blocks
286+
present_ids = set(re.findall(r'<details\s[^>]*\bid="([^"]+)"', content))
287+
288+
# Which <details> types exist in content at all
289+
types_present = set(
290+
m.group(1) for m in re.finditer(r'<details\s[^>]*\btype="(\w+)"', content)
291+
)
292+
293+
# Of those, which carry id= attributes (post-fix content).
294+
# Legacy content without id= on a given type → keep all items of that type.
295+
types_with_ids = set()
296+
for m in re.finditer(r'<details\s[^>]*\btype="(\w+)"[^>]*\bid=', content):
297+
types_with_ids.add(m.group(1))
298+
for m in re.finditer(r'<details\s[^>]*\bid=[^>]*\btype="(\w+)"', content):
299+
types_with_ids.add(m.group(1))
300+
301+
# Map output item type → <details> type attribute value
302+
DETAILS_TYPE = {
303+
"function_call": "tool_calls",
304+
"function_call_output": "tool_calls",
305+
"reasoning": "reasoning",
306+
"open_webui:code_interpreter": "code_interpreter",
307+
}
308+
309+
filtered = []
310+
for item in output:
311+
item_type = item.get("type", "")
312+
details_type = DETAILS_TYPE.get(item_type)
313+
314+
if details_type is None:
315+
# Not a details-backed type (e.g. message) — pass through
316+
filtered.append(item)
317+
elif details_type not in types_present:
318+
pass # type completely gone from content — drop
319+
elif details_type not in types_with_ids:
320+
# Legacy content — has <details> but no id= — keep all
321+
filtered.append(item)
322+
else:
323+
item_id = item.get("call_id") or item.get("id", "")
324+
if item_id in present_ids:
325+
filtered.append(item)
326+
# else: user deleted the <details> block — drop
327+
328+
return filtered
329+
330+
278331
def get_last_user_message(messages: list[dict]) -> Optional[str]:
279332
message = get_last_user_message_item(messages)
280333
if message is None:

0 commit comments

Comments
 (0)