fix: validate required image fields, narrow exception catch to preserve 500s for real bugs

This commit is contained in:
DrMelone 2026-04-12 22:59:59 +02:00
parent 5ac3930c98
commit 8768a909c0
2 changed files with 8 additions and 3 deletions

View file

@ -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))

View file

@ -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}')