diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py index bcd0ef738c7..5f5d2d231b1 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py @@ -699,8 +699,16 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): chain_tag_filtering: Final[object] = ( _chain_tag_filtering_override(router, model, deployments) if isinstance(model, str) else None ) + router_settings_override: Final[object] = request_data.get("router_settings_override") + override_tag_filtering: Final[object] = ( + router_settings_override.get("enable_tag_filtering") + if isinstance(router_settings_override, Mapping) + else None + ) effective_tag_filtering: Final = ( - chain_tag_filtering + True + if override_tag_filtering is True + else chain_tag_filtering if isinstance(chain_tag_filtering, bool) else getattr(router, "enable_tag_filtering", False) ) @@ -757,6 +765,8 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): bool(header_strings) and has_regex_deployments and not required_set ) if not has_positive_filter: + if required_set or excluded_set: + return candidate_deployments default_deployments: Final = [ deployment for deployment in candidate_deployments if "default" in _deployment_tags(deployment) ] diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py index 30a899ab9ef..d70f7bbd94b 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py @@ -294,6 +294,61 @@ def test_bedrock_guardrail_ignores_client_tag_filtering_override(monkeypatch: py ) +def test_bedrock_guardrail_honors_router_settings_tag_filtering_override(monkeypatch: pytest.MonkeyPatch): + from litellm.proxy import proxy_server + + router = MagicMock() + router.enable_tag_filtering = False + router.get_model_list.return_value = [ + { + "litellm_params": {"custom_llm_provider": "openai", "tags": ["slow"]}, + "model_info": {}, + }, + { + "litellm_params": {"custom_llm_provider": "bedrock", "tags": ["fast"]}, + "model_info": {}, + }, + ] + monkeypatch.setattr(proxy_server, "llm_router", router) + + assert ( + BedrockGuardrail._get_bedrock_api_key( + { + "model": "shared-alias", + "api_key": "bedrock-key", + "router_settings_override": {"enable_tag_filtering": True}, + "metadata": {"tags": ["fast"]}, + } + ) + == "bedrock-key" + ) + + +def test_bedrock_guardrail_keeps_all_required_tag_matches(monkeypatch: pytest.MonkeyPatch): + from litellm.proxy import proxy_server + + router = MagicMock() + router.enable_tag_filtering = True + router.get_model_list.return_value = [ + { + "litellm_params": {"custom_llm_provider": "bedrock", "tags": ["required", "default"]}, + "model_info": {}, + }, + { + "litellm_params": {"custom_llm_provider": "openai", "tags": ["required"]}, + "model_info": {}, + }, + ] + monkeypatch.setattr(proxy_server, "llm_router", router) + + assert ( + BedrockGuardrail._router_allows_bedrock( + {"model": "shared-alias", "metadata": {"tags": ["&required"]}} + ) + is False + ) + + @pytest.mark.asyncio async def test_bedrock_guardrail_async_sync_strategy_uses_unfiltered_pool(monkeypatch: pytest.MonkeyPatch): from litellm.proxy import proxy_server