-
-
Notifications
You must be signed in to change notification settings - Fork 7k
fix: Register DynamoAI guardrail initializer and enum entry #23752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+113
−1
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a02962c
fix: Register DynamoAI guardrail initializer and enum entry
Harshit28j 62ab632
Update litellm/proxy/guardrails/guardrail_hooks/dynamoai/__init__.py
Harshit28j 4b29152
Update litellm/proxy/guardrails/guardrail_hooks/dynamoai/__init__.py
Harshit28j ed213ba
test: Add tests for DynamoAI guardrail registration
Harshit28j File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
32 changes: 31 additions & 1 deletion
32
litellm/proxy/guardrails/guardrail_hooks/dynamoai/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,33 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from litellm.types.guardrails import SupportedGuardrailIntegrations | ||
|
|
||
| from .dynamoai import DynamoAIGuardrails | ||
|
|
||
| __all__ = ["DynamoAIGuardrails"] | ||
| if TYPE_CHECKING: | ||
| from litellm.types.guardrails import Guardrail, LitellmParams | ||
|
|
||
|
|
||
| def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail"): | ||
| import litellm | ||
|
|
||
| _dynamoai_callback = DynamoAIGuardrails( | ||
| api_base=litellm_params.api_base, | ||
| api_key=litellm_params.api_key, | ||
| guardrail_name=guardrail.get("guardrail_name", ""), | ||
| event_hook=litellm_params.mode, | ||
| default_on=litellm_params.default_on, | ||
| ) | ||
| litellm.logging_callback_manager.add_litellm_callback(_dynamoai_callback) | ||
|
|
||
| return _dynamoai_callback | ||
|
|
||
|
|
||
| guardrail_initializer_registry = { | ||
| SupportedGuardrailIntegrations.DYNAMOAI.value: initialize_guardrail, | ||
| } | ||
|
|
||
|
|
||
| guardrail_class_registry = { | ||
| SupportedGuardrailIntegrations.DYNAMOAI.value: DynamoAIGuardrails, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
tests/test_litellm/proxy/guardrails/guardrail_hooks/test_dynamoai.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| """ | ||
| Tests for DynamoAI guardrail registration and initialization. | ||
| """ | ||
|
|
||
| import os | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
Harshit28j marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class TestDynamoAIGuardrailRegistration: | ||
| """Tests for DynamoAI guardrail registration in the guardrail system.""" | ||
|
|
||
| def test_supported_guardrail_enum_entry(self): | ||
| """Test that DYNAMOAI is in SupportedGuardrailIntegrations enum.""" | ||
| from litellm.types.guardrails import SupportedGuardrailIntegrations | ||
|
|
||
| assert hasattr(SupportedGuardrailIntegrations, "DYNAMOAI") | ||
| assert SupportedGuardrailIntegrations.DYNAMOAI.value == "dynamoai" | ||
|
|
||
| def test_initialize_guardrail_function_exists(self): | ||
| """Test that initialize_guardrail function is properly exported.""" | ||
| from litellm.proxy.guardrails.guardrail_hooks.dynamoai import ( | ||
| guardrail_initializer_registry, | ||
| initialize_guardrail, | ||
| ) | ||
|
|
||
| assert initialize_guardrail is not None | ||
| assert "dynamoai" in guardrail_initializer_registry | ||
|
|
||
| def test_guardrail_class_registry_exists(self): | ||
| """Test that guardrail_class_registry is properly exported.""" | ||
| from litellm.proxy.guardrails.guardrail_hooks.dynamoai import ( | ||
| guardrail_class_registry, | ||
| ) | ||
| from litellm.proxy.guardrails.guardrail_hooks.dynamoai.dynamoai import ( | ||
| DynamoAIGuardrails, | ||
| ) | ||
|
|
||
| assert "dynamoai" in guardrail_class_registry | ||
| assert guardrail_class_registry["dynamoai"] == DynamoAIGuardrails | ||
|
|
||
| def test_initialize_guardrail_creates_instance(self): | ||
| """Test that initialize_guardrail creates a DynamoAIGuardrails instance.""" | ||
| from litellm.proxy.guardrails.guardrail_hooks.dynamoai import ( | ||
| initialize_guardrail, | ||
| ) | ||
| from litellm.proxy.guardrails.guardrail_hooks.dynamoai.dynamoai import ( | ||
| DynamoAIGuardrails, | ||
| ) | ||
| from litellm.types.guardrails import LitellmParams | ||
|
|
||
| litellm_params = LitellmParams( | ||
| guardrail="dynamoai", | ||
| mode="pre_call", | ||
| api_key="test-key", | ||
| api_base="https://test.dynamo.ai", | ||
| ) | ||
|
|
||
| guardrail = { | ||
| "guardrail_name": "test-dynamoai-guard", | ||
| } | ||
|
|
||
| with patch( | ||
| "litellm.logging_callback_manager.add_litellm_callback" | ||
| ) as mock_add: | ||
| result = initialize_guardrail(litellm_params, guardrail) | ||
|
|
||
| assert isinstance(result, DynamoAIGuardrails) | ||
| assert result.api_key == "test-key" | ||
| assert result.api_base == "https://test.dynamo.ai" | ||
| assert result.guardrail_name == "test-dynamoai-guard" | ||
| mock_add.assert_called_once_with(result) | ||
|
|
||
| def test_dynamoai_in_global_registry(self): | ||
| """Test that dynamoai is discoverable in the global guardrail registry.""" | ||
| from litellm.proxy.guardrails.guardrail_registry import ( | ||
| guardrail_initializer_registry, | ||
| ) | ||
|
|
||
| assert "dynamoai" in guardrail_initializer_registry | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No test covering the new registration
The PR fixes the "Unsupported guardrail: dynamoai" error by wiring the
guardrail_initializer_registryandguardrail_class_registry, but there is no new test asserting that"dynamoai"is present in either registry. The existingtest_guardrail_registry.pyasserts presence of"aim","aporia","noma", etc., but does not cover"dynamoai".Adding a focused assertion (similar to
test_guardrail_class_registry) would serve as direct evidence that the bug is resolved:This mirrors what already exists for
nomaandnoma_v2intest_noma_registry_resolution.Rule Used: What: Ensure that any PR claiming to fix an issue ... (source)