From c36c63f1e8bb005db9e91bc66d8d7801d618c548 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 5 Feb 2025 02:10:28 -0800 Subject: [PATCH] fix: tag --- backend/open_webui/utils/middleware.py | 33 ++++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 6e4a875b4..68979e647 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1233,6 +1233,16 @@ async def process_chat_response( ].replace(match.group(0), "") if not content_blocks[-1]["content"]: content_blocks.pop() + + if not content_blocks: + # Append the new block + content_blocks.append( + { + "type": "text", + "content": "", + } + ) + # Append the new block content_blocks.append( { @@ -1461,14 +1471,23 @@ async def process_chat_response( log.debug("Error: ", e) continue - # Clean up the last text block - if content_blocks[-1]["type"] == "text": - content_blocks[-1]["content"] = content_blocks[-1][ - "content" - ].strip() + if content_blocks: + # Clean up the last text block + if content_blocks[-1]["type"] == "text": + content_blocks[-1]["content"] = content_blocks[-1][ + "content" + ].strip() - if not content_blocks[-1]["content"]: - content_blocks.pop() + if not content_blocks[-1]["content"]: + content_blocks.pop() + + if not content_blocks: + content_blocks.append( + { + "type": "text", + "content": "", + } + ) if response_tool_calls: tool_calls.append(response_tool_calls)