Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(policy_engine): only hide config policies behind production DB versions in policies list

mateo-berri91290c60206e
diff --git a/litellm/proxy/policy_engine/policy_endpoints.py b/litellm/proxy/policy_engine/policy_endpoints.py
index 787f7069996..718223da5d8 100644
--- a/litellm/proxy/policy_engine/policy_endpoints.py
+++ b/litellm/proxy/policy_engine/policy_endpoints.py
@@ -80,7 +80,10 @@ async def list_policies(version_status: Optional[str] = None):
List all policies from the database and config.yaml. Optionally filter by version_status.
Config-defined policies are returned with definition_location "config" and are treated
- as production versions. On a name conflict with a DB policy, only the DB policy is returned.
+ as production versions. On a name conflict with a production DB policy, only the DB policy
+ is returned, mirroring runtime resolution where only production DB versions override config.
+ A draft or published DB version does not hide the config policy, since the config version
+ is still the one being enforced.
Query params:
- version_status: Optional. One of "draft", "published", "production".
@@ -125,7 +128,9 @@ async def list_policies(version_status: Optional[str] = None):
if prisma_client is not None
else []
)
- db_policy_names = {db_policy.policy_name for db_policy in db_policies}
+ db_policy_names = {
+ db_policy.policy_name for db_policy in db_policies if db_policy.version_status == "production"
+ }
include_config = version_status in (None, "production")
config_policies = (
[
diff --git a/tests/test_litellm/proxy/policy_engine/test_attachment_registry.py b/tests/test_litellm/proxy/policy_engine/test_attachment_registry.py
index cf470c66000..cc231e383a3 100644
--- a/tests/test_litellm/proxy/policy_engine/test_attachment_registry.py
+++ b/tests/test_litellm/proxy/policy_engine/test_attachment_registry.py
@@ -453,3 +453,14 @@ class TestConfigAttachmentsPreservedAcrossDbSync:
await registry.sync_attachments_from_db(_prisma_with_attachment_rows([]))
assert len(registry.get_all_attachments()) == 1
+
+ @pytest.mark.asyncio
+ async def test_clear_removes_config_snapshot_so_sync_does_not_resurrect(self):
+ registry = AttachmentRegistry()
+ registry.load_attachments([{"policy": "config-policy", "scope": "*"}])
+
+ registry.clear()
+ await registry.sync_attachments_from_db(_prisma_with_attachment_rows([]))
+
+ assert registry.get_all_attachments() == []
+ assert registry.get_config_attachments() == ()
diff --git a/tests/test_litellm/proxy/policy_engine/test_policy_engine_endpoints.py b/tests/test_litellm/proxy/policy_engine/test_policy_engine_endpoints.py
index 78126508c2d..9c486540b3d 100644
--- a/tests/test_litellm/proxy/policy_engine/test_policy_engine_endpoints.py
+++ b/tests/test_litellm/proxy/policy_engine/test_policy_engine_endpoints.py
@@ -133,6 +133,32 @@ class TestListPoliciesIncludesConfig:
assert response.policies[0].definition_location == "db"
assert response.policies[0].guardrails_add == ["db-guard"]
+ @pytest.mark.asyncio
+ async def test_draft_db_policy_does_not_hide_enforced_config_policy(self, policy_registry, monkeypatch):
+ """
+ Runtime sync only lets production DB versions override a config policy,
+ so a draft or published DB version sharing the name must not suppress
+ the config entry: the config version is still the one being enforced,
+ and hiding it makes the list API disagree with actual enforcement.
+ """
+ row = _make_policy_row(
+ policy_id="uuid-1", policy_name="shared-name", version_status="draft", guardrails_add=["db-guard"]
+ )
+ prisma = MagicMock()
+ prisma.db.litellm_policytable.find_many = AsyncMock(return_value=[row])
+ _set_prisma(monkeypatch, prisma)
+ policy_registry.load_policies({"shared-name": {"guardrails": {"add": ["config-guard"]}}})
+
+ response = await policy_endpoints.list_policies()
+
+ assert response.total_count == 2
+ config_entry = next(p for p in response.policies if p.definition_location == "config")
+ assert config_entry.policy_name == "shared-name"
+ assert config_entry.version_status == "production"
+ assert config_entry.guardrails_add == ["config-guard"]
+ db_entry = next(p for p in response.policies if p.definition_location == "db")
+ assert db_entry.version_status == "draft"
+
@pytest.mark.asyncio
async def test_version_status_filter_excludes_config_policies(self, policy_registry, monkeypatch):
row = _make_policy_row(policy_id="uuid-1", policy_name="db-policy", version_status="draft")
diff --git a/tests/test_litellm/proxy/policy_engine/test_policy_versioning.py b/tests/test_litellm/proxy/policy_engine/test_policy_versioning.py
index 5a840979c1b..ae80373ee23 100644
--- a/tests/test_litellm/proxy/policy_engine/test_policy_versioning.py
+++ b/tests/test_litellm/proxy/policy_engine/test_policy_versioning.py
@@ -13,8 +13,10 @@ from litellm.proxy.policy_engine.policy_registry import (
get_policy_registry,
)
from litellm.types.proxy.policy_engine import (
+ Policy,
PolicyCreateRequest,
PolicyDBResponse,
+ PolicyGuardrails,
PolicyUpdateRequest,
)
@@ -529,3 +531,28 @@ class TestConfigPoliciesPreservedAcrossDbSync:
context=None,
)
assert resolved.guardrails == ["tooling"]
+
+ @pytest.mark.asyncio
+ async def test_add_policy_with_config_source_survives_sync(self):
+ registry = PolicyRegistry()
+ registry.add_policy(
+ "late-config-policy",
+ Policy(guardrails=PolicyGuardrails(add=["tooling"])),
+ source="config",
+ )
+
+ await registry.sync_policies_from_db(_prisma_with_policy_rows([]))
+
+ assert registry.has_policy("late-config-policy")
+ assert registry.get_source("late-config-policy") == "config"
+
+ @pytest.mark.asyncio
+ async def test_clear_removes_config_snapshot_so_sync_does_not_resurrect(self):
+ registry = PolicyRegistry()
+ registry.load_policies({"config-policy": {"guardrails": {"add": ["tooling"]}}})
+
+ registry.clear()
+ await registry.sync_policies_from_db(_prisma_with_policy_rows([]))
+
+ assert not registry.has_policy("config-policy")
+ assert registry.get_source("config-policy") is None