From f9705f389260d4db2f701236f5181ca9b2b53559 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 7 May 2026 18:25:00 +0800 Subject: [PATCH] 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 --- harnessed_agent/llm_client.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/harnessed_agent/llm_client.py b/harnessed_agent/llm_client.py index 79d7393..f349029 100644 --- a/harnessed_agent/llm_client.py +++ b/harnessed_agent/llm_client.py @@ -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: