Skip to content

Commit a6fbdf7

Browse files
committed
fix: respect content edits for assistant messages with output
1 parent 6da7dd1 commit a6fbdf7

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
@@ -95,6 +95,7 @@
9595
convert_logit_bias_input_to_json,
9696
get_content_from_message,
9797
convert_output_to_messages,
98+
filter_output_by_content,
9899
)
99100
from open_webui.utils.tools import (
100101
get_tools,
@@ -418,9 +419,9 @@ def serialize_output(output: list) -> str:
418419
)
419420

420421
if status == "completed" or duration is not None or not is_last_item:
421-
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'
422+
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'
422423
else:
423-
content = f'{content}<details type="reasoning" done="false">\n<summary>Thinking…</summary>\n{display}\n</details>\n'
424+
content = f'{content}<details type="reasoning" done="false" id="{item.get("id", "")}">\n<summary>Thinking…</summary>\n{display}\n</details>\n'
424425

425426
elif item_type == "open_webui:code_interpreter":
426427
content_stripped, original_whitespace = split_content_and_whitespace(
@@ -460,9 +461,9 @@ def serialize_output(output: list) -> str:
460461
output_attr = f' output="{html.escape(output_json)}"'
461462

462463
if status == "completed" or duration is not None or not is_last_item:
463-
content += f'<details type="code_interpreter" done="true" duration="{duration or 0}"{output_attr}>\n<summary>Analyzed</summary>\n{display}\n</details>\n'
464+
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'
464465
else:
465-
content += f'<details type="code_interpreter" done="false"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n'
466+
content += f'<details type="code_interpreter" done="false" id="{item.get("id", "")}"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n'
466467

467468
return content.strip()
468469

@@ -1977,11 +1978,40 @@ def process_messages_with_output(messages: list[dict]) -> list[dict]:
19771978

19781979
for message in messages:
19791980
if message.get("role") == "assistant" and message.get("output"):
1980-
# Use output items for clean OpenAI-format messages
1981-
output_messages = convert_output_to_messages(message["output"], raw=True)
1981+
# Drop output items for <details> blocks removed from content
1982+
output = filter_output_by_content(
1983+
message["output"], message.get("content", "")
1984+
)
1985+
1986+
# Use content for text (respects edits), output for structured items
1987+
content = re.sub(
1988+
r"<details\b[^>]*>.*?</details>", "",
1989+
message.get("content", ""), flags=re.S,
1990+
).strip()
1991+
non_message_items = [
1992+
i for i in output if i.get("type") != "message"
1993+
]
1994+
output_messages = convert_output_to_messages(non_message_items, raw=True)
1995+
19821996
if output_messages:
1997+
# Prepend edited text to first assistant message
1998+
for om in output_messages:
1999+
if om.get("role") == "assistant":
2000+
om["content"] = (
2001+
(content + "\n" + om["content"]).strip()
2002+
if om.get("content")
2003+
else content
2004+
)
2005+
content = ""
2006+
break
2007+
if content:
2008+
output_messages.insert(
2009+
0, {"role": "assistant", "content": content}
2010+
)
19832011
processed.extend(output_messages)
1984-
continue
2012+
elif content:
2013+
processed.append({"role": "assistant", "content": content})
2014+
continue
19852015

19862016
# Strip 'output' field before adding (LLM shouldn't see it)
19872017
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
@@ -270,6 +270,59 @@ def flush_pending():
270270
return messages
271271

272272

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

0 commit comments

Comments
 (0)