From e4f7b3b1569181e84c2757217a116ecf64e899f4 Mon Sep 17 00:00:00 2001 From: eml-henn Date: Mon, 8 Dec 2025 15:25:39 +0100 Subject: [PATCH] Fix #19750 : Handle reference images correctly in image edit - Modified get_last_images() to distinguish between user-uploaded and assistant-generated images - Fixes bug where uploading a reference image after generation would ignore the previously generated image - Assistant images are now prioritized for editing with user references appended --- backend/open_webui/utils/middleware.py | 35 +++++++++----------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index aee448c370..49b25e560e 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -716,36 +716,25 @@ async def chat_web_search_handler( return form_data - - -def get_last_user_images(message_list): - """Get all images from the last USER message only""" +def get_last_images(message_list): + """Get images from last user and assistant messages in chronological order""" + found_assistant_with_images = False + found_user_with_images = False images = [] for message in reversed(message_list): - if message.get("role") == "user": + if found_assistant_with_images and found_user_with_images: + break + if message.get("role") == "user" and not images: for file in message.get("files", []): if file.get("type") == "image": images.append(file.get("url")) - break # Only from most recent user message - return images - -def get_last_assistant_image(message_list): - """Get the most recent ASSISTANT-generated image""" - for message in reversed(message_list): - if message.get("role") == "assistant": + found_user_with_images = True + if message.get("role") == "assistant" and not found_assistant_with_images: for file in message.get("files", []): if file.get("type") == "image": - return file.get("url") - return None - -def get_last_images(message_list): - assistant_image = get_last_assistant_image(message_list) - user_images = get_last_user_images(message_list) - - if assistant_image: - return [assistant_image] + user_images - - return user_images + images.append(file.get("url")) + found_assistant_with_images = True + return images def get_image_urls(delta_images, request, metadata, user) -> list[str]: