invoke-ai/InvokeAI · error · ExternalProviderRequestError
OpenAI API key is not configured
Error message
OpenAI API key is not configured
What it means
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.
Source
Thrown at invokeai/app/services/external_generation/providers/openai.py:34
ExternalGenerationResult,
)
from invokeai.app.services.external_generation.image_utils import decode_image_base64
class OpenAIProvider(ExternalProvider):
provider_id = "openai"
_GPT_IMAGE_MODELS = {"gpt-image-1", "gpt-image-1.5", "gpt-image-1-mini", "gpt-image-2"}
_DEFAULT_TIMEOUT = 120
_MODEL_TIMEOUTS: dict[str, int] = {"gpt-image-2": 300}
def is_configured(self) -> bool:
return bool(self._app_config.external_openai_api_key)
def generate(self, request: ExternalGenerationRequest) -> ExternalGenerationResult:
api_key = self._app_config.external_openai_api_key
if not api_key:
raise ExternalProviderRequestError("OpenAI API key is not configured")
model_id = request.model.provider_model_id
is_gpt_image = model_id in self._GPT_IMAGE_MODELS
timeout = self._MODEL_TIMEOUTS.get(model_id, self._DEFAULT_TIMEOUT)
size = f"{request.width}x{request.height}"
base_url = (self._app_config.external_openai_base_url or "https://api.openai.com").rstrip("/")
headers = {"Authorization": f"Bearer {api_key}"}
use_edits_endpoint = request.mode != "txt2img" or bool(request.reference_images)
opts = request.provider_options or {}
if not use_edits_endpoint:
payload: dict[str, object] = {
"model": model_id,
"prompt": request.prompt,
"n": request.num_images,
"size": size,View on GitHub (pinned to 0b6a024f2f)
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.
Example fix
// before: raises deep in generate()
result = openai_provider.generate(request)
// after: pre-flight config check
if not openai_provider.is_configured():
raise RuntimeError("OpenAI provider not configured: set external_openai_api_key")
result = openai_provider.generate(request) Defensive patterns
Strategy: validation
Validate before calling
if not app_config.external_openai_api_key:
raise RuntimeError("OpenAI is not configured: set external_openai_api_key before using OpenAI models")
# or gate the UI:
if not openai_provider.is_configured():
hide_openai_models() Try / catch
try:
result = provider.generate(request)
except ExternalProviderRequestError as e:
if "not configured" in str(e):
show_setup_instructions("openai")
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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
- Gemini API key is not configured
- Alibaba Cloud DashScope API key is not configured
- LoRA '{lora.lora.key}' has conflicting weights on the transf
- Model '{main_config.name}' is not a Krea-2 main model. Selec
- Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/9c0e7fadaea699a5.
Report an issue: GitHub.