From f8f2a86ce135c52e872da77554c2805675c0ed35 Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Tue, 17 Feb 2026 19:29:50 -0300 Subject: [PATCH 1/2] fix(lakera-guardrail): use os.environ.get() to avoid KeyError on missing LAKERA_API_KEY `os.environ["LAKERA_API_KEY"]` raises KeyError when the env var is absent, causing test_active_callbacks to error during fixture setup. Switch to `os.environ.get()` in both lakera_ai.py and lakera_ai_v2.py so initialization succeeds without the key (actual API calls will fail separately if key is unset). Also mock `premium_user=True` in the test fixture so the enterprise `hide_secrets` guardrail can initialize, matching the test's expectations. Co-Authored-By: Claude Sonnet 4.6 --- litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py | 2 +- litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py | 2 +- tests/proxy_unit_tests/test_proxy_setting_guardrails.py | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py index a530682ae4..0b0e5c63db 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py +++ b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py @@ -59,7 +59,7 @@ class lakeraAI_Moderation(CustomGuardrail): self.async_handler = get_async_httpx_client( llm_provider=httpxSpecialProvider.GuardrailCallback ) - self.lakera_api_key = api_key or os.environ["LAKERA_API_KEY"] + self.lakera_api_key = api_key or os.environ.get("LAKERA_API_KEY") self.moderation_check = moderation_check self.category_thresholds = category_thresholds self.api_base = ( diff --git a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py index 732331349e..0483e257e7 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py +++ b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py @@ -54,7 +54,7 @@ class LakeraAIGuardrail(CustomGuardrail): self.async_handler = get_async_httpx_client( llm_provider=httpxSpecialProvider.GuardrailCallback ) - self.lakera_api_key = api_key or os.environ["LAKERA_API_KEY"] + self.lakera_api_key = api_key or os.environ.get("LAKERA_API_KEY") self.project_id = project_id self.api_base = ( api_base or get_secret_str("LAKERA_API_BASE") or "https://api.lakera.ai" diff --git a/tests/proxy_unit_tests/test_proxy_setting_guardrails.py b/tests/proxy_unit_tests/test_proxy_setting_guardrails.py index b845f86b6e..d5dac59b3c 100644 --- a/tests/proxy_unit_tests/test_proxy_setting_guardrails.py +++ b/tests/proxy_unit_tests/test_proxy_setting_guardrails.py @@ -30,7 +30,8 @@ from litellm.proxy.proxy_server import ( # Replace with the actual module where def client(): filepath = os.path.dirname(os.path.abspath(__file__)) config_fp = f"{filepath}/test_configs/test_guardrails_config.yaml" - asyncio.run(initialize(config=config_fp)) + with mock.patch("litellm.proxy.proxy_server.premium_user", True): + asyncio.run(initialize(config=config_fp)) from litellm.proxy.proxy_server import app return TestClient(app) From fad4d4cff02280e1fb39aa41423f966261a31054 Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Tue, 17 Feb 2026 19:51:18 -0300 Subject: [PATCH 2/2] fix(lakera-guardrail): ensure lakera_api_key is always str to fix mypy error Appending `or ""` keeps the type as `str` (not `str | None`), fixing: lakera_ai.py:267: error: Unsupported operand types for + ("str" and "None") Also prevents lakera_ai_v2.py from silently sending "Bearer None" when the key is absent; a missing key now yields a clear 401 from the Lakera API. Co-Authored-By: Claude Sonnet 4.6 --- litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py | 2 +- litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py index 0b0e5c63db..28f0d830f1 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py +++ b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py @@ -59,7 +59,7 @@ class lakeraAI_Moderation(CustomGuardrail): self.async_handler = get_async_httpx_client( llm_provider=httpxSpecialProvider.GuardrailCallback ) - self.lakera_api_key = api_key or os.environ.get("LAKERA_API_KEY") + self.lakera_api_key = api_key or os.environ.get("LAKERA_API_KEY") or "" self.moderation_check = moderation_check self.category_thresholds = category_thresholds self.api_base = ( diff --git a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py index 0483e257e7..738827b7ad 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py +++ b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py @@ -54,7 +54,7 @@ class LakeraAIGuardrail(CustomGuardrail): self.async_handler = get_async_httpx_client( llm_provider=httpxSpecialProvider.GuardrailCallback ) - self.lakera_api_key = api_key or os.environ.get("LAKERA_API_KEY") + self.lakera_api_key = api_key or os.environ.get("LAKERA_API_KEY") or "" self.project_id = project_id self.api_base = ( api_base or get_secret_str("LAKERA_API_BASE") or "https://api.lakera.ai"