mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(proxy): log a skipped content-policy rule whenever the saved rules change
Some checks failed
Cost map pins recheck / rerun-cost-map-guard (push) Has been cancelled
LiteLLM Rust DB / db-queries (push) Has been cancelled
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled
Terraform Provider / gofmt, vet, build, test (push) Has been cancelled
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Has been cancelled
Some checks failed
Cost map pins recheck / rerun-cost-map-guard (push) Has been cancelled
LiteLLM Rust DB / db-queries (push) Has been cancelled
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled
Terraform Provider / gofmt, vet, build, test (push) Has been cancelled
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Has been cancelled
This commit is contained in:
parent
34be6d9dc7
commit
835b953587
3 changed files with 42 additions and 7 deletions
|
|
@ -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}))
|
||||
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue