From ca4e07a40b4ff11989a25d84b517d3d0049f93ae Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 25 Aug 2026 11:07:54 -0400 Subject: [PATCH] refac --- backend/open_webui/routers/openai.py | 17 +++++++++++++++-- backend/open_webui/utils/middleware.py | 13 +++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/backend/open_webui/routers/openai.py b/backend/open_webui/routers/openai.py index 5988006059..3bc56ebcf6 100644 --- a/backend/open_webui/routers/openai.py +++ b/backend/open_webui/routers/openai.py @@ -1359,8 +1359,21 @@ def convert_to_responses_payload(payload: dict) -> dict: content_parts.append({'type': text_type, 'text': part.get('text', '')}) elif part.get('type') == 'image_url': url_data = part.get('image_url', {}) - url = url_data.get('url', '') if isinstance(url_data, dict) else url_data - content_parts.append({'type': 'input_image', 'image_url': url}) + if isinstance(url_data, dict): + url = url_data.get('url', '') + detail = url_data.get('detail') or 'auto' + else: + url = url_data if isinstance(url_data, str) else '' + detail = 'auto' + content_parts.append({'type': 'input_image', 'image_url': url, 'detail': detail}) + elif part.get('type') == 'file': + # OpenAI-compatible proxy path only. Open WebUI attachments are handled + # separately via metadata.files/RAG and must not be converted here. + file = part.get('file') + if isinstance(file, dict): + file_part = {k: file[k] for k in ('file_id', 'file_data', 'filename') if k in file} + if 'file_id' in file_part or 'file_data' in file_part: + content_parts.append({'type': 'input_file', **file_part}) else: content_parts = [{'type': text_type, 'text': str(content)}] diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index d2dd962641..cc46184d4a 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -2123,7 +2123,13 @@ async def convert_url_images_to_base64(form_data, user=None): new_content.append(item) continue - image_url = item.get('image_url', {}).get('url', '') + image_url_data = item.get('image_url', {}) + if isinstance(image_url_data, dict): + image_url = image_url_data.get('url') or '' + elif isinstance(image_url_data, str): + image_url = image_url_data + else: + image_url = '' if image_url.startswith('data:image/'): new_content.append(item) continue @@ -2131,10 +2137,13 @@ async def convert_url_images_to_base64(form_data, user=None): try: base64_data = await get_image_base64_from_url(image_url, user=user) if base64_data: + image_url_payload = {'url': base64_data} + if isinstance(image_url_data, dict) and image_url_data.get('detail'): + image_url_payload['detail'] = image_url_data['detail'] new_content.append( { 'type': 'image_url', - 'image_url': {'url': base64_data}, + 'image_url': image_url_payload, } ) else: