abi/screenshot-to-code · critical · Exception
OpenAI API key is missing.
Error message
OpenAI API key is missing.
What it means
Raised by create_provider_session in the provider factory when the selected model is in OPENAI_MODELS but no OpenAI API key is available. The factory reads the key from explicit parameter then the OPENAI_API_KEY env var; the check is fail-fast before any client is constructed.
Source
Thrown at backend/agent/providers/factory.py:43
anthropic_api_key: Optional[str],
gemini_api_key: Optional[str],
replicate_api_key: Optional[str],
should_extract_assets: bool = True,
recorder: Optional[AgentRunRecorder] = None,
) -> ProviderSession:
canonical_tools = canonical_tool_definitions(
image_generation_enabled=should_generate_images,
# The edit_images tool calls Replicate, so don't offer it without a key.
image_editing_enabled=bool(replicate_api_key or REPLICATE_API_KEY),
# The extract_assets tool calls Gemini, so don't offer it without a key.
asset_extraction_enabled=should_extract_assets and bool(gemini_api_key),
# screenshot_preview needs headless Chromium; skip it if it can't launch.
screenshot_enabled=is_screenshot_preview_available(),
)
if model in OPENAI_MODELS:
if not openai_api_key:
raise Exception("OpenAI API key is missing.")
client = AsyncOpenAI(api_key=openai_api_key, base_url=openai_base_url)
return OpenAIProviderSession(
client=client,
model=model,
prompt_messages=prompt_messages,
tools=serialize_openai_tools(canonical_tools),
recorder=recorder,
)
if model in ANTHROPIC_MODELS:
if not anthropic_api_key:
raise Exception("Anthropic API key is missing.")
client = AsyncAnthropic(api_key=anthropic_api_key)
return AnthropicProviderSession(
client=client,
model=model,View on GitHub (pinned to d026163f58)
Solutions
- Set OPENAI_API_KEY in backend/.env and restart the backend.
- Or enter the key in the in-app Settings dialog so it is passed through the generation route.
- Verify with: cd backend && poetry run python -c "import os; print(bool(os.environ['OPENAI_API_KEY']))".
- Or switch to a model family whose key you already have (Anthropic/Gemini).
Example fix
# backend/.env OPENAI_API_KEY=sk-...
Defensive patterns
Strategy: validation
Validate before calling
import os
if model in OPENAI_MODELS and not os.environ.get("OPENAI_API_KEY"):
raise SystemExit("Set OPENAI_API_KEY in backend/.env before using OpenAI models") Try / catch
try:
session = create_provider_session(model=model, ...)
except Exception as e:
if "API key is missing" in str(e):
# prompt the user to open Settings / configure .env
... Prevention
- Configure backend/.env at deploy time with the keys for every model family you expose in the UI.
- Restart uvicorn after editing .env — env changes are not hot-reloaded.
- Expose a startup check that logs which providers have keys.
When it happens
Trigger: Requesting generation with an OpenAI model (e.g. gpt-4o family) while neither the function argument openai_api_key nor the OPENAI_API_KEY environment variable is set.
Common situations: Fresh clone without backend/.env, key set in the frontend Settings but not passed through this code path, env var set in a different shell than the one running uvicorn, or typo in the variable name.
Related errors
- Anthropic API key is missing.
- Gemini API key is missing.
- OpenAI API key not found
- Anthropic API key not found
- Gemini API key not found
AI-assisted analysis of abi/screenshot-to-code@d026163f58 (2026-08-14).
Data as JSON: /api/errors/247c26bd65f58845.
Report an issue: GitHub.