abi/screenshot-to-code · critical · Exception
No OpenAI, Anthropic, or Gemini API key found. Please add th
Error message
No OpenAI, Anthropic, or Gemini API key found. Please add the environment variable OPENAI_API_KEY, ANTHROPIC_API_KEY, or GEMINI_API_KEY to backend/.env or in the settings dialog. If you add it to .env, make sure to restart the backend server.
What it means
Error emitted via throw_error in backend/routes/generate_code.py:449 by the variant-model resolver: its try block (which selects models per variant based on available keys) raised, which in practice means none of OPENAI_API_KEY, ANTHROPIC_API_KEY, or GEMINI_API_KEY was available from settings or the environment. Generation cannot proceed without at least one provider key, and the resolver's bare 'except Exception' converts any failure there into this message followed by raise Exception('No API key').
Source
Thrown at backend/routes/generate_code.py:449
num_variants,
openai_api_key,
anthropic_api_key,
gemini_api_key,
)
# Print the variant models (one per line)
print("Variant models:")
for index, model in enumerate(variant_models):
print(f"Variant {index + 1}: {model.value}")
return variant_models
except Exception:
await self.throw_error(
"No OpenAI, Anthropic, or Gemini API key found. Please add the environment variable "
"OPENAI_API_KEY, ANTHROPIC_API_KEY, or GEMINI_API_KEY to backend/.env or in the settings dialog. "
"If you add it to .env, make sure to restart the backend server."
)
raise Exception("No API key")
def _get_variant_models(
self,
generation_type: Literal["create", "update"],
input_mode: InputMode,
num_variants: int,
openai_api_key: str | None,
anthropic_api_key: str | None,
gemini_api_key: str | None,
) -> List[Llm]:
"""Simple model cycling that scales with num_variants"""
# Video mode requires Gemini - 2 variants for comparison
if input_mode == "video":
if not gemini_api_key:
raise Exception(
"Video mode requires a Gemini API key. "
"Please add GEMINI_API_KEY to backend/.env or in the settings dialog"View on GitHub (pinned to d026163f58)
Solutions
- Add OPENAI_API_KEY (or ANTHROPIC_API_KEY / GEMINI_API_KEY) to backend/.env and restart the backend.
- Or enter the key at runtime in the app's Settings dialog, which persists it without a restart.
- Verify with a simple script that the key is non-empty in the backend process env; confirm it is not only REPLICATE_API_KEY.
Example fix
# before (backend/.env) REPLICATE_API_KEY=r8_xxx # after (backend/.env) REPLICATE_API_KEY=r8_xxx OPENAI_API_KEY=sk-... # then restart: poetry run uvicorn main:app --reload --port 7001
Defensive patterns
Strategy: validation
Validate before calling
// client-side preflight: refuse to start generation without a key configured
if (!settings.openAiApiKey && !settings.anthropicApiKey && !settings.geminiApiKey) {
openSettingsDialog();
return; // do not open the generation WebSocket
} Try / catch
ws.onmessage = (ev) => {
const msg = JSON.parse(ev.data);
if (msg.type === 'error' && msg.message.includes('API key')) {
// open Settings dialog; after the user saves a key, retry generation
}
}; Prevention
- Put provider keys in backend/.env and restart the backend after editing.
- Remember REPLICATE_API_KEY alone is not sufficient for text generation.
- Check key presence before opening the generation stream, not after failure.
When it happens
Trigger: Starting a generation with no API key in backend/.env, no key entered in the in-app Settings dialog, and no key in the process environment; or the keys exist but all selected models' providers are unavailable. Note REPLICATE_API_KEY does not count here.
Common situations: Fresh setup where backend/.env was never created; .env edited after the backend started (values are read at startup — restart required); key placed in the frontend .env instead of backend/.env; relying on REPLICATE_API_KEY alone.
Related errors
- Invalid generated code config: {generated_code_config}
- Invalid input mode: {input_mode}
- Invalid generation type: {generation_type}
- OpenAI API key is missing.
- Anthropic API key is missing.
AI-assisted analysis of abi/screenshot-to-code@d026163f58 (2026-08-14).
Data as JSON: /api/errors/1a652afdaf3596ca.
Report an issue: GitHub.