fix: handle non-JSON LLM API responses gracefully

- Check resp.content_type before calling resp.json()
- Return clear error if server returns HTML (e.g. proxy error or wrong endpoint)
- Prevents 'Attempt to decode JSON with unexpected mimetype' crashes
This commit is contained in:
yumoqing 2026-05-07 18:25:00 +08:00
parent e5e6796a4b
commit f9705f3892

View File

@ -229,6 +229,19 @@ async def _post_chat_completions(
}
}
# Check content type before parsing JSON
content_type = resp.content_type
if 'json' not in content_type:
err_text = await resp.text()
return {
'error': {
'message': f'LLM API returned non-JSON response (Content-Type: {content_type}). URL may be incorrect or blocked.',
'type': 'content_type_error',
'code': resp.status,
'detail': err_text[:500],
}
}
return await resp.json()
except asyncio.TimeoutError: