Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.
Python agent-evaluationaiai-alignmentai-evaluationai-governanceai-safetyclidiagnostic-tooleu-ai-acthallucination-detectioniso-42001llm-evaluationllm-securitynist-ai-rmfowasp-llmprompt-injectionpythonresponsible-airisk-assessmentrisk-management
fix: report malformed HTTP success payloads as provider errors (#125)
Rudy Mizrahi Celekli5b9b12764614
diff --git a/ifixai/providers/http.py b/ifixai/providers/http.pyindex c019c38..75198e2 100644--- a/ifixai/providers/http.py+++ b/ifixai/providers/http.py@@ -182,7 +182,20 @@ class HttpProvider(ChatProvider):details=f"HTTP {resp.status}: {scrub_secrets(body[:500])}",)- data = await resp.json()+ try:+ data = await resp.json()+ except (aiohttp.ContentTypeError, json.JSONDecodeError) as exc:+ raise ProviderResponseError(+ provider="http",+ endpoint=endpoint,+ details="HTTP 200 response is not valid JSON",+ ) from exc+ if not isinstance(data, dict):+ raise ProviderResponseError(+ provider="http",+ endpoint=endpoint,+ details="HTTP 200 response must be a JSON object",+ )return self._extract_response_text(data, endpoint)except aiohttp.ClientConnectionError as exc:@@ -199,30 +212,23 @@ class HttpProvider(ChatProvider):) from excdef _extract_response_text(self, data: dict[str, Any], endpoint: str) -> str:- try:- choices = data.get("choices", [])- if not choices:- raise ProviderResponseError(- provider="http",- endpoint=endpoint,- details="No choices in response",- )- message = choices[0].get("message", {})- content = message.get("content", "")- if not content:- raise ProviderResponseError(- provider="http",- endpoint=endpoint,- details="Empty content in response",- )- except (KeyError, IndexError, TypeError) as exc:+ choices = data.get("choices")+ if not isinstance(choices, list) or not choices:raise ProviderResponseError(provider="http",endpoint=endpoint,- details=f"Unexpected response format: {exc}",- ) from exc- else:- return content+ details="Missing or invalid choices in response",+ )+ first = choices[0]+ message = first.get("message") if isinstance(first, dict) else None+ content = message.get("content") if isinstance(message, dict) else None+ if not isinstance(content, str) or not content:+ raise ProviderResponseError(+ provider="http",+ endpoint=endpoint,+ details="Missing or non-text content in response",+ )+ return contentasync def retrieve_sources(self,@@ -243,13 +249,18 @@ class HttpProvider(ChatProvider):if resp.status >= 400:return Nonedata = await resp.json()+ if not isinstance(data, dict):+ return Noneraw = data.get("sources")if not isinstance(raw, list):return None- return [- _source_item_to_retrieved(s) for s in raw if isinstance(s, dict)- ]- except (aiohttp.ClientError, asyncio.TimeoutError):+ try:+ return [+ _source_item_to_retrieved(s) for s in raw if isinstance(s, dict)+ ]+ except (TypeError, ValueError):+ return None+ except (aiohttp.ClientError, asyncio.TimeoutError, json.JSONDecodeError):# Sources attached to a previous chat response cannot establish# what this independent retrieval query would have returned.return Nonediff --git a/ifixai/tests/providers/test_http_response_shapes.py b/ifixai/tests/providers/test_http_response_shapes.pynew file mode 100644index 0000000..2bfde2b--- /dev/null+++ b/ifixai/tests/providers/test_http_response_shapes.py@@ -0,0 +1,64 @@+"""Malformed successful HTTP replies should surface as provider errors."""++import aiohttp.web+import pytest++from ifixai.core.types import ChatMessage, ProviderConfig+from ifixai.providers.base import ProviderResponseError+from ifixai.providers.http import HttpProvider+++ "reply",+ [+ "not json",+ [],+ {"choices": "bad"},+ {"choices": [{"message": {"content": ["not", "text"]}}]},+ ],+)+async def test_malformed_chat_reply_is_provider_response_error(reply):+ async def chat(_request):+ if isinstance(reply, str):+ return aiohttp.web.Response(text=reply, content_type="text/plain")+ return aiohttp.web.json_response(reply)++ app = aiohttp.web.Application()+ app.router.add_post("/v1/chat/completions", chat)+ runner = aiohttp.web.AppRunner(app)+ await runner.setup()+ site = aiohttp.web.TCPSite(runner, "127.0.0.1", 0)+ await site.start()+ port = site._server.sockets[0].getsockname()[1]+ provider = HttpProvider()+ config = ProviderConfig(+ provider="http", endpoint=f"http://127.0.0.1:{port}/v1", max_retries=0+ )+ try:+ with pytest.raises(ProviderResponseError):+ await provider.send_message([ChatMessage(content="hello")], config)+ finally:+ await provider.aclose()+ await runner.cleanup()+++async def test_malformed_retrieval_reply_reports_unavailable():+ async def retrieve(_request):+ return aiohttp.web.json_response([{"document_uri": "wrong shape"}])++ app = aiohttp.web.Application()+ app.router.add_post("/v1/retrieve", retrieve)+ runner = aiohttp.web.AppRunner(app)+ await runner.setup()+ site = aiohttp.web.TCPSite(runner, "127.0.0.1", 0)+ await site.start()+ port = site._server.sockets[0].getsockname()[1]+ provider = HttpProvider()+ config = ProviderConfig(provider="http", endpoint=f"http://127.0.0.1:{port}/v1")+ try:+ assert await provider.retrieve_sources("hello", config) is None+ finally:+ await provider.aclose()+ await runner.cleanup()