invoke-ai/InvokeAI · error · ExternalProviderRequestError
Gemini API key is not configured
Error message
Gemini API key is not configured
What it means
ExternalProviderRequestError raised by GeminiProvider.generate when the app config has no external_gemini_api_key set. The provider requires an API key to authenticate requests to the Gemini generateContent endpoint (sent as the ?key= query parameter), and refuses to make a doomed request when it is absent. This is a configuration error, not a network or API failure.
Source
Thrown at invokeai/app/services/external_generation/providers/gemini.py:32
)
from invokeai.app.services.external_generation.image_utils import decode_image_base64, encode_image_base64
class GeminiProvider(ExternalProvider):
provider_id = "gemini"
_SYSTEM_INSTRUCTION = (
"You are an image generation model. Always respond with an image based on the user's prompt. "
"Do not return text-only responses. If the user input is not an edit instruction, "
"interpret it as a request to create a new image."
)
def is_configured(self) -> bool:
return bool(self._app_config.external_gemini_api_key)
def generate(self, request: ExternalGenerationRequest) -> ExternalGenerationResult:
api_key = self._app_config.external_gemini_api_key
if not api_key:
raise ExternalProviderRequestError("Gemini API key is not configured")
base_url = (self._app_config.external_gemini_base_url or "https://generativelanguage.googleapis.com").rstrip(
"/"
)
if not base_url.endswith("/v1") and not base_url.endswith("/v1beta"):
base_url = f"{base_url}/v1beta"
model_id = request.model.provider_model_id.removeprefix("models/")
endpoint = f"{base_url}/models/{model_id}:generateContent"
request_parts: list[dict[str, object]] = []
if request.init_image is not None:
request_parts.append(
{
"inlineData": {
"mimeType": "image/png",
"data": encode_image_base64(request.init_image),
}View on GitHub (pinned to 0b6a024f2f)
Solutions
- Set the external_gemini_api_key config value (or its corresponding environment variable) to a valid Google AI Studio API key and restart InvokeAI.
- Before selecting a Gemini model, call provider.is_configured() and surface a setup prompt to the user if it returns False.
- Double-check the key is placed in the active config file/profile actually loaded by the app, not a stale one.
- Verify the key works with a direct curl to https://generativelanguage.googleapis.com/v1beta/models?key=... .
Example fix
// before: key missing, generate() raises at runtime
result = gemini_provider.generate(request)
// after: check configuration first and give an actionable message
if not gemini_provider.is_configured():
raise RuntimeError(
"Gemini is not configured: set external_gemini_api_key in InvokeAI config"
)
result = gemini_provider.generate(request) Defensive patterns
Strategy: validation
Validate before calling
if not app_config.external_gemini_api_key:
raise RuntimeError("Gemini is not configured: set external_gemini_api_key before using Gemini models")
# or, non-raising:
if not gemini_provider.is_configured():
disable_gemini_models_in_ui() Try / catch
try:
result = provider.generate(request)
except ExternalProviderRequestError as e:
if "not configured" in str(e):
show_setup_instructions("gemini")
else:
raise Prevention
- Check provider.is_configured() at startup and expose the result in the UI/model list.
- Keep API keys in environment variables loaded by the actual service process.
- Add a config smoke test that asserts required keys exist when a provider is enabled.
- Document which env vars each provider needs.
When it happens
Trigger: A generation request is routed to provider_id 'gemini' while self._app_config.external_gemini_api_key is empty/None — i.e., generate() is called without the key ever having been configured (is_configured() would also return False).
Common situations: Missing environment variable / config entry for the Gemini API key; key set under the wrong config name (e.g. only OPENAI key configured); key removed after a config migration; user selects a Gemini model without ever enrolling an API key.
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
- OpenAI API key is not configured
- Alibaba Cloud DashScope API key is not configured
- Gemini request failed with status {response.status_code} for
- LoRA '{lora.lora.key}' has conflicting weights on the transf
- Model '{main_config.name}' is not a Krea-2 main model. Selec
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/bdbe27ffebe1a7d1.
Report an issue: GitHub.