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 <noreply@anthropic.com>
This commit is contained in:
Julio Quinteros Pro 2026-02-17 19:29:50 -03:00
parent 7f1cb90228
commit f8f2a86ce1
3 changed files with 4 additions and 3 deletions

View file

@ -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 = (

View file

@ -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"

View file

@ -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)