mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-09-04 02:01:48 +03:00
test(fallback): classify authentication exception messages
This commit is contained in:
@@ -623,6 +623,8 @@ class FallbackProvider(LLMProvider):
|
|||||||
raise
|
raise
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
error_name = type(exc).__name__.lower()
|
error_name = type(exc).__name__.lower()
|
||||||
|
detail = str(exc).strip() or type(exc).__name__
|
||||||
|
detail_lower = detail.lower()
|
||||||
error_kind: str | None = None
|
error_kind: str | None = None
|
||||||
error_should_retry: bool | None = None
|
error_should_retry: bool | None = None
|
||||||
if isinstance(exc, (httpx.TimeoutException, asyncio.TimeoutError)):
|
if isinstance(exc, (httpx.TimeoutException, asyncio.TimeoutError)):
|
||||||
@@ -634,7 +636,7 @@ class FallbackProvider(LLMProvider):
|
|||||||
elif any(
|
elif any(
|
||||||
token in error_name
|
token in error_name
|
||||||
for token in ("auth", "credential", "permissiondenied", "unauthor")
|
for token in ("auth", "credential", "permissiondenied", "unauthor")
|
||||||
):
|
) or any(token in detail_lower for token in _AUTHENTICATION_ERROR_TOKENS):
|
||||||
error_kind = "authentication"
|
error_kind = "authentication"
|
||||||
elif "ratelimit" in error_name or "throttl" in error_name:
|
elif "ratelimit" in error_name or "throttl" in error_name:
|
||||||
error_kind = "rate_limit"
|
error_kind = "rate_limit"
|
||||||
@@ -652,7 +654,6 @@ class FallbackProvider(LLMProvider):
|
|||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
error_status_code = None
|
error_status_code = None
|
||||||
|
|
||||||
detail = str(exc).strip() or type(exc).__name__
|
|
||||||
return LLMResponse(
|
return LLMResponse(
|
||||||
content=f"Error calling LLM: {detail}",
|
content=f"Error calling LLM: {detail}",
|
||||||
finish_reason="error",
|
finish_reason="error",
|
||||||
|
|||||||
@@ -423,6 +423,19 @@ class TestFallbackWhenPrimaryRaises:
|
|||||||
assert result.finish_reason == "stop"
|
assert result.finish_reason == "stop"
|
||||||
factory.assert_called_once_with(_fallback("fallback-a"))
|
factory.assert_called_once_with(_fallback("fallback-a"))
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_authentication_exception_message_is_classified(self) -> None:
|
||||||
|
primary = _RaisingProvider("primary")
|
||||||
|
|
||||||
|
response, exception = await FallbackProvider._call_provider(
|
||||||
|
lambda provider, kwargs: provider.chat(**kwargs),
|
||||||
|
primary,
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert exception is primary._exc
|
||||||
|
assert response.error_kind == "authentication"
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"exc",
|
"exc",
|
||||||
[
|
[
|
||||||
|
|||||||
Reference in New Issue
Block a user