fix(proxy): type a hook's 401 or 403 on completions, moderations and transcription by its status, as chat does

Only a status the proxy remapped into 401 or 403, such as a tag-routing denial, keeps auth_error.
This commit is contained in:
mateo-berri 2026-10-29 08:54:46 -07:00
parent ed2c5c0bdc
commit 7c75bc78f4
4 changed files with 41 additions and 7 deletions

View file

@ -142,10 +142,11 @@ def route_failure_proxy_exception(
headers: dict[str, str] | None = None, # mutable-ok: ProxyException.headers
openai_code: str | None = None,
) -> ProxyException:
answered_status: Final = answered_status_code(message, error_status_code(exc, default_status_code))
raised_status: Final = error_status_code(exc, default_status_code)
answered_status: Final = answered_status_code(message, raised_status)
return ProxyException(
message=message,
type=_catch_all_error_type(exc, answered_status),
type=_route_failure_error_type(exc, raised_status, answered_status),
param=openai_error_param(exc),
headers=headers,
openai_code=openai_code,
@ -153,6 +154,12 @@ def route_failure_proxy_exception(
)
def _route_failure_error_type(exc: Exception, raised_status: int, answered_status: int) -> str:
if answered_status != raised_status and answered_status in _AUTH_FAILURE_STATUS_CODES:
return ProxyErrorTypes.auth_error.value
return openai_error_type(exc, answered_status)
def _openai_shaped_proxy_exception(exc: Exception, message: str, status_code: int) -> ProxyException:
answered_status: Final = answered_status_code(message, status_code)
return ProxyException(

View file

@ -14,6 +14,7 @@ from unittest.mock import AsyncMock, MagicMock
import httpx
import pytest
from fastapi import HTTPException
from litellm.proxy import proxy_server
from litellm.proxy._types import ProxyErrorTypes, ProxyException
@ -284,19 +285,31 @@ def test_audio_speech_tag_routing_denial_is_typed_as_a_proxy_auth_error(
assert (error["type"], error["param"]) == ("auth_error", None)
def test_audio_transcription_tag_routing_denial_is_typed_as_a_proxy_auth_error(
client, auth_as, patched_transcription, monkeypatch
@pytest.mark.parametrize(
("failure", "expected_status", "expected_type"),
[
pytest.param(ValueError(_TAG_ROUTING_DENIAL), 401, "auth_error", id="tag_routing_denial"),
pytest.param(
HTTPException(status_code=403, detail="Blocked by the content policy"),
403,
"permission_error",
id="hook_policy_block",
),
],
)
def test_audio_transcription_failure_is_typed_by_who_rejected_it(
client, auth_as, patched_transcription, monkeypatch, failure, expected_status, expected_type
):
async def _deny(*args, **kwargs):
raise ValueError(_TAG_ROUTING_DENIAL)
raise failure
monkeypatch.setattr(proxy_server, "route_request", _deny)
files = {"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")}
with auth_as():
response = client.post("/v1/audio/transcriptions", files=files, data={"model": "whisper-1"})
assert response.status_code == 401
assert response.status_code == expected_status
error = response.json()["error"]
assert (error["type"], error["param"]) == ("auth_error", None)
assert (error["type"], error["param"]) == (expected_type, None)
@pytest.mark.parametrize(

View file

@ -12,6 +12,7 @@ from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock
import pytest
from fastapi import HTTPException
from litellm.proxy import common_request_processing, proxy_server
from litellm.types.router import RouterErrors
@ -131,6 +132,13 @@ def test_completion_pipeline_error(client, auth_as, completion_pipeline_raises,
("failure", "expected_status", "expected_type"),
[
pytest.param(ValueError("boom"), 500, "internal_server_error", id="unexpected_failure"),
pytest.param(
HTTPException(status_code=403, detail="Blocked by the content policy"),
403,
"permission_error",
id="hook_policy_block",
),
pytest.param(HTTPException(status_code=401, detail="key expired"), 401, "authentication_error", id="hook_401"),
pytest.param(
ValueError(f"{RouterErrors.no_deployments_with_tag_routing.value}. Passed model=gpt-4o and tags=['x']"),
401,

View file

@ -14259,6 +14259,12 @@ async def test_moderations_reraises_proxy_exception_unwrapped():
[
pytest.param(HTTPException(status_code=400, detail="bad input"), "invalid_request_error", "400", id="http_400"),
pytest.param(HTTPException(status_code=429, detail="slow down"), "rate_limit_error", "429", id="http_429"),
pytest.param(
HTTPException(status_code=403, detail="Blocked by the content policy"),
"permission_error",
"403",
id="hook_policy_block",
),
pytest.param(Exception("provider down"), "internal_server_error", "500", id="bare_exception"),
pytest.param(
ValueError("Not allowed to access model due to tags configuration. Passed model=m and tags=['x']"),