diff --git a/litellm/proxy/management_endpoints/fallback_management_endpoints.py b/litellm/proxy/management_endpoints/fallback_management_endpoints.py index 821354cda31..b39afeea86b 100644 --- a/litellm/proxy/management_endpoints/fallback_management_endpoints.py +++ b/litellm/proxy/management_endpoints/fallback_management_endpoints.py @@ -104,7 +104,7 @@ def _checked_content_policy_rules( return tuple((candidate, _checked_content_policy_rule(candidate)) for candidate in candidates) -def apply_content_policy_fallbacks(llm_router: "Router", rules: object) -> None: +def apply_content_policy_fallbacks(llm_router: "Router", rules: object, *, log_skipped_rules: bool) -> None: checked_rules: Final = _checked_content_policy_rules(rules) runtime_rules: Final = tuple(result for _, result in checked_rules if not isinstance(result, ValidationError)) layered_rules: Final = _layered_over_config_rules( @@ -113,17 +113,17 @@ def apply_content_policy_fallbacks(llm_router: "Router", rules: object) -> None: litellm.content_policy_fallbacks # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # untyped module list ), ) + for rule, result in checked_rules: + if log_skipped_rules and isinstance(result, ValidationError): + _log_ignored_content_policy_rules(rule, result) if layered_rules == llm_router.settings().content_policy_fallbacks: # pyright: ignore[reportUnknownMemberType] # untyped RouterSettings list return - for rule, result in checked_rules: - if isinstance(result, ValidationError): - _log_ignored_content_policy_rules(rule, result) llm_router.apply(ChangeSet(settings={"content_policy_fallbacks": layered_rules})) def _apply_fallback_rules(llm_router: "Router", fallback_key: str, rules: Sequence[StoredFallback]) -> None: if fallback_key == "content_policy_fallbacks": - apply_content_policy_fallbacks(llm_router, rules) + apply_content_policy_fallbacks(llm_router, rules, log_skipped_rules=True) return llm_router.apply(ChangeSet(assign={fallback_key: rules})) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 3a34ca11d09..dc133e66a4d 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -8159,6 +8159,7 @@ class ProxyConfig: if db_router_settings is not None and db_router_settings.param_value is not None else _EMPTY_SETTINGS_MAPPING ) + previous_content_policy_rules: Final = self.router_settings.resolved().get("content_policy_fallbacks") self.router_settings.apply_db_row("router_settings", db_values) combined_router_settings: Final = self.router_settings.resolved() if combined_router_settings: @@ -8166,11 +8167,18 @@ class ProxyConfig: llm_router, combined_router_settings, content_policy_owned_by_config=self.router_settings.owned_by_config("content_policy_fallbacks"), + content_policy_rules_changed=( + combined_router_settings.get("content_policy_fallbacks") != previous_content_policy_rules + ), ) @staticmethod def _apply_router_settings( - llm_router: Router, router_settings: Mapping[str, object], *, content_policy_owned_by_config: bool + llm_router: Router, + router_settings: Mapping[str, object], + *, + content_policy_owned_by_config: bool, + content_policy_rules_changed: bool, ) -> None: llm_router.apply( ChangeSet( @@ -8180,7 +8188,9 @@ class ProxyConfig: ) ) if "content_policy_fallbacks" in router_settings and not content_policy_owned_by_config: - apply_content_policy_fallbacks(llm_router, router_settings["content_policy_fallbacks"]) + apply_content_policy_fallbacks( + llm_router, router_settings["content_policy_fallbacks"], log_skipped_rules=content_policy_rules_changed + ) if "routing_groups" not in router_settings: return try: diff --git a/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py b/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py index c77db89551d..1f679a94f37 100644 --- a/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py +++ b/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py @@ -5733,6 +5733,31 @@ async def test_an_invalid_db_content_policy_rule_is_logged_once_per_change_of_th assert await _answer_after_content_policy_refusal(router, "secondary") == "backup answered" +@pytest.mark.asyncio +async def test_a_db_sync_logs_a_new_invalid_content_policy_rule_when_no_valid_rule_changes( + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture +): + monkeypatch.setattr(litellm, "content_policy_fallbacks", []) + router: Final = _content_policy_router() + proxy_config: Final = proxy_server_module.ProxyConfig() + + with caplog.at_level(logging.ERROR, logger="LiteLLM Proxy"): + await proxy_config._add_router_settings_from_db_config( + llm_router=router, prisma_client=_router_settings_row([{"secondary": "backup"}]) + ) + await proxy_config._add_router_settings_from_db_config( + llm_router=router, prisma_client=_router_settings_row([{"secondary": "backup"}, {"primary": "backup"}]) + ) + + notices: Final = [ + record.getMessage() + for record in caplog.records + if "Ignoring invalid content_policy_fallbacks" in record.getMessage() + ] + assert len(notices) == 3 + assert "{'primary': 'backup'}" in notices[-1] + + @pytest.mark.asyncio async def test_config_file_content_policy_fallbacks_stay_the_only_rules_after_a_db_sync( monkeypatch: pytest.MonkeyPatch,