mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-02 01:28:38 +00:00
* test(proxy): move auth, hooks, policy_engine and client tests into tests/unit/proxy Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): stub HIBP through respx by disabling the aiohttp transport Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): share the httpx transport fixture across proxy unit tests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): restore proxy globals without a missing-value sentinel Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): package moved dirs and stub the login breach check at the HTTP boundary Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): isolate the mcp server manager per test Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): reuse the shared httpx transport fixture in moved proxy tests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): stub outbound HTTP and package moved test dirs Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): restore the config server hostname in the mcp resolution test Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): pin the completion tokenizer model in the straiker screening test Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: yuneng <yuneng@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
419 lines
16 KiB
Python
419 lines
16 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import json
|
|
import warnings
|
|
from collections.abc import Mapping
|
|
from dataclasses import dataclass, field
|
|
from typing import Final, Literal
|
|
|
|
import httpx
|
|
import pytest
|
|
import respx
|
|
from fastapi import HTTPException
|
|
|
|
import litellm
|
|
from litellm.integrations.custom_guardrail import CustomGuardrail
|
|
from litellm.proxy.guardrails.guardrail_endpoints import get_guardrail_ui_settings, get_provider_specific_params
|
|
from litellm.proxy.guardrails.guardrail_hooks.conduct import (
|
|
DEFAULT_TIMEOUT_SECONDS,
|
|
ConductGuardrail,
|
|
initialize_guardrail,
|
|
)
|
|
from litellm.proxy.guardrails.guardrail_hooks.conduct.conduct import (
|
|
apply_conduct_guardrail,
|
|
binds_unreachable_fallback,
|
|
record_decision,
|
|
request_payload,
|
|
)
|
|
from litellm.proxy.guardrails.guardrail_registry import InMemoryGuardrailHandler
|
|
from litellm.types.guardrails import Guardrail, GuardrailEventHooks, LitellmParams
|
|
from litellm.types.llms.openai import ChatCompletionAssistantMessage
|
|
from litellm.types.proxy.guardrails.guardrail_hooks.conduct import (
|
|
ConductGuardrailConfigModel,
|
|
ConductGuardrailConfigModelOptionalParams,
|
|
)
|
|
from litellm.types.utils import GenericGuardrailAPIInputs
|
|
|
|
PACKAGE_INSTALLED: Final = importlib.util.find_spec("conduct_litellm_guard") is not None
|
|
|
|
|
|
class _RecordingGuardrail(CustomGuardrail):
|
|
"""Stand-in with the ``conduct_litellm_guard.ConductGuard`` class contract."""
|
|
|
|
@classmethod
|
|
def get_supported_event_hooks(cls) -> list[GuardrailEventHooks]:
|
|
return [GuardrailEventHooks.pre_call]
|
|
|
|
def __init__(
|
|
self,
|
|
*,
|
|
api_url: str | None = None,
|
|
agent_token: str | None = None,
|
|
workspace_id: str | None = None,
|
|
unreachable_fallback: str | None = None,
|
|
tool_name: str = "llm_call",
|
|
timeout: float = 8.0,
|
|
guardrail_name: str | None = None,
|
|
event_hook: str | None = None,
|
|
default_on: bool = False,
|
|
supported_event_hooks: list[GuardrailEventHooks] | None = None,
|
|
) -> None:
|
|
super().__init__(
|
|
guardrail_name=guardrail_name,
|
|
event_hook=event_hook, # pyright: ignore[reportArgumentType] # CustomGuardrail coerces the str at runtime
|
|
default_on=default_on,
|
|
supported_event_hooks=supported_event_hooks,
|
|
)
|
|
self.api_url = api_url
|
|
self.agent_token = agent_token
|
|
self.workspace_id = workspace_id
|
|
self.unreachable_fallback = unreachable_fallback or "fail_closed"
|
|
self.tool_name = tool_name
|
|
self.timeout = timeout
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class _Decision:
|
|
verdict: str
|
|
rule_id: str | None = None
|
|
|
|
|
|
class _Blocked(Exception):
|
|
def __init__(self, decision: _Decision) -> None:
|
|
super().__init__(decision.verdict)
|
|
self.decision = decision
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class _RecordingCheck:
|
|
verdict: str
|
|
rule_id: str | None = None
|
|
calls: list[tuple[Mapping[str, object], str]] = field(default_factory=list) # mutable-ok: test spy
|
|
recorded: list[_Decision] = field(default_factory=list) # mutable-ok: test spy
|
|
|
|
async def __call__(self, *, data: Mapping[str, object], call_type: str) -> _Decision:
|
|
self.calls.append((data, call_type))
|
|
return _Decision(self.verdict, self.rule_id)
|
|
|
|
def record(self, decision: _Decision) -> None:
|
|
self.recorded.append(decision)
|
|
|
|
|
|
async def _bridge(
|
|
check: _RecordingCheck,
|
|
inputs: GenericGuardrailAPIInputs,
|
|
request_data: Mapping[str, object],
|
|
input_type: Literal["request", "response"],
|
|
) -> GenericGuardrailAPIInputs:
|
|
return await apply_conduct_guardrail(inputs, request_data, input_type, check, _Blocked, check.record)
|
|
|
|
|
|
def _guardrail_records(request_data: Mapping[str, object]) -> list[tuple[str, object]]:
|
|
metadata: Final = request_data["metadata"]
|
|
assert isinstance(metadata, dict)
|
|
records: Final = metadata["standard_logging_guardrail_information"]
|
|
assert isinstance(records, list)
|
|
return [(record["guardrail_status"], record["guardrail_response"]) for record in records]
|
|
|
|
|
|
def _params(mode: str = "pre_call", **extras: object) -> LitellmParams:
|
|
return LitellmParams(guardrail="conduct", mode=mode, api_key="cond_agt_test", **extras)
|
|
|
|
|
|
def _guardrail(litellm_params: LitellmParams) -> Guardrail:
|
|
return Guardrail(guardrail_name="conduct-guard", litellm_params=litellm_params)
|
|
|
|
|
|
def _init(litellm_params: LitellmParams) -> _RecordingGuardrail:
|
|
callback: Final = initialize_guardrail(
|
|
litellm_params, _guardrail(litellm_params), guardrail_cls=_RecordingGuardrail
|
|
)
|
|
assert isinstance(callback, _RecordingGuardrail)
|
|
return callback
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _isolate_callbacks(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(litellm, "callbacks", [])
|
|
|
|
|
|
def test_maps_typed_fields_and_extras_onto_plugin_kwargs() -> None:
|
|
callback: Final = _init(
|
|
_params(
|
|
api_base="https://guard.example.test",
|
|
unreachable_fallback="fail_open",
|
|
timeout="3",
|
|
workspace_id="ws_123",
|
|
tool_name="workflow",
|
|
default_on=True,
|
|
)
|
|
)
|
|
|
|
assert callback.api_url == "https://guard.example.test"
|
|
assert callback.agent_token == "cond_agt_test"
|
|
assert callback.unreachable_fallback == "fail_open"
|
|
assert callback.timeout == 3.0
|
|
assert callback.workspace_id == "ws_123"
|
|
assert callback.tool_name == "workflow"
|
|
assert callback.guardrail_name == "conduct-guard"
|
|
assert callback.event_hook == "pre_call"
|
|
assert callback.default_on is True
|
|
assert litellm.callbacks == [callback]
|
|
|
|
|
|
def test_defaults_when_optional_config_is_omitted() -> None:
|
|
callback: Final = _init(_params())
|
|
|
|
assert callback.unreachable_fallback == "fail_closed"
|
|
assert callback.timeout == DEFAULT_TIMEOUT_SECONDS
|
|
assert callback.workspace_id is None
|
|
assert callback.tool_name == "llm_call"
|
|
|
|
|
|
def test_ui_form_defaults_match_what_the_initializer_forwards() -> None:
|
|
optional: Final = ConductGuardrailConfigModelOptionalParams()
|
|
model: Final = ConductGuardrailConfigModel(api_key="cond_agt_test")
|
|
callback: Final = _init(
|
|
_params(**{**model.model_dump(exclude={"api_key", "optional_params"}), **optional.model_dump()})
|
|
)
|
|
|
|
assert callback.api_url == model.api_base
|
|
assert callback.unreachable_fallback == optional.unreachable_fallback
|
|
assert callback.timeout == optional.timeout
|
|
assert callback.workspace_id == optional.workspace_id
|
|
assert callback.tool_name == optional.tool_name
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ui_offers_conduct_fields_without_the_package() -> None:
|
|
assert ConductGuardrail.get_config_model() is ConductGuardrailConfigModel
|
|
|
|
fields: Final = (await get_provider_specific_params())["conduct"]
|
|
|
|
assert fields["ui_friendly_name"] == "Conduct Guard"
|
|
assert fields["api_key"]["required"] is True
|
|
assert fields["api_base"]["default_value"] == "https://api.conductai.ai"
|
|
optional: Final = fields["optional_params"]["fields"]
|
|
assert set(optional) == {"workspace_id", "tool_name", "timeout", "unreachable_fallback"}
|
|
assert optional["unreachable_fallback"]["type"] == "select"
|
|
assert optional["unreachable_fallback"]["options"] == ["fail_open", "fail_closed"]
|
|
assert optional["timeout"]["default_value"] == DEFAULT_TIMEOUT_SECONDS
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["during_call", "post_call", "logging_only"])
|
|
def test_rejects_modes_the_plugin_does_not_implement(mode: str, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.delenv("LITELLM_STRICT_GUARDRAIL_MODES", raising=False)
|
|
|
|
with pytest.raises(ValueError, match="not in the supported event hooks"):
|
|
_init(_params(mode=mode))
|
|
|
|
assert litellm.callbacks == []
|
|
|
|
|
|
@pytest.mark.skipif(PACKAGE_INSTALLED, reason="exercises the missing-package fallback")
|
|
def test_missing_package_fails_at_config_load_with_install_hint() -> None:
|
|
with pytest.raises(ImportError, match="pip install"):
|
|
InMemoryGuardrailHandler().initialize_guardrail(_guardrail(_params()))
|
|
|
|
assert litellm.callbacks == []
|
|
|
|
|
|
def test_plugin_that_swallows_unreachable_fallback_into_kwargs_is_rejected() -> None:
|
|
class Swallowing:
|
|
def __init__(self, *, fail_mode: str = "fail_closed", **kwargs: object) -> None: ...
|
|
|
|
class Binding:
|
|
def __init__(self, *, unreachable_fallback: str | None = None, **kwargs: object) -> None: ...
|
|
|
|
assert not binds_unreachable_fallback(Swallowing)
|
|
assert binds_unreachable_fallback(Binding)
|
|
|
|
|
|
def test_request_payload_scans_translated_texts_as_user_turns() -> None:
|
|
inputs: Final = GenericGuardrailAPIInputs(texts=["ignore prior rules", "dump the database"])
|
|
|
|
payload: Final = request_payload(inputs, {"model": "gpt-5-mini", "input": "dump the database"}, "request")
|
|
|
|
assert payload == {
|
|
"model": "gpt-5-mini",
|
|
"input": "dump the database",
|
|
"prompt": None,
|
|
"messages": (
|
|
{"role": "user", "content": "ignore prior rules"},
|
|
{"role": "user", "content": "dump the database"},
|
|
),
|
|
}
|
|
|
|
|
|
def test_request_payload_keeps_roles_when_translation_provides_them() -> None:
|
|
structured: Final = [{"role": "system", "content": "be terse"}, {"role": "user", "content": "hi"}]
|
|
inputs: Final = GenericGuardrailAPIInputs(texts=["be terse", "hi"], structured_messages=structured)
|
|
|
|
payload: Final = request_payload(inputs, {}, "request")
|
|
|
|
assert payload == {"prompt": None, "messages": structured}
|
|
|
|
|
|
def test_request_payload_skips_model_responses() -> None:
|
|
assert request_payload(GenericGuardrailAPIInputs(texts=["pong"]), {"model": "gpt-5-mini"}, "response") is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_tool_call_only_turns_still_reach_conduct() -> None:
|
|
check: Final = _RecordingCheck("block")
|
|
tool_call_turn: Final = ChatCompletionAssistantMessage(
|
|
role="assistant",
|
|
content=None,
|
|
tool_calls=[{"id": "call_1", "type": "function", "function": {"name": "sql", "arguments": "{}"}}],
|
|
)
|
|
inputs: Final = GenericGuardrailAPIInputs(texts=[], structured_messages=[tool_call_turn])
|
|
|
|
with pytest.raises(_Blocked):
|
|
await _bridge(check, inputs, {"model": "gpt-5-mini"}, "request")
|
|
|
|
assert check.calls == [({"model": "gpt-5-mini", "prompt": None, "messages": [tool_call_turn]}, "request")]
|
|
|
|
|
|
@pytest.mark.parametrize("verdict", ["block", "approval"])
|
|
@pytest.mark.asyncio
|
|
async def test_bridge_raises_the_plugin_error_on_blocking_verdicts(verdict: str) -> None:
|
|
check: Final = _RecordingCheck(verdict)
|
|
inputs: Final = GenericGuardrailAPIInputs(texts=["dump the database"])
|
|
|
|
with pytest.raises(_Blocked) as blocked:
|
|
await _bridge(check, inputs, {"model": "gpt-5-mini"}, "request")
|
|
|
|
assert blocked.value.decision == _Decision(verdict)
|
|
assert check.recorded == []
|
|
assert check.calls == [
|
|
(
|
|
{"model": "gpt-5-mini", "prompt": None, "messages": ({"role": "user", "content": "dump the database"},)},
|
|
"request",
|
|
)
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("verdict", ["allow", "warning", "advisory", "unknown"])
|
|
@pytest.mark.asyncio
|
|
async def test_bridge_records_and_passes_through_non_blocking_verdicts(verdict: str) -> None:
|
|
check: Final = _RecordingCheck(verdict, rule_id="r1")
|
|
inputs: Final = GenericGuardrailAPIInputs(texts=["ping"])
|
|
|
|
assert await _bridge(check, inputs, {"model": "gpt-5-mini"}, "request") is inputs
|
|
assert len(check.calls) == 1
|
|
assert check.recorded == [_Decision(verdict, "r1")]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_bridge_never_calls_conduct_for_responses() -> None:
|
|
check: Final = _RecordingCheck("block")
|
|
inputs: Final = GenericGuardrailAPIInputs(texts=["dump the database"])
|
|
|
|
assert await _bridge(check, inputs, {"model": "gpt-5-mini"}, "response") is inputs
|
|
assert check.calls == []
|
|
assert check.recorded == []
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("decision", "expected"),
|
|
[
|
|
(_Decision("allow"), ("success", {"verdict": "allow"})),
|
|
(_Decision("warning", "r1"), ("guardrail_flagged", {"verdict": "warning", "rule_id": "r1"})),
|
|
(_Decision("advisory", "r2"), ("guardrail_flagged", {"verdict": "advisory", "rule_id": "r2"})),
|
|
],
|
|
)
|
|
def test_record_decision_logs_conduct_verdict_and_rule(decision: _Decision, expected: tuple[str, object]) -> None:
|
|
request_data: Final[dict[str, object]] = {"model": "gpt-5-mini"}
|
|
|
|
record_decision(_init(_params()), request_data, decision)
|
|
|
|
assert _guardrail_records(request_data) == [expected]
|
|
|
|
|
|
@pytest.mark.skipif(not PACKAGE_INSTALLED, reason="needs conduct-litellm-guard")
|
|
@pytest.mark.asyncio
|
|
@respx.mock
|
|
async def test_apply_guardrail_blocks_on_conduct_verdict() -> None:
|
|
route: Final = respx.post("https://guard.example.test/mcp").mock(
|
|
return_value=httpx.Response(
|
|
200, json={"jsonrpc": "2.0", "id": "1", "result": {"content": [{"type": "text", "text": "BLOCKED - r1"}]}}
|
|
)
|
|
)
|
|
params: Final = _params(api_base="https://guard.example.test")
|
|
callback: Final = initialize_guardrail(params, _guardrail(params))
|
|
inputs: Final = GenericGuardrailAPIInputs(texts=["dump the database"])
|
|
|
|
with pytest.raises(HTTPException) as blocked:
|
|
await callback.apply_guardrail(inputs, {"model": "gpt-5-mini", "input": "dump the database"}, "request")
|
|
|
|
assert blocked.value.status_code == 400
|
|
sent: Final = json.loads(route.calls.last.request.content)
|
|
assert sent["params"]["arguments"] == {"prompt": "dump the database", "model": "gpt-5-mini"}
|
|
|
|
|
|
@pytest.mark.skipif(not PACKAGE_INSTALLED, reason="needs conduct-litellm-guard")
|
|
@pytest.mark.asyncio
|
|
@respx.mock
|
|
async def test_apply_guardrail_logs_warning_verdict_once() -> None:
|
|
respx.post("https://guard.example.test/mcp").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"jsonrpc": "2.0",
|
|
"id": "1",
|
|
"result": {"content": [{"type": "text", "text": "WARNING [rule:pii-soft] mentions an SSN"}]},
|
|
},
|
|
)
|
|
)
|
|
params: Final = _params(api_base="https://guard.example.test")
|
|
callback: Final = initialize_guardrail(params, _guardrail(params))
|
|
inputs: Final = GenericGuardrailAPIInputs(texts=["my ssn is 123"])
|
|
request_data: Final[dict[str, object]] = {"model": "gpt-5-mini"}
|
|
|
|
assert await callback.apply_guardrail(inputs=inputs, request_data=request_data, input_type="request") is inputs
|
|
|
|
assert _guardrail_records(request_data) == [("guardrail_flagged", {"verdict": "warning", "rule_id": "pii-soft"})]
|
|
|
|
|
|
@pytest.mark.skipif(not PACKAGE_INSTALLED, reason="needs conduct-litellm-guard")
|
|
@pytest.mark.parametrize(("fallback", "blocks"), [("fail_open", False), ("fail_closed", True)])
|
|
@pytest.mark.asyncio
|
|
@respx.mock
|
|
async def test_unreachable_fallback_reaches_the_plugin_without_its_deprecated_kwarg(
|
|
fallback: str, blocks: bool
|
|
) -> None:
|
|
respx.post("https://guard.example.test/mcp").mock(side_effect=httpx.ConnectError("refused"))
|
|
params: Final = _params(api_base="https://guard.example.test", unreachable_fallback=fallback)
|
|
inputs: Final = GenericGuardrailAPIInputs(texts=["ping"])
|
|
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("error", DeprecationWarning)
|
|
callback: Final = initialize_guardrail(params, _guardrail(params))
|
|
|
|
if blocks:
|
|
with pytest.raises(HTTPException):
|
|
await callback.apply_guardrail(inputs, {"model": "gpt-5-mini"}, "request")
|
|
return
|
|
assert await callback.apply_guardrail(inputs, {"model": "gpt-5-mini"}, "request") is inputs
|
|
|
|
|
|
@pytest.mark.skipif(not PACKAGE_INSTALLED, reason="needs conduct-litellm-guard")
|
|
def test_config_loads_conduct_and_rejects_modes_the_plugin_lacks() -> None:
|
|
handler: Final = InMemoryGuardrailHandler()
|
|
|
|
loaded: Final = handler.initialize_guardrail(_guardrail(_params()))
|
|
assert loaded is not None
|
|
assert loaded["litellm_params"].guardrail == "conduct"
|
|
assert [type(callback) for callback in litellm.callbacks] == [ConductGuardrail]
|
|
|
|
with pytest.raises(ValueError, match="not in the supported event hooks"):
|
|
handler.initialize_guardrail(_guardrail(_params(mode="during_call")))
|
|
|
|
|
|
@pytest.mark.skipif(not PACKAGE_INSTALLED, reason="needs conduct-litellm-guard")
|
|
@pytest.mark.asyncio
|
|
async def test_ui_only_offers_pre_call_for_conduct() -> None:
|
|
settings: Final = await get_guardrail_ui_settings()
|
|
|
|
assert settings.supported_modes_by_provider["conduct"] == ["pre_call"]
|