From b660a8fe7f73c2b5545fa430fc6e09b50001a49e Mon Sep 17 00:00:00 2001 From: Gaofeng Date: Mon, 8 Dec 2025 15:45:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20transform=5Fuser=5Fmodel?= =?UTF-8?q?=5Fif=5Fneeded=20=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BB=A5=E9=80=82?= =?UTF-8?q?=E9=85=8D=20/api/chat/completed=20=E6=8E=A5=E5=8F=A3=E5=AF=B9?= =?UTF-8?q?=E7=A7=81=E6=9C=89=E6=A8=A1=E5=9E=8B=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/open_webui/utils/models.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backend/open_webui/utils/models.py b/backend/open_webui/utils/models.py index 93e058aa74..22a19ee8e4 100644 --- a/backend/open_webui/utils/models.py +++ b/backend/open_webui/utils/models.py @@ -374,6 +374,22 @@ def get_filtered_models(models, user): async def transform_user_model_if_needed(form_data: dict, user: UserModel): + # If model_item is missing, try to reconstruct it from chat history + if "model_item" not in form_data or not form_data.get("model_item"): + chat_id = form_data.get("chat_id") + if chat_id: + from open_webui.models.chats import Chats + messages_map = Chats.get_messages_map_by_chat_id(chat_id) + if messages_map: + for message in messages_map.values(): + if "models" in message and isinstance(message["models"], list) and message["models"]: + for model_ref in message["models"]: + if isinstance(model_ref, str) and model_ref.startswith("user:"): + form_data["model_item"] = { "credential_id": model_ref } + break + if "model_item" in form_data: + break + model_id = form_data.get("model") model_item = form_data.get("model_item", {})