Skip to content

Commit 755a3a7

Browse files
Algorithm5838github-actions[bot]
authored andcommitted
fix: prevent answer split from late reasoning
1 parent 9951dc7 commit 755a3a7

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

backend/open_webui/utils/middleware.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4321,13 +4321,26 @@ async def queue_pending_delta_data(delta_data: dict, delta_type: str):
43214321
reasoning_content = ''
43224322
reasoning_details = None
43234323

4324-
if reasoning_content or reasoning_details:
4324+
# Skip orphan details (e.g. a lone signature) with nothing to render and no block to merge into.
4325+
if reasoning_content or (
4326+
reasoning_details
4327+
and (
4328+
any(item.get('type') == 'reasoning' for item in output)
4329+
or any(
4330+
isinstance(d, dict)
4331+
and (d.get('text') or d.get('summary') or d.get('data'))
4332+
for d in reasoning_details
4333+
)
4334+
)
4335+
):
4336+
# Merge late reasoning into the pre-message block so the answer isn't split.
4337+
answer_started = any(item.get('type') == 'message' for item in output)
43254338
reasoning_item = (
43264339
next(
43274340
(item for item in reversed(output) if item.get('type') == 'reasoning'),
43284341
None,
43294342
)
4330-
if reasoning_details and not reasoning_content
4343+
if (reasoning_details and not reasoning_content) or answer_started
43314344
else None
43324345
)
43334346

@@ -4344,7 +4357,13 @@ async def queue_pending_delta_data(delta_data: dict, delta_type: str):
43444357
'summary': None,
43454358
'started_at': time.time(),
43464359
}
4347-
output.append(reasoning_item)
4360+
if answer_started:
4361+
message_index = next(
4362+
i for i, x in enumerate(output) if x.get('type') == 'message'
4363+
)
4364+
output.insert(message_index, reasoning_item)
4365+
else:
4366+
output.append(reasoning_item)
43484367
else:
43494368
reasoning_item = output[-1]
43504369

0 commit comments

Comments
 (0)