Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(guardrails): refuse scan_only_tool_results combos that scan nothing

mateo-berri14d4897e55a8
diff --git a/litellm/proxy/guardrails/guardrail_hooks/prompt_security/prompt_security.py b/litellm/proxy/guardrails/guardrail_hooks/prompt_security/prompt_security.py
index 743ad888949..1a2c46f306c 100644
--- a/litellm/proxy/guardrails/guardrail_hooks/prompt_security/prompt_security.py
+++ b/litellm/proxy/guardrails/guardrail_hooks/prompt_security/prompt_security.py
@@ -74,6 +74,9 @@ class PromptSecurityGuardrail(CustomGuardrail):
super().__init__(**kwargs)
+ def supports_scan_only_tool_results(self) -> bool:
+ return self.check_tool_results
+
@log_guardrail_information
async def apply_guardrail(
self,
diff --git a/litellm/proxy/guardrails/guardrail_registry.py b/litellm/proxy/guardrails/guardrail_registry.py
index 15e884c939c..9f70ed63dcb 100644
--- a/litellm/proxy/guardrails/guardrail_registry.py
+++ b/litellm/proxy/guardrails/guardrail_registry.py
@@ -16,6 +16,7 @@ from litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.litellm_core_utils.safe_json_dumps import safe_dumps
from litellm.llms.base_llm.guardrail_translation.utils import (
effective_scan_only_tool_results_for_guardrail,
+ effective_skip_tool_message_for_guardrail,
)
from litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails import (
BedrockGuardrail,
@@ -496,15 +497,21 @@ class InMemoryGuardrailHandler:
"scan_only_tool_results",
):
setattr(custom_guardrail_callback, scoping_param, getattr(litellm_params, scoping_param, None))
- if (
- effective_scan_only_tool_results_for_guardrail(custom_guardrail_callback)
- and not custom_guardrail_callback.supports_scan_only_tool_results()
- ):
+ scan_only_tool_results_enabled: Final = effective_scan_only_tool_results_for_guardrail(
+ custom_guardrail_callback
+ )
+ if scan_only_tool_results_enabled and not custom_guardrail_callback.supports_scan_only_tool_results():
raise ValueError(
f"Guardrail {guardrail['guardrail_name']}: scan_only_tool_results is enabled, but this "
"guardrail's role filtering never scans tool results, so no request content would ever "
"be scanned. Remove scan_only_tool_results or the guardrail's role-filtering option."
)
+ if scan_only_tool_results_enabled and effective_skip_tool_message_for_guardrail(custom_guardrail_callback):
+ raise ValueError(
+ f"Guardrail {guardrail['guardrail_name']}: scan_only_tool_results and "
+ "skip_tool_message_in_guardrail are enabled together, which excludes every message from "
+ "scanning, so no request content would ever be scanned. Remove one of the two."
+ )
configured_run_in_parallel: Final = getattr(litellm_params, "run_in_parallel", None)
if configured_run_in_parallel is not None:
custom_guardrail_callback.run_in_parallel = bool(configured_run_in_parallel)
diff --git a/tests/test_litellm/proxy/guardrails/test_guardrail_registry.py b/tests/test_litellm/proxy/guardrails/test_guardrail_registry.py
index 3053664ef27..729dbce6b9a 100644
--- a/tests/test_litellm/proxy/guardrails/test_guardrail_registry.py
+++ b/tests/test_litellm/proxy/guardrails/test_guardrail_registry.py
@@ -615,3 +615,45 @@ class TestScanOnlyToolResultsInitRefusal:
},
)
assert result is not None
+
+ def test_prompt_security_default_tool_filtering_rejects_scan_only_tool_results(self, monkeypatch):
+ monkeypatch.delenv("PROMPT_SECURITY_CHECK_TOOL_RESULTS", raising=False)
+ with pytest.raises(ValueError, match="never scans tool results"):
+ self._initialize(
+ "prompt-security-scan-only-combo",
+ {
+ "guardrail": "prompt_security",
+ "mode": "pre_call",
+ "api_key": "test-key",
+ "api_base": "https://ps.example.com",
+ "scan_only_tool_results": True,
+ },
+ )
+
+ def test_prompt_security_check_tool_results_accepts_scan_only_tool_results(self, monkeypatch):
+ monkeypatch.setenv("PROMPT_SECURITY_CHECK_TOOL_RESULTS", "true")
+ result = self._initialize(
+ "prompt-security-scan-only-ok",
+ {
+ "guardrail": "prompt_security",
+ "mode": "pre_call",
+ "api_key": "test-key",
+ "api_base": "https://ps.example.com",
+ "scan_only_tool_results": True,
+ },
+ )
+ assert result is not None
+
+ def test_skip_tool_message_with_scan_only_tool_results_is_rejected(self):
+ with pytest.raises(ValueError, match="skip_tool_message_in_guardrail are enabled together"):
+ self._initialize(
+ "bedrock-skip-tool-scan-only-combo",
+ {
+ "guardrail": "bedrock",
+ "mode": "pre_call",
+ "guardrailIdentifier": "gr-1",
+ "guardrailVersion": "1",
+ "skip_tool_message_in_guardrail": True,
+ "scan_only_tool_results": True,
+ },
+ )