Skip to content

Commit d15c2d5

Browse files
Harshit28jclaudegreptile-apps[bot]
authored
fix: Register DynamoAI guardrail initializer and enum entry (#23752)
* fix: Register DynamoAI guardrail initializer and enum entry Fix the "Unsupported guardrail: dynamoai" error by: 1. Adding DYNAMOAI to SupportedGuardrailIntegrations enum 2. Implementing initialize_guardrail() and registries in dynamoai/__init__.py The DynamoAI guardrail was added in PR #15920 but never properly registered in the initialization system. The __init__.py was missing the guardrail_initializer_registry and guardrail_class_registry dictionaries that the dynamic discovery mechanism looks for at module load time. Fixes #22773 Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com> * Update litellm/proxy/guardrails/guardrail_hooks/dynamoai/__init__.py Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * Update litellm/proxy/guardrails/guardrail_hooks/dynamoai/__init__.py Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * test: Add tests for DynamoAI guardrail registration Verifies enum entry, initializer registry, class registry, instance creation, and global registry discovery. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 278c9ba commit d15c2d5

3 files changed

Lines changed: 113 additions & 1 deletion

File tree

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
from typing import TYPE_CHECKING
2+
3+
from litellm.types.guardrails import SupportedGuardrailIntegrations
4+
15
from .dynamoai import DynamoAIGuardrails
26

3-
__all__ = ["DynamoAIGuardrails"]
7+
if TYPE_CHECKING:
8+
from litellm.types.guardrails import Guardrail, LitellmParams
9+
10+
11+
def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail"):
12+
import litellm
13+
14+
_dynamoai_callback = DynamoAIGuardrails(
15+
api_base=litellm_params.api_base,
16+
api_key=litellm_params.api_key,
17+
guardrail_name=guardrail.get("guardrail_name", ""),
18+
event_hook=litellm_params.mode,
19+
default_on=litellm_params.default_on,
20+
)
21+
litellm.logging_callback_manager.add_litellm_callback(_dynamoai_callback)
22+
23+
return _dynamoai_callback
24+
25+
26+
guardrail_initializer_registry = {
27+
SupportedGuardrailIntegrations.DYNAMOAI.value: initialize_guardrail,
28+
}
29+
30+
31+
guardrail_class_registry = {
32+
SupportedGuardrailIntegrations.DYNAMOAI.value: DynamoAIGuardrails,
33+
}

litellm/types/guardrails.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
class SupportedGuardrailIntegrations(Enum):
4545
APORIA = "aporia"
4646
BEDROCK = "bedrock"
47+
DYNAMOAI = "dynamoai"
4748
GUARDRAILS_AI = "guardrails_ai"
4849
LAKERA = "lakera"
4950
LAKERA_V2 = "lakera_v2"
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
Tests for DynamoAI guardrail registration and initialization.
3+
"""
4+
5+
import os
6+
from unittest.mock import patch
7+
8+
import pytest
9+
10+
11+
class TestDynamoAIGuardrailRegistration:
12+
"""Tests for DynamoAI guardrail registration in the guardrail system."""
13+
14+
def test_supported_guardrail_enum_entry(self):
15+
"""Test that DYNAMOAI is in SupportedGuardrailIntegrations enum."""
16+
from litellm.types.guardrails import SupportedGuardrailIntegrations
17+
18+
assert hasattr(SupportedGuardrailIntegrations, "DYNAMOAI")
19+
assert SupportedGuardrailIntegrations.DYNAMOAI.value == "dynamoai"
20+
21+
def test_initialize_guardrail_function_exists(self):
22+
"""Test that initialize_guardrail function is properly exported."""
23+
from litellm.proxy.guardrails.guardrail_hooks.dynamoai import (
24+
guardrail_initializer_registry,
25+
initialize_guardrail,
26+
)
27+
28+
assert initialize_guardrail is not None
29+
assert "dynamoai" in guardrail_initializer_registry
30+
31+
def test_guardrail_class_registry_exists(self):
32+
"""Test that guardrail_class_registry is properly exported."""
33+
from litellm.proxy.guardrails.guardrail_hooks.dynamoai import (
34+
guardrail_class_registry,
35+
)
36+
from litellm.proxy.guardrails.guardrail_hooks.dynamoai.dynamoai import (
37+
DynamoAIGuardrails,
38+
)
39+
40+
assert "dynamoai" in guardrail_class_registry
41+
assert guardrail_class_registry["dynamoai"] == DynamoAIGuardrails
42+
43+
def test_initialize_guardrail_creates_instance(self):
44+
"""Test that initialize_guardrail creates a DynamoAIGuardrails instance."""
45+
from litellm.proxy.guardrails.guardrail_hooks.dynamoai import (
46+
initialize_guardrail,
47+
)
48+
from litellm.proxy.guardrails.guardrail_hooks.dynamoai.dynamoai import (
49+
DynamoAIGuardrails,
50+
)
51+
from litellm.types.guardrails import LitellmParams
52+
53+
litellm_params = LitellmParams(
54+
guardrail="dynamoai",
55+
mode="pre_call",
56+
api_key="test-key",
57+
api_base="https://test.dynamo.ai",
58+
)
59+
60+
guardrail = {
61+
"guardrail_name": "test-dynamoai-guard",
62+
}
63+
64+
with patch(
65+
"litellm.logging_callback_manager.add_litellm_callback"
66+
) as mock_add:
67+
result = initialize_guardrail(litellm_params, guardrail)
68+
69+
assert isinstance(result, DynamoAIGuardrails)
70+
assert result.api_key == "test-key"
71+
assert result.api_base == "https://test.dynamo.ai"
72+
assert result.guardrail_name == "test-dynamoai-guard"
73+
mock_add.assert_called_once_with(result)
74+
75+
def test_dynamoai_in_global_registry(self):
76+
"""Test that dynamoai is discoverable in the global guardrail registry."""
77+
from litellm.proxy.guardrails.guardrail_registry import (
78+
guardrail_initializer_registry,
79+
)
80+
81+
assert "dynamoai" in guardrail_initializer_registry

0 commit comments

Comments
 (0)