{"record":{"id":"f9f1109025cb9229","repo":"BerriAI/litellm","slug":"bfl-api-key-is-not-set-please-set-it-via-environm","errorCode":null,"errorMessage":"BFL_API_KEY is not set. Please set it via environment variable or pass api_key parameter.","messagePattern":"BFL_API_KEY is not set\\. Please set it via environment variable or pass api_key parameter\\.","errorType":"exception","errorClass":"BlackForestLabsError","httpStatus":401,"severity":"error","filePath":"litellm/llms/black_forest_labs/image_edit/transformation.py","lineNumber":141,"sourceCode":"    def validate_environment(\n        self,\n        headers: dict,\n        model: str,\n        api_key: str | None = None,\n        litellm_params: dict | None = None,\n        api_base: str | None = None,\n    ) -> dict:\n        \"\"\"\n        Validate environment and set up headers for Black Forest Labs.\n\n        BFL uses x-key header for authentication.\n        \"\"\"\n        final_api_key: Final[str | None] = (\n            api_key or get_secret_str(\"BFL_API_KEY\") or get_secret_str(\"BLACK_FOREST_LABS_API_KEY\")\n        )\n\n        if not final_api_key:\n            raise BlackForestLabsError(\n                status_code=401,\n                message=\"BFL_API_KEY is not set. Please set it via environment variable or pass api_key parameter.\",\n            )\n\n        headers[\"x-key\"] = final_api_key\n        headers[\"Content-Type\"] = \"application/json\"\n        headers[\"Accept\"] = \"application/json\"\n\n        return headers\n\n    def use_multipart_form_data(self) -> bool:\n        \"\"\"\n        BFL uses JSON requests, not multipart/form-data.\n        \"\"\"\n        return False\n\n    def _get_model_endpoint(self, model: str) -> str:\n        \"\"\"","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/black_forest_labs/image_edit/transformation.py#L123-L159","documentation":"The BFL image-edit transformation builds auth headers and requires an API key. It resolves the key from the explicit `api_key` parameter, then the BFL_API_KEY environment variable, then BLACK_FOREST_LABS_API_KEY. If none is present, it raises BlackForestLabsError(401) before any network call is made. This is a local configuration failure, not an upstream rejection.","triggerScenarios":"Calling image_edit with a `black_forest_labs/*` model when: no api_key argument is passed AND neither BFL_API_KEY nor BLACK_FOREST_LABS_API_KEY is set in the environment of the LiteLLM process (or proxy server worker).","commonSituations":"Forgetting to export BFL_API_KEY in the shell; .env not loaded in the deployed environment; LiteLLM proxy config missing the `api_key`/environment_variables entry; CI runners lacking secrets; key set under a different name (e.g. BLACK_FOREST_LABS_KEY).","solutions":["export BFL_API_KEY=<your key> in the environment where LiteLLM runs, then retry.","Or pass the key explicitly: litellm.image_edit(..., api_key=...).","On the LiteLLM proxy, add the key to the model's litellm_params (api_key: os.environ/BFL_API_KEY) and ensure the env var is present in the proxy's environment.","As a fallback name, BLACK_FOREST_LABS_API_KEY is also accepted if BFL_API_KEY is unset."],"exampleFix":"# before\nlitellm.image_edit(model=\"black_forest_labs/flux-kontext-pro\", image=b, prompt=\"add a hat\")\n\n# after\nlitellm.image_edit(model=\"black_forest_labs/flux-kontext-pro\", image=b, prompt=\"add a hat\", api_key=os.environ[\"BFL_API_KEY\"])","handlingStrategy":"validation","validationCode":"import os\n\nassert os.getenv(\"BFL_API_KEY\") or os.getenv(\"BLACK_FOREST_LABS_API_KEY\") or passed_api_key, \\\n    \"BFL credentials missing — set BFL_API_KEY or pass api_key\"","typeGuard":"null","tryCatchPattern":"from litellm.exceptions import AuthenticationError\ntry:\n    litellm.image_edit(model=M, image=img, prompt=p)\nexcept AuthenticationError:\n    # surface a config action, not a retry\n    raise RuntimeError(\"Configure BFL_API_KEY before using black_forest_labs models\")","preventionTips":["Fail fast at startup: check BFL_API_KEY when your app boots if any BFL model is configured.","On the LiteLLM proxy, declare the env var in the config so missing keys fail at proxy startup, not per-request.","Never retry 401s — they are deterministic config errors."],"tags":["bfl","auth","api-key","configuration","image-edit"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}