BerriAI/litellm · critical · ValueError

DynamoAI API key is required. Set DYNAMOAI_API_KEY environme

Error message

DynamoAI API key is required. Set DYNAMOAI_API_KEY environment variable or pass api_key parameter.

What it means

ValueError raised in DynamoAIGuardrail.__init__ when neither the api_key parameter nor the DYNAMOAI_API_KEY environment variable yields a key. LiteLLM constructs the guardrail when loading guardrails-config (or on first use), so this fails proxy startup or guardrail initialization before any request is moderated.

Source

Thrown at litellm/proxy/guardrails/guardrail_hooks/dynamoai/dynamoai.py:57

    Provides content moderation and policy enforcement using DynamoAI's guardrail API.
    """

    def __init__(
        self,
        guardrail_name: str = "litellm_test",
        api_key: str | None = None,
        api_base: str | None = None,
        model_id: str = "",
        policy_ids: list[str] = [],
        **kwargs,
    ):
        self.async_handler = get_async_httpx_client(llm_provider=httpxSpecialProvider.GuardrailCallback)

        # Set API configuration
        self.api_key = api_key or os.getenv("DYNAMOAI_API_KEY")
        if not self.api_key:
            raise ValueError(
                "DynamoAI API key is required. Set DYNAMOAI_API_KEY environment variable or pass api_key parameter."
            )

        self.api_base = api_base or os.getenv("DYNAMOAI_API_BASE", "https://api.dynamo.ai")
        self.api_url = f"{self.api_base}/v1/moderation/analyze/"

        # Model ID for tracking/logging purposes
        self.model_id = model_id or os.getenv("DYNAMOAI_MODEL_ID", "")

        # Policy IDs - get from parameter, env var, or use empty list
        env_policy_ids: Final = os.getenv("DYNAMOAI_POLICY_IDS", "")
        self.policy_ids = policy_ids or (env_policy_ids.split(",") if env_policy_ids else [])
        self.guardrail_name = guardrail_name
        self.guardrail_provider = "dynamoai"

        # store kwargs as optional_params
        self.optional_params = kwargs

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Export DYNAMOAI_API_KEY in the environment the proxy runs in (docker-compose environment:, K8s secret env, systemd Environment=).
  2. Or pass the key explicitly in the guardrail config: litellm_params: { guardrail: dynamoai, api_key: os.environ/DYNAMOAI_API_KEY }.
  3. Restart the proxy after setting the variable — the check runs at guardrail construction, not per request.
  4. Verify with a quick print of os.getenv('DYNAMOAI_API_KEY') in the same process context.

Example fix

# before
litellm_params:
  guardrail: dynamoai
  api_base: https://api.dynamo.ai

# after
litellm_params:
  guardrail: dynamoai
  api_base: https://api.dynamo.ai
  api_key: os.environ/DYNAMOAI_API_KEY
Defensive patterns

Strategy: validation

Validate before calling

import os

if not (os.getenv('DYNAMOAI_API_KEY') or cfg.litellm_params.get('api_key')):
    raise SystemExit('DYNAMOAI_API_KEY not set — refusing to start proxy with dynamoai guardrail')

Try / catch

try:
    guardrail = DynamoAIGuardrail(litellm_params=cfg)
except ValueError as e:
    if 'DYNAMOAI_API_KEY' in str(e):
        logger.error('deploy misconfigured: %s', e)
        raise
    raise

Prevention

When it happens

Trigger: A dynamoai entry in guardrails-config with no api_key in litellm_params while DYNAMOAI_API_KEY is unset in the proxy process environment; deploying with docker/systemd where the env var was not passed into the container/unit.

Common situations: Env var set in an interactive shell but not in the deployed environment (docker-compose env, Kubernetes secret, systemd Environment=); renamed or missing variable after a config refactor; CI pipelines running the proxy without the secret injected.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


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