From c37b41ad58fa135bc9e319a358bc4e7f036dbafa Mon Sep 17 00:00:00 2001 From: Hedi Daoud Date: Mon, 8 Jun 2026 15:47:49 +0200 Subject: [PATCH] test: inject fake lakera_ai module to avoid importing the real one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CI fix for the lakera regression test: it stubbed litellm.proxy.proxy_server with a SimpleNamespace and then monkeypatch.setattr'd the real lakera_ai module, which forces importing it — and lakera_ai does `from litellm.proxy.proxy_server import LiteLLM_TeamTable`, absent on the stub -> ImportError under proxy-infra tests. Inject a fake lakera_ai module into sys.modules instead, so the callbacks branch's `from ...lakera_ai import lakeraAI_Moderation` resolves to the stub without loading the real module. The guard under test (isinstance(dict) in the lakera branch) is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../proxy/common_utils/test_callback_utils.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/test_litellm/proxy/common_utils/test_callback_utils.py b/tests/test_litellm/proxy/common_utils/test_callback_utils.py index 2154651941c..16b6a7838d1 100644 --- a/tests/test_litellm/proxy/common_utils/test_callback_utils.py +++ b/tests/test_litellm/proxy/common_utils/test_callback_utils.py @@ -1,7 +1,7 @@ import copy import sys import os -from types import SimpleNamespace +from types import ModuleType, SimpleNamespace sys.path.insert( 0, os.path.abspath("../../..") @@ -331,15 +331,22 @@ def test_initialize_callbacks_on_proxy_lakera_ignores_non_dict_callback_settings def __init__(self, **kwargs): captured["kwargs"] = kwargs + # Inject a fake lakera_ai module so the branch's + # `from ...lakera_ai import lakeraAI_Moderation` resolves to our stub without + # importing the real module (which imports proxy_server symbols not present + # under the stubbed proxy_server below). + fake_lakera = ModuleType("litellm.proxy.guardrails.guardrail_hooks.lakera_ai") + fake_lakera.lakeraAI_Moderation = _DummyLakera + monkeypatch.setitem( + sys.modules, + "litellm.proxy.guardrails.guardrail_hooks.lakera_ai", + fake_lakera, + ) monkeypatch.setitem( sys.modules, "litellm.proxy.proxy_server", SimpleNamespace(prisma_client=None), ) - monkeypatch.setattr( - "litellm.proxy.guardrails.guardrail_hooks.lakera_ai.lakeraAI_Moderation", - _DummyLakera, - ) original_callbacks = ( list(litellm.callbacks) if isinstance(litellm.callbacks, list) else []