fix: strict image source validation, catch all conversion errors as 400

This commit is contained in:
DrMelone 2026-04-12 22:55:29 +02:00
parent 93186ba02c
commit 5ac3930c98
2 changed files with 17 additions and 13 deletions

View file

@ -1956,7 +1956,8 @@ async def generate_messages(
try:
openai_payload = convert_anthropic_to_openai_payload(form_data)
except (ValueError, NotImplementedError) as e:
except Exception as e:
log.debug('Anthropic payload conversion failed: %s', e)
raise HTTPException(status_code=400, detail=str(e))
# Route through the existing chat_completion handler

View file

@ -92,13 +92,18 @@ async def get_anthropic_models(url: str, key: str, user: UserModel = None) -> di
def _convert_anthropic_image_source(source: dict) -> str:
"""Convert an Anthropic image source block to an OpenAI-compatible image URL."""
if source.get('type') == 'base64':
if not isinstance(source, dict):
raise ValueError(
f'Expected dict for image source, got {type(source).__name__}'
)
source_type = source.get('type')
if source_type == 'base64':
media_type = source.get('media_type', 'image/png')
data = source.get('data', '')
return f'data:{media_type};base64,{data}'
elif source.get('type') == 'url':
elif source_type == 'url':
return source.get('url', '')
return ''
raise ValueError(f'Unsupported image source type: {source_type!r}')
def convert_anthropic_to_openai_payload(anthropic_payload: dict) -> dict:
@ -157,13 +162,12 @@ def convert_anthropic_to_openai_payload(anthropic_payload: dict) -> dict:
)
elif block_type == 'image':
url = _convert_anthropic_image_source(block.get('source', {}))
if url:
openai_content.append(
{
'type': 'image_url',
'image_url': {'url': url},
}
)
openai_content.append(
{
'type': 'image_url',
'image_url': {'url': url},
}
)
elif block_type == 'tool_use':
tool_calls.append(
{
@ -196,8 +200,7 @@ def convert_anthropic_to_openai_payload(anthropic_payload: dict) -> dict:
url = _convert_anthropic_image_source(
tc.get('source', {})
)
if url:
image_urls.append(url)
image_urls.append(url)
elif tc_type in ('search_result', 'document'):
raise NotImplementedError(
f'Anthropic tool_result content type '