{"record":{"id":"a614c42d736e7939","repo":"BerriAI/litellm","slug":"missing-cloudflare-account-id-set-cloudflare-acc","errorCode":null,"errorMessage":"Missing CLOUDFLARE_ACCOUNT_ID - set CLOUDFLARE_ACCOUNT_ID in the environment or pass api_base explicitly","messagePattern":"Missing CLOUDFLARE_ACCOUNT_ID - set CLOUDFLARE_ACCOUNT_ID in the environment or pass api_base explicitly","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/cloudflare/chat/transformation.py","lineNumber":53,"sourceCode":"        optional_params: dict,\n        litellm_params: dict,\n        stream: bool | None = None,\n    ) -> str:\n        return super().get_complete_url(\n            api_base=self._resolve_api_base(api_base),\n            api_key=api_key,\n            model=model,\n            optional_params=optional_params,\n            litellm_params=litellm_params,\n            stream=stream,\n        )\n\n    @staticmethod\n    def _resolve_api_base(api_base: str | None) -> str:\n        if not api_base:\n            account_id: Final = normalize_nonempty_secret_str(get_secret_str(\"CLOUDFLARE_ACCOUNT_ID\"))\n            if account_id is None:\n                raise ValueError(\n                    \"Missing CLOUDFLARE_ACCOUNT_ID - set CLOUDFLARE_ACCOUNT_ID in the environment or pass api_base explicitly\"\n                )\n            return f\"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1\"\n        trimmed: Final = api_base.rstrip(\"/\")\n        if trimmed.endswith(\"/ai/run\"):\n            verbose_logger.warning(\n                \"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\"\n            )\n            return f\"{trimmed[: -len('/ai/run')]}/ai/v1\"\n        return api_base\n\n    def validate_environment(\n        self,\n        headers: dict,\n        model: str,\n        messages: list[AllMessageValues],\n        optional_params: dict,\n        litellm_params: dict,","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/cloudflare/chat/transformation.py#L35-L71","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["export CLOUDFLARE_ACCOUNT_ID=<your account id> (Cloudflare dashboard, right sidebar) or add it to your .env","Or pass the full endpoint explicitly: completion(..., api_base=\"https://api.cloudflare.com/client/v4/accounts/<id>/ai/v1\")","Confirm the variable is visible to the process (printenv CLOUDFLARE_ACCOUNT_ID) — not just your login shell","Note legacy /ai/run bases are auto-rewritten to /ai/v1; keep account id + key in the same env scope"],"exampleFix":"# before\nlitellm.completion(model=\"cloudflare/@cf/meta/llama-3.1-8b-instruct\", messages=messages)\n# ValueError: Missing CLOUDFLARE_ACCOUNT_ID\n\n# after\nos.environ[\"CLOUDFLARE_ACCOUNT_ID\"] = \"a1b2c3d4\"\nlitellm.completion(model=\"cloudflare/@cf/meta/llama-3.1-8b-instruct\", messages=messages)","handlingStrategy":"validation","validationCode":"import os\n\nif not os.environ.get(\"CLOUDFLARE_ACCOUNT_ID\", \"\").strip():\n    raise RuntimeError(\n        \"CLOUDFLARE_ACCOUNT_ID is not set; get it from the Cloudflare dashboard right sidebar\"\n    )\n\nlitellm.completion(model=\"cloudflare/@cf/meta/llama-3.1-8b-instruct\", messages=messages)","typeGuard":null,"tryCatchPattern":"try:\n    litellm.completion(model=\"cloudflare/@cf/...\", messages=messages)\nexcept ValueError as e:\n    if \"CLOUDFLARE_ACCOUNT_ID\" in str(e):\n        os.environ[\"CLOUDFLARE_ACCOUNT_ID\"] = fetch_account_id()\n        litellm.completion(model=\"cloudflare/@cf/...\", messages=messages)\n    else:\n        raise","preventionTips":["Add CLOUDFLARE_ACCOUNT_ID next to CLOUDFLARE_API_KEY in one .env loaded at startup","Fail fast on missing env vars with a startup check rather than at first request","Document the account ID in deployment secrets for every environment (dev/CI/prod)"],"tags":["cloudflare","env-var","config","account-id"],"backgroundTag":"missing-env-var","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}