From 8768a909c09937361f5e6559e022af63eaa13190 Mon Sep 17 00:00:00 2001 From: DrMelone <27028174+Classic298@users.noreply.github.com> Date: Sun, 12 Apr 2026 22:59:59 +0200 Subject: [PATCH] fix: validate required image fields, narrow exception catch to preserve 500s for real bugs --- backend/open_webui/main.py | 2 +- backend/open_webui/utils/anthropic.py | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index 6b69a7f748..664f19c0d9 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -1956,7 +1956,7 @@ async def generate_messages( try: openai_payload = convert_anthropic_to_openai_payload(form_data) - except Exception as e: + except (ValueError, NotImplementedError, TypeError, KeyError, AttributeError) as e: log.debug('Anthropic payload conversion failed: %s', e) raise HTTPException(status_code=400, detail=str(e)) diff --git a/backend/open_webui/utils/anthropic.py b/backend/open_webui/utils/anthropic.py index cc5cb32532..e62638fc46 100644 --- a/backend/open_webui/utils/anthropic.py +++ b/backend/open_webui/utils/anthropic.py @@ -99,10 +99,15 @@ def _convert_anthropic_image_source(source: dict) -> str: source_type = source.get('type') if source_type == 'base64': media_type = source.get('media_type', 'image/png') - data = source.get('data', '') + data = source.get('data') + if not data: + raise ValueError('Image source type "base64" is missing required "data" field') return f'data:{media_type};base64,{data}' elif source_type == 'url': - return source.get('url', '') + url = source.get('url') + if not url: + raise ValueError('Image source type "url" is missing required "url" field') + return url raise ValueError(f'Unsupported image source type: {source_type!r}')