BerriAI/litellm · error · ValueError

Missing CLOUDFLARE_ACCOUNT_ID - set CLOUDFLARE_ACCOUNT_ID…

Error message

Missing CLOUDFLARE_ACCOUNT_ID - set CLOUDFLARE_ACCOUNT_ID in the environment or pass api_base explicitly

What it means

ValueError raised by the Cloudflare adapter's _resolve_api_base when no api_base was passed and get_secret_str('CLOUDFLARE_ACCOUNT_ID') returns nothing (unset or empty). LiteLLM builds the Workers AI OpenAI-compatible URL as https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1, so the account ID is mandatory unless you supply the full api_base yourself.

Solutions

  1. export CLOUDFLARE_ACCOUNT_ID=<your account id> (Cloudflare dashboard, right sidebar) or add it to your .env
  2. Or pass the full endpoint explicitly: completion(..., api_base="https://api.cloudflare.com/client/v4/accounts/<id>/ai/v1")
  3. Confirm the variable is visible to the process (printenv CLOUDFLARE_ACCOUNT_ID) — not just your login shell
  4. Note legacy /ai/run bases are auto-rewritten to /ai/v1; keep account id + key in the same env scope

Example fix

# before
litellm.completion(model="cloudflare/@cf/meta/llama-3.1-8b-instruct", messages=messages)
# ValueError: Missing CLOUDFLARE_ACCOUNT_ID

# after
os.environ["CLOUDFLARE_ACCOUNT_ID"] = "a1b2c3d4"
litellm.completion(model="cloudflare/@cf/meta/llama-3.1-8b-instruct", messages=messages)
Defensive patterns

Strategy: validation

Validate before calling

import os

if not os.environ.get("CLOUDFLARE_ACCOUNT_ID", "").strip():
    raise RuntimeError(
        "CLOUDFLARE_ACCOUNT_ID is not set; get it from the Cloudflare dashboard right sidebar"
    )

litellm.completion(model="cloudflare/@cf/meta/llama-3.1-8b-instruct", messages=messages)

Try / catch

try:
    litellm.completion(model="cloudflare/@cf/...", messages=messages)
except ValueError as e:
    if "CLOUDFLARE_ACCOUNT_ID" in str(e):
        os.environ["CLOUDFLARE_ACCOUNT_ID"] = fetch_account_id()
        litellm.completion(model="cloudflare/@cf/...", messages=messages)
    else:
        raise

Prevention

When it happens

Trigger: Calling completion(model="cloudflare/@cf/...") with no CLOUDFLARE_ACCOUNT_ID environment variable (or one set to an empty/whitespace string) and no explicit api_base parameter.

Common situations: New Cloudflare integration where only the API key was configured; .env not loaded in the runtime; account ID set in a different shell/deployment than the one running litellm; CI secrets omitted.

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/a614c42d736e7939. Report an issue: GitHub.

Appendix: source

Thrown at litellm/llms/cloudflare/chat/transformation.py:53

        optional_params: dict,
        litellm_params: dict,
        stream: bool | None = None,
    ) -> str:
        return super().get_complete_url(
            api_base=self._resolve_api_base(api_base),
            api_key=api_key,
            model=model,
            optional_params=optional_params,
            litellm_params=litellm_params,
            stream=stream,
        )

    @staticmethod
    def _resolve_api_base(api_base: str | None) -> str:
        if not api_base:
            account_id: Final = normalize_nonempty_secret_str(get_secret_str("CLOUDFLARE_ACCOUNT_ID"))
            if account_id is None:
                raise ValueError(
                    "Missing CLOUDFLARE_ACCOUNT_ID - set CLOUDFLARE_ACCOUNT_ID in the environment or pass api_base explicitly"
                )
            return f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1"
        trimmed: Final = api_base.rstrip("/")
        if trimmed.endswith("/ai/run"):
            verbose_logger.warning(
                "Cloudflare api_base ending in '/ai/run' is the legacy Workers AI path and no longer serves OpenAI-compatible requests; rewriting to the '/ai/v1' endpoint"
            )
            return f"{trimmed[: -len('/ai/run')]}/ai/v1"
        return api_base

    def validate_environment(
        self,
        headers: dict,
        model: str,
        messages: list[AllMessageValues],
        optional_params: dict,
        litellm_params: dict,

View on GitHub (pinned to 77b7c6c40c)