diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index d377446249..94521d6ce0 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -97,6 +97,7 @@ from open_webui.utils.misc import ( convert_logit_bias_input_to_json, get_content_from_message, convert_output_to_messages, + strip_empty_content_blocks, ) from open_webui.utils.tools import ( get_tools, @@ -2634,6 +2635,10 @@ async def process_chat_payload(request, form_data, user, metadata, model): } ) + # Strip empty text content blocks from multimodal messages + # to prevent errors from providers like Gemini and Claude + form_data['messages'] = strip_empty_content_blocks(form_data.get('messages', [])) + return form_data, metadata, events diff --git a/backend/open_webui/utils/misc.py b/backend/open_webui/utils/misc.py index dec97abd25..91217b3c23 100644 --- a/backend/open_webui/utils/misc.py +++ b/backend/open_webui/utils/misc.py @@ -401,6 +401,31 @@ def append_or_update_assistant_message(content: str, messages: list[dict]): return messages +def strip_empty_content_blocks(messages: list[dict]) -> list[dict]: + """ + Remove empty text content blocks from multimodal message content arrays. + + Providers like Gemini and Claude reject messages where a text block has + an empty string. This can happen when a user sends only file/image + attachments without typing any text. + """ + for message in messages: + content = message.get('content') + if isinstance(content, list): + cleaned = [ + block + for block in content + if not ( + isinstance(block, dict) + and block.get('type') == 'text' + and not block.get('text', '').strip() + ) + ] + if cleaned: + message['content'] = cleaned + return messages + + def openai_chat_message_template(model: str): return { 'id': f'{model}-{str(uuid.uuid4())}', diff --git a/src/lib/components/chat/MessageInput/InputVariablesModal.svelte b/src/lib/components/chat/MessageInput/InputVariablesModal.svelte index b50ed38395..5a6cfd4346 100644 --- a/src/lib/components/chat/MessageInput/InputVariablesModal.svelte +++ b/src/lib/components/chat/MessageInput/InputVariablesModal.svelte @@ -17,7 +17,7 @@ export let onSave = (e) => {}; - let loading = false; + let loading = true; let variableValues = {}; const submitHandler = async () => {