Skip to content

Commit 44e0c73

Browse files
Merge pull request #14653 from ARajan1084/in-memory-guardrail-fix
fix: In Memory Guardrail fails to update
2 parents 8b80430 + 268b984 commit 44e0c73

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

litellm/integrations/custom_guardrail.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@ def update_in_memory_litellm_params(self, litellm_params: LitellmParams) -> None
487487
"""
488488
Update the guardrails litellm params in memory
489489
"""
490-
pass
490+
for key, value in vars(litellm_params).items():
491+
setattr(self, key, value)
491492

492493

493494
def log_guardrail_information(func):

litellm/proxy/guardrails/guardrail_hooks/presidio.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,5 +675,6 @@ def update_in_memory_litellm_params(self, litellm_params: LitellmParams) -> None
675675
"""
676676
Update the guardrails litellm params in memory
677677
"""
678+
super().update_in_memory_litellm_params(litellm_params)
678679
if litellm_params.pii_entities_config:
679680
self.pii_entities_config = litellm_params.pii_entities_config

tests/test_litellm/proxy/guardrails/test_guardrail_registry.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from litellm.integrations.custom_guardrail import CustomGuardrail
12
from litellm.proxy.guardrails.guardrail_registry import (
23
get_guardrail_initializer_from_hooks,
4+
InMemoryGuardrailHandler,
35
)
6+
from litellm.types.guardrails import GuardrailEventHooks, Guardrail, LitellmParams
47

58

69
def test_get_guardrail_initializer_from_hooks():
@@ -15,3 +18,33 @@ def test_guardrail_class_registry():
1518
print(f"guardrail_class_registry: {guardrail_class_registry}")
1619
assert "aim" in guardrail_class_registry
1720
assert "aporia" in guardrail_class_registry
21+
22+
23+
def test_update_in_memory_guardrail():
24+
handler = InMemoryGuardrailHandler()
25+
handler.guardrail_id_to_custom_guardrail["123"] = CustomGuardrail(
26+
guardrail_name="test-guardrail",
27+
default_on=False,
28+
event_hook=GuardrailEventHooks.pre_call,
29+
)
30+
31+
handler.update_in_memory_guardrail(
32+
"123",
33+
Guardrail(
34+
guardrail_name="test-guardrail",
35+
litellm_params=LitellmParams(
36+
guardrail="test-guardrail", mode="pre_call", default_on=True
37+
),
38+
),
39+
)
40+
41+
assert (
42+
handler.guardrail_id_to_custom_guardrail["123"].should_run_guardrail(
43+
data={}, event_type=GuardrailEventHooks.pre_call
44+
)
45+
is True
46+
)
47+
assert (
48+
handler.guardrail_id_to_custom_guardrail["123"].event_hook
49+
is GuardrailEventHooks.pre_call
50+
)

0 commit comments

Comments
 (0)