BerriAI/litellm · critical · ValueError

Compresr guardrail requires an API key. Set `api_key` in the

Error message

Compresr guardrail requires an API key. Set `api_key` in the guardrail config or the COMPRESR_API_KEY env var.

What it means

ValueError raised in the Compresr guardrail constructor when no API key is found: the api_key param is empty and the COMPRESR_API_KEY environment variable (via get_secret_str) is also unset. Compresr is a prompt-compression service that requires authenticated calls, so the guardrail fails fast at init instead of sending unauthenticated requests that would fail per-call.

Source

Thrown at litellm/proxy/guardrails/guardrail_hooks/compresr/compresr.py:525

        compress_history: bool | None = None,
        compress_last_user: bool | None = None,
        enable_retrieval: bool | None = None,
        guardrail_name: str | None = None,
        event_hook: GuardrailEventHooks | list[GuardrailEventHooks] | Mode | None = None,
        default_on: bool = False,
        unreachable_fallback: str | None = None,
        max_bytes_per_call: int | None = None,
        allow_bypass_header: bool | None = None,
        dynamic: bool | None = None,
        dynamic_min_ratio: float | None = None,
        dynamic_max_ratio: float | None = None,
        compression_params: dict[str, object] | None = None,
    ):
        raw_api_base: Final = (api_base or get_secret_str("COMPRESR_API_BASE") or DEFAULT_API_BASE).rstrip("/")
        self.compresr_api_base = _validate_api_base(raw_api_base)
        self.compresr_api_key = api_key or get_secret_str("COMPRESR_API_KEY")
        if not self.compresr_api_key:
            raise ValueError(
                "Compresr guardrail requires an API key. Set `api_key` in the "
                "guardrail config or the COMPRESR_API_KEY env var."
            )
        self.compression_model = model or DEFAULT_COMPRESSION_MODEL
        self.target_compression_ratio = (
            DEFAULT_TARGET_COMPRESSION_RATIO if target_compression_ratio is None else target_compression_ratio
        )
        self.coarse = True if coarse is None else coarse
        self.min_chars_to_compress = (
            DEFAULT_MIN_CHARS_TO_COMPRESS if min_chars_to_compress is None else min_chars_to_compress
        )
        self.compress_tool_outputs = True if compress_tool_outputs is None else compress_tool_outputs
        self.compress_system = False if compress_system is None else compress_system
        self.compress_history = False if compress_history is None else compress_history
        self.compress_last_user = False if compress_last_user is None else compress_last_user
        self.enable_retrieval = True if enable_retrieval is None else enable_retrieval
        self.unreachable_fallback: Literal["fail_closed", "fail_open"] = (
            "fail_open" if unreachable_fallback == "fail_open" else "fail_closed"

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Set COMPRESR_API_KEY in the proxy's runtime environment (and restart).
  2. Or pass api_key: in the guardrail's litellm_params (secret-manager references supported).
  3. Verify resolution before startup: the proxy logs which secrets loaded; or run `printenv COMPRESR_API_KEY` inside the service context.

Example fix

# before
guardrails:
  - guardrail_name: compresr-compress
    litellm_params:
      guardrail: compresr

# after
#   export COMPRESR_API_KEY=<key>
guardrails:
  - guardrail_name: compresr-compress
    litellm_params:
      guardrail: compresr
      api_key: os.environ/COMPRESR_API_KEY
Defensive patterns

Strategy: validation

Validate before calling

import os
from litellm import get_secret_str
if not (get_secret_str("COMPRESR_API_KEY") or os.environ.get("COMPRESR_API_KEY")):
    raise SystemExit("COMPRESR_API_KEY must be set to use the compresr guardrail")

Prevention

When it happens

Trigger: Adding a guardrail: compresr entry without api_key in a process whose environment lacks COMPRESR_API_KEY; secret-manager reference for COMPRESR_API_KEY failing to resolve; deploying with docker/systemd where the env var was never injected.

Common situations: Local works (env var in shell) but containerized proxy fails at startup; key stored under a different name (COMPRESR_TOKEN); .env file not loaded by the deployment.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/61f395061536edb9. Report an issue: GitHub.