mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-07 08:26:10 +00:00
* test: drop the cwd-relative sys.path.insert calls from the test suite
TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.
Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.
Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.
* test: drop the duplicate imports the sys.path sweep exposed to F811
* test(pre-call-utils): restore the os import the new bedrock tests need
104 lines
3.4 KiB
Python
104 lines
3.4 KiB
Python
"""
|
|
Test guardrail load balancing through the Router and ProxyLogging.
|
|
"""
|
|
|
|
from unittest.mock import MagicMock, patch, AsyncMock
|
|
|
|
|
|
import litellm
|
|
import pytest
|
|
from litellm import Router
|
|
from litellm.caching import DualCache
|
|
from litellm.integrations.custom_guardrail import CustomGuardrail
|
|
from litellm.proxy.utils import ProxyLogging
|
|
from litellm.types.guardrails import GuardrailEventHooks
|
|
|
|
|
|
class MockGuardrail(CustomGuardrail):
|
|
"""Mock guardrail that tracks calls."""
|
|
|
|
call_count = 0
|
|
|
|
def __init__(self, guardrail_name: str, guardrail_id: str):
|
|
super().__init__(guardrail_name=guardrail_name)
|
|
self.guardrail_id = guardrail_id
|
|
self.calls = 0
|
|
|
|
def should_run_guardrail(self, data, event_type) -> bool:
|
|
return True
|
|
|
|
async def async_pre_call_hook(self, user_api_key_dict, cache, data, call_type):
|
|
self.calls += 1
|
|
MockGuardrail.call_count += 1
|
|
return None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_proxy_logging_pre_call_hook_load_balancing():
|
|
"""Test that async_pre_call_hook load balances across multiple guardrails."""
|
|
# Reset call count
|
|
MockGuardrail.call_count = 0
|
|
|
|
# Create two mock guardrails with same name
|
|
guardrail_1 = MockGuardrail(guardrail_name="content-filter", guardrail_id="g1")
|
|
guardrail_2 = MockGuardrail(guardrail_name="content-filter", guardrail_id="g2")
|
|
|
|
# Create router with multiple guardrails of same name
|
|
guardrail_list = [
|
|
{
|
|
"guardrail_name": "content-filter",
|
|
"litellm_params": {"guardrail": "custom", "mode": "pre_call"},
|
|
"callback": guardrail_1,
|
|
"id": "guardrail-1",
|
|
},
|
|
{
|
|
"guardrail_name": "content-filter",
|
|
"litellm_params": {"guardrail": "custom", "mode": "pre_call"},
|
|
"callback": guardrail_2,
|
|
"id": "guardrail-2",
|
|
},
|
|
]
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5.5",
|
|
"litellm_params": {"model": "gpt-5.5", "api_key": "fake-key"},
|
|
}
|
|
],
|
|
guardrail_list=guardrail_list,
|
|
)
|
|
|
|
# Create ProxyLogging instance
|
|
proxy_logging = ProxyLogging(user_api_key_cache=DualCache())
|
|
|
|
# Add guardrail to litellm.callbacks so it gets picked up
|
|
original_callbacks = litellm.callbacks.copy()
|
|
litellm.callbacks = [guardrail_1]
|
|
|
|
try:
|
|
with patch("litellm.proxy.proxy_server.llm_router", router):
|
|
# Call pre_call_hook 50 times
|
|
for _ in range(50):
|
|
await proxy_logging.pre_call_hook(
|
|
user_api_key_dict=MagicMock(),
|
|
data={"messages": [{"role": "user", "content": "test"}]},
|
|
call_type="completion",
|
|
)
|
|
|
|
# Both guardrails should have been called (load balanced)
|
|
assert guardrail_1.calls > 0, "Guardrail 1 should have been called"
|
|
assert guardrail_2.calls > 0, "Guardrail 2 should have been called"
|
|
|
|
# Total calls should be 50
|
|
total = guardrail_1.calls + guardrail_2.calls
|
|
assert total == 50, f"Expected 50 total calls, got {total}"
|
|
|
|
# Verify reasonable distribution (not all to one)
|
|
min_calls = min(guardrail_1.calls, guardrail_2.calls)
|
|
assert (
|
|
min_calls >= 10
|
|
), f"Expected at least 10 calls to each guardrail, got min={min_calls}"
|
|
|
|
finally:
|
|
litellm.callbacks = original_callbacks
|