litellm/tests/unit/proxy/proxy_server/test_routes_moderations.py
paveln83 b84493d9a7
fix(proxy): log each endpoint's own call type on failed audio, moderation, image, and rerank spend rows (#49684)
* fix(proxy): record the route's call type on failed speech spend rows

A failed /v1/audio/speech request never puts a logging object on the request
data, so the failure lift returned nothing and the spend row's call_type came
out empty (or whatever the client sent in the body). Without a logging object,
lift the call type the route already resolved, so a failure row records the
same call type a success row on that route records

* fix(proxy): keep the call type of a failure logged under its own route

An MCP tool listing that fails inside a Responses, chat, or messages call
is logged under /mcp/list_tools, so its spend row keeps list_mcp_tools
instead of taking the outer request's call type

* fix(proxy): take a failed row's call type from its endpoint, not a route path guess

* fix(proxy): log the endpoint call type on failed image generation and rerank rows

* fix(proxy): estimate failed-call ledger usage from the route's call type again

The failure usage snapshot now gets the same call type the spend row uses for
its estimate, so a failed Responses call counts input from the newest
compaction item again, as on main.

The endpoint call type tests now go through real routers whose provider
answers 400, instead of asserting on a mocked failure hook.

* test(proxy): type the failed spend row tests and fixture

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-02 23:57:11 +00:00

127 lines
3.8 KiB
Python

"""Behavior pins for ``proxy_server.py`` moderations routes.
Pins (PR2):
- POST /v1/moderations
- POST /moderations
"""
from __future__ import annotations
import contextlib
from typing import TYPE_CHECKING, Callable, Final
from unittest.mock import AsyncMock, MagicMock
import pytest
from litellm.proxy import proxy_server
if TYPE_CHECKING:
from fastapi.testclient import TestClient
from .conftest import normalize # type: ignore[import-not-found]
HAPPY_RESPONSE = {
"id": "modr-test",
"model": "text-moderation-stable",
"results": [
{
"flagged": False,
"categories": {"violence": False},
"category_scores": {"violence": 0.01},
}
],
}
@pytest.fixture
def patched_moderation(monkeypatch):
monkeypatch.setattr(proxy_server, "llm_router", MagicMock())
monkeypatch.setattr(
proxy_server,
"proxy_logging_obj",
MagicMock(
pre_call_hook=AsyncMock(side_effect=lambda **kw: kw["data"]),
post_call_failure_hook=AsyncMock(),
update_request_status=AsyncMock(),
),
)
async def _add_data(data, **kwargs):
return data
monkeypatch.setattr(proxy_server, "add_litellm_data_to_request", _add_data)
async def _fake_llm_call():
return dict(HAPPY_RESPONSE)
async def _fake_route_request(*args, **kwargs):
return _fake_llm_call()
monkeypatch.setattr(proxy_server, "route_request", _fake_route_request)
yield
@pytest.fixture
def moderation_pipeline_raises(monkeypatch):
monkeypatch.setattr(proxy_server, "llm_router", MagicMock())
monkeypatch.setattr(
proxy_server,
"proxy_logging_obj",
MagicMock(
pre_call_hook=AsyncMock(side_effect=lambda **kw: kw["data"]),
post_call_failure_hook=AsyncMock(),
update_request_status=AsyncMock(),
),
)
async def _add_data(data, **kwargs):
return data
monkeypatch.setattr(proxy_server, "add_litellm_data_to_request", _add_data)
async def _raise(*args, **kwargs):
raise ValueError("boom")
monkeypatch.setattr(proxy_server, "route_request", _raise)
yield
@pytest.mark.parametrize("path", ["/v1/moderations", "/moderations"])
def test_moderation_happy_path(client, auth_as, patched_moderation, path):
"""Pins ``POST /v1/moderations`` and ``POST /moderations`` (happy)."""
payload = {"model": "text-moderation-stable", "input": "Sample text"}
with auth_as():
response = client.post(path, json=payload)
assert response.status_code == 200
assert normalize(response.json()) == {
"id": "<VOLATILE>",
"model": "text-moderation-stable",
"results": [
{
"flagged": False,
"categories": {"violence": False},
"category_scores": {"violence": 0.01},
}
],
}
@pytest.mark.parametrize("path", ["/v1/moderations", "/moderations"])
def test_moderation_error(client, auth_as, moderation_pipeline_raises, path):
"""Pins ``POST /v1/moderations`` and ``POST /moderations`` (error)."""
payload = {"model": "text-moderation-stable", "input": "Sample text"}
with auth_as():
response = client.post(path, json=payload)
assert response.status_code == 500
assert len(response.content) > 0
def test_a_provider_rejection_logs_the_moderation_call_type(
client: TestClient,
auth_as: Callable[..., contextlib.AbstractContextManager[object]],
failed_spend_row: Callable[[], tuple[str, str]],
) -> None:
with auth_as():
response: Final = client.post("/v1/moderations", json={"model": "omni-moderation", "input": ["one", "two"]})
assert response.status_code == 400, response.text
assert failed_spend_row() == ("failure", "amoderation")