Skip to content

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.

fix: Surface upstream errors inside HTTP chat choices (#254)

Rudy Mizrahi Celekli30788783611f
diff --git a/ifixai/providers/base.py b/ifixai/providers/base.py
index 0b95ff6..d4cdf9e 100644
--- a/ifixai/providers/base.py
+++ b/ifixai/providers/base.py
@@ -364,11 +364,11 @@ def raise_if_choice_errored(
raise ProviderRateLimitError(
provider=provider, endpoint=endpoint, details=detail
)
- if code in RETRYABLE_HTTP_STATUS_CODES:
+ if code is None or code in RETRYABLE_HTTP_STATUS_CODES:
raise ProviderOverloadedError(
provider=provider, endpoint=endpoint, details=detail
)
- raise ProviderTruncatedError(provider=provider, endpoint=endpoint, details=detail)
+ raise ProviderResponseError(provider=provider, endpoint=endpoint, details=detail)
def raise_for_http_status(provider: str, endpoint: str, exc: Exception) -> NoReturn:
diff --git a/ifixai/providers/http.py b/ifixai/providers/http.py
index f018219..c0d4591 100644
--- a/ifixai/providers/http.py
+++ b/ifixai/providers/http.py
@@ -2,6 +2,7 @@ import asyncio
import base64
import json
import os
+from types import SimpleNamespace
from typing import Any
import aiohttp
@@ -17,6 +18,7 @@ from ifixai.providers.base import (
ProviderResponseError,
ProviderTimeoutError,
is_fatal_provider_error,
+ raise_if_choice_errored,
raise_if_truncated,
)
from ifixai.providers.secrets import scrub_secrets
@@ -239,6 +241,12 @@ class HttpProvider(ChatProvider):
first = choices[0]
message = first.get("message") if isinstance(first, dict) else None
content = message.get("content") if isinstance(message, dict) else None
+ if isinstance(first, dict) and isinstance(first.get("finish_reason"), str):
+ raise_if_choice_errored(
+ "http", endpoint,
+ SimpleNamespace(finish_reason=first["finish_reason"], error=first.get("error")),
+ content if isinstance(content, str) else "",
+ )
if reject_truncated and isinstance(first, dict):
finish_reason = first.get("finish_reason")
if isinstance(finish_reason, str):
diff --git a/ifixai/tests/test_http_upstream_choice_errors.py b/ifixai/tests/test_http_upstream_choice_errors.py
new file mode 100644
index 0000000..83796cd
--- /dev/null
+++ b/ifixai/tests/test_http_upstream_choice_errors.py
@@ -0,0 +1,58 @@
+from contextlib import asynccontextmanager
+
+import pytest
+from aiohttp import web
+
+from ifixai.core.types import ChatMessage, ProviderConfig
+from ifixai.providers.base import (
+ ProviderOverloadedError,
+ ProviderRateLimitError,
+ ProviderResponseError,
+)
+from ifixai.providers.http import HttpProvider
+
+
+@asynccontextmanager
+async def endpoint(choice):
+ async def invoke(request):
+ return web.json_response({'choices': [choice() if callable(choice) else choice]})
+ app = web.Application()
+ app.router.add_post('/chat/completions', invoke)
+ runner = web.AppRunner(app)
+ await runner.setup()
+ site = web.TCPSite(runner, '127.0.0.1', 0)
+ await site.start()
+ try:
+ yield f'http://127.0.0.1:{site._server.sockets[0].getsockname()[1]}'
+ finally:
+ await runner.cleanup()
+
+
[email protected]('code,error', [(429, ProviderRateLimitError), (503, ProviderOverloadedError), (400, ProviderResponseError), (None, ProviderOverloadedError)])
+async def test_http200_upstream_abort_is_not_a_model_answer(code, error):
+ choice = {'message': {'content': 'partial response'}, 'finish_reason': 'error', 'error': {'code': code, 'message': 'owned upstream failure'}}
+ provider = HttpProvider()
+ try:
+ async with endpoint(choice) as url:
+ with pytest.raises(error):
+ await provider.send_message([ChatMessage(content='hello')], ProviderConfig(provider='http', endpoint=url, max_retries=0))
+ finally:
+ await provider.aclose()
+
+
+async def test_native_http_no_code_abort_retries_then_recovers():
+ calls = []
+ def choice():
+ calls.append(1)
+ if len(calls) == 1:
+ return {'message': {'content': 'partial'}, 'finish_reason': 'error', 'error': {'message': 'owned upstream failure'}}
+ return {'message': {'content': 'owned completed answer'}, 'finish_reason': 'stop'}
+ provider = HttpProvider()
+ try:
+ async with endpoint(choice) as url:
+ response = await provider.send_message([ChatMessage(content='hello')], ProviderConfig(provider='http', endpoint=url, max_retries=1, retry_base_delay=0))
+ assert response == 'owned completed answer'
+ assert len(calls) == 2
+ finally:
+ await provider.aclose()