fix(proxy): log the request as sent when a guardrail blocks after the model check passed

The model check now refreshes the logged request copy only when it fails, so a later
block logs the copy taken on arrival, as it did before the check moved
This commit is contained in:
yucheng 2026-10-26 18:34:54 -07:00
parent 7ee528e294
commit a3de32375a
2 changed files with 26 additions and 7 deletions

View file

@ -2562,13 +2562,16 @@ class ProxyBaseLLMRequestProcessing:
async def raise_unless_routable(request: dict[str, object]) -> None: # mutable-ok: request payload
self.data = request
refresh_proxy_server_request_body_snapshot(request)
await raise_if_model_not_routable(
data=request,
llm_router=llm_router,
user_model=user_model,
route_type=call_type,
)
try:
await raise_if_model_not_routable(
data=request,
llm_router=llm_router,
user_model=user_model,
route_type=call_type,
)
except Exception:
refresh_proxy_server_request_body_snapshot(request)
raise
passed_routing_check.append(True)
try:

View file

@ -12131,6 +12131,22 @@ class TestPreCallHooksForUnroutableModel:
logging_obj: Final = processor.data["litellm_logging_obj"]
assert logging_obj.standard_callback_dynamic_params.get("turn_off_message_logging") is not True
@pytest.mark.asyncio
async def test_request_rejected_after_the_routing_check_passed_logs_the_request_as_sent(self, monkeypatch):
monkeypatch.setattr(litellm, "callbacks", [_ModelRewritingHook(), _CountingGuardrail(), _RejectingHook()])
processor: Final = ProxyBaseLLMRequestProcessing(data={})
with pytest.raises(HTTPException):
await self._run(monkeypatch, model="virtual-model", llm_router=self._router(), processor=processor)
snapshot: Final = processor.data["proxy_server_request"]["body"]
assert processor.data["model"] == "gpt-4o-mini"
assert (snapshot["model"], snapshot["messages"], "litellm_call_id" in snapshot) == (
"virtual-model",
[{"role": "user", "content": "email jane@example.com"}],
False,
), snapshot
@pytest.mark.asyncio
async def test_request_a_hook_replaced_with_another_unknown_model_is_the_one_rejected_and_redacted(
self, monkeypatch