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]

feat: guardrail-mode-default-list

Harshit28j22e682b1e89c
diff --git a/docs/my-website/docs/proxy/guardrails/quick_start.md b/docs/my-website/docs/proxy/guardrails/quick_start.md
index e5a90f74a8a..eb56c27f876 100644
--- a/docs/my-website/docs/proxy/guardrails/quick_start.md
+++ b/docs/my-website/docs/proxy/guardrails/quick_start.md
@@ -499,6 +499,11 @@ curl --location 'http://0.0.0.0:4000/chat/completions' \
Run guardrails based on the user-agent header. This is useful for running pre-call checks on OpenWebUI but only masking in logs for Claude CLI.
+`default` can be a single mode string or a list of modes.
+
+<Tabs>
+<TabItem value="single" label="Single Default Mode">
+
```yaml
model_list:
- model_name: gpt-3.5-turbo
@@ -519,6 +524,32 @@ guardrails:
default_on: true # run on every request
```
+</TabItem>
+<TabItem value="multi" label="Multiple Default Modes">
+
+```yaml
+model_list:
+ - model_name: gpt-3.5-turbo
+ litellm_params:
+ model: gpt-3.5-turbo
+ api_key: os.environ/OPENAI_API_KEY
+
+guardrails:
+ - guardrail_name: "guardrails_ai-guard"
+ litellm_params:
+ guardrail: guardrails_ai
+ guard_name: "pii_detect"
+ mode:
+ tags:
+ "User-Agent: claude-cli": "logging_only"
+ default: ["pre_call", "post_call"] # Run on both pre and post call when no tags match
+ api_base: os.environ/GUARDRAILS_AI_API_BASE
+ default_on: true
+```
+
+</TabItem>
+</Tabs>
+
### ✨ Model-level Guardrails
@@ -640,13 +671,22 @@ guardrails:
Mode Specification
+`default` accepts either a single string or a list of strings.
+
```python
from litellm.types.guardrails import Mode
+# Single default mode
mode = Mode(
tags={"User-Agent: claude-cli": "logging_only"},
default="logging_only"
)
+
+# Multiple default modes
+mode = Mode(
+ tags={"User-Agent: claude-cli": "logging_only"},
+ default=["pre_call", "post_call"]
+)
```
### `guardrails` Request Parameter
diff --git a/litellm/integrations/custom_guardrail.py b/litellm/integrations/custom_guardrail.py
index 5d11fd68475..3fd179bb411 100644
--- a/litellm/integrations/custom_guardrail.py
+++ b/litellm/integrations/custom_guardrail.py
@@ -235,8 +235,13 @@ class CustomGuardrail(CustomLogger):
list(event_hook.tags.values()), supported_event_hooks
)
if event_hook.default:
+ default_list = (
+ event_hook.default
+ if isinstance(event_hook.default, list)
+ else [event_hook.default]
+ )
_validate_event_hook_list_is_in_supported_event_hooks(
- [event_hook.default], supported_event_hooks
+ default_list, supported_event_hooks
)
elif isinstance(event_hook, GuardrailEventHooks):
if event_hook not in supported_event_hooks:
@@ -461,7 +466,16 @@ class CustomGuardrail(CustomLogger):
if isinstance(self.event_hook, list):
return event_type.value in self.event_hook
if isinstance(self.event_hook, Mode):
- return event_type.value in self.event_hook.tags.values()
+ if event_type.value in self.event_hook.tags.values():
+ return True
+ if self.event_hook.default:
+ default_list = (
+ self.event_hook.default
+ if isinstance(self.event_hook.default, list)
+ else [self.event_hook.default]
+ )
+ return event_type.value in default_list
+ return False
return self.event_hook == event_type.value
def get_guardrail_dynamic_request_body_params(self, request_data: dict) -> dict:
diff --git a/litellm/types/guardrails.py b/litellm/types/guardrails.py
index a68c4e2f762..dc95ed3314a 100644
--- a/litellm/types/guardrails.py
+++ b/litellm/types/guardrails.py
@@ -718,7 +718,7 @@ class BaseLitellmParams(
class Mode(BaseModel):
tags: Dict[str, str] = Field(description="Tags for the guardrail mode")
- default: Optional[str] = Field(
+ default: Optional[Union[str, List[str]]] = Field(
default=None, description="Default mode when no tags match"
)