fix: add case-insensitive support for guardrail mode and actions (#19480)

This commit is contained in:
Harshit Jain
2026-01-21 20:52:57 -08:00
committed by GitHub
parent 73d49f8d63
commit 22000f3beb
2 changed files with 22 additions and 19 deletions
+12 -17
View File
@@ -5,11 +5,6 @@ from typing import Any, Dict, List, Literal, Optional, Union
from pydantic import BaseModel, ConfigDict, Field, field_validator
from typing_extensions import Required, TypedDict
from litellm.types.llms.openai import (
AllMessageValues,
ChatCompletionToolCallChunk,
ChatCompletionToolParam,
)
from litellm.types.proxy.guardrails.guardrail_hooks.enkryptai import (
EnkryptAIGuardrailConfigs,
)
@@ -673,20 +668,20 @@ class LitellmParams(
description="When to apply the guardrail (pre_call, post_call, during_call, logging_only)"
)
@field_validator("default_action", mode="before", check_fields=False)
@field_validator(
"mode",
"default_action",
"on_disallowed_action",
mode="before",
check_fields=False,
)
@classmethod
def normalize_default_action_litellm_params(cls, v):
"""Normalize default_action to lowercase for ALL guardrail types."""
if isinstance(v, str):
return v.lower()
return v
@field_validator("on_disallowed_action", mode="before", check_fields=False)
@classmethod
def normalize_on_disallowed_action_litellm_params(cls, v):
"""Normalize on_disallowed_action to lowercase for ALL guardrail types."""
def normalize_lowercase(cls, v):
"""Normalize string and list fields to lowercase for ALL guardrail types."""
if isinstance(v, str):
return v.lower()
if isinstance(v, list):
return [x.lower() if isinstance(x, str) else x for x in v]
return v
def __init__(self, **kwargs):
@@ -695,7 +690,7 @@ class LitellmParams(
kwargs["default_on"] = default_on
else:
kwargs["default_on"] = False
super().__init__(**kwargs)
def __contains__(self, key):
@@ -2,8 +2,11 @@ import os
import sys
sys.path.insert(
0, os.path.abspath("../..")
) # Adds the parent directory to the system path
0,
os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../litellm-proxy-extras")
),
)
from litellm_proxy_extras.utils import ProxyExtrasDBManager
@@ -99,6 +102,11 @@ class TestIdempotentErrorDetection:
error_message = "COLUMN 'ID' ALREADY EXISTS"
assert ProxyExtrasDBManager._is_idempotent_error(error_message) is True
def test_is_idempotent_error_does_not_exist(self):
"""Test detection of 'does not exist' error"""
error_message = "ERROR: index 'idx' does not exist"
assert ProxyExtrasDBManager._is_idempotent_error(error_message) is True
def test_is_idempotent_error_negative(self):
"""Test that non-idempotent errors are not detected as idempotent errors"""
error_message = "Database error code: 42501 - permission denied"