{"record":{"id":"9c0e7fadaea699a5","repo":"invoke-ai/InvokeAI","slug":"openai-api-key-is-not-configured","errorCode":null,"errorMessage":"OpenAI API key is not configured","messagePattern":"OpenAI API key is not configured","errorType":"exception","errorClass":"ExternalProviderRequestError","httpStatus":null,"severity":"error","filePath":"invokeai/app/services/external_generation/providers/openai.py","lineNumber":34,"sourceCode":"    ExternalGenerationResult,\n)\nfrom invokeai.app.services.external_generation.image_utils import decode_image_base64\n\n\nclass OpenAIProvider(ExternalProvider):\n    provider_id = \"openai\"\n\n    _GPT_IMAGE_MODELS = {\"gpt-image-1\", \"gpt-image-1.5\", \"gpt-image-1-mini\", \"gpt-image-2\"}\n    _DEFAULT_TIMEOUT = 120\n    _MODEL_TIMEOUTS: dict[str, int] = {\"gpt-image-2\": 300}\n\n    def is_configured(self) -> bool:\n        return bool(self._app_config.external_openai_api_key)\n\n    def generate(self, request: ExternalGenerationRequest) -> ExternalGenerationResult:\n        api_key = self._app_config.external_openai_api_key\n        if not api_key:\n            raise ExternalProviderRequestError(\"OpenAI API key is not configured\")\n\n        model_id = request.model.provider_model_id\n        is_gpt_image = model_id in self._GPT_IMAGE_MODELS\n        timeout = self._MODEL_TIMEOUTS.get(model_id, self._DEFAULT_TIMEOUT)\n        size = f\"{request.width}x{request.height}\"\n        base_url = (self._app_config.external_openai_base_url or \"https://api.openai.com\").rstrip(\"/\")\n        headers = {\"Authorization\": f\"Bearer {api_key}\"}\n\n        use_edits_endpoint = request.mode != \"txt2img\" or bool(request.reference_images)\n\n        opts = request.provider_options or {}\n\n        if not use_edits_endpoint:\n            payload: dict[str, object] = {\n                \"model\": model_id,\n                \"prompt\": request.prompt,\n                \"n\": request.num_images,\n                \"size\": size,","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/services/external_generation/providers/openai.py#L16-L52","documentation":"ExternalProviderRequestError raised by OpenAIProvider.generate when the app config has no external_openai_api_key set. The provider uses the key for the Authorization: Bearer header on /v1/images/generations and /v1/images/edits, and skips making a request that would certainly fail with 401. This is a local configuration error, not an API response.","triggerScenarios":"A generation request routed to provider_id 'openai' while self._app_config.external_openai_api_key is empty/None — including edits requests that would otherwise need reference images. is_configured() likewise returns False.","commonSituations":"OPENAI_API_KEY env var not exported to the InvokeAI process (e.g. set in shell but not in systemd/docker); key configured under the wrong config attribute; fresh install where only Gemini was configured; container secret not mounted.","solutions":["Set external_openai_api_key (or its environment variable) to a valid OpenAI API key and restart InvokeAI.","Guard before dispatch: call provider.is_configured() and show a configuration error to the user instead of attempting generation.","Confirm the key is visible inside the runtime environment (docker exec env / systemd show-environment).","Validate the key against https://api.openai.com/v1/models with a curl Bearer request."],"exampleFix":"// before: raises deep in generate()\nresult = openai_provider.generate(request)\n// after: pre-flight config check\nif not openai_provider.is_configured():\n    raise RuntimeError(\"OpenAI provider not configured: set external_openai_api_key\")\nresult = openai_provider.generate(request)","handlingStrategy":"validation","validationCode":"if not app_config.external_openai_api_key:\n    raise RuntimeError(\"OpenAI is not configured: set external_openai_api_key before using OpenAI models\")\n# or gate the UI:\nif not openai_provider.is_configured():\n    hide_openai_models()","typeGuard":null,"tryCatchPattern":"try:\n    result = provider.generate(request)\nexcept ExternalProviderRequestError as e:\n    if \"not configured\" in str(e):\n        show_setup_instructions(\"openai\")\n    else:\n        raise","preventionTips":["Call provider.is_configured() before offering OpenAI models in the UI or dispatching jobs.","Ensure the env var is exported inside the actual runtime (docker/systemd), not just your shell.","Add a startup config check that warns when an enabled provider lacks its API key.","Rotate and validate keys periodically with a lightweight authenticated call."],"tags":["configuration","missing-credentials","api-key","openai"],"backgroundTag":"missing-env-var","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}