{"record":{"id":"247c26bd65f58845","repo":"abi/screenshot-to-code","slug":"openai-api-key-is-missing","errorCode":null,"errorMessage":"OpenAI API key is missing.","messagePattern":"OpenAI API key is missing\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"backend/agent/providers/factory.py","lineNumber":43,"sourceCode":"    anthropic_api_key: Optional[str],\n    gemini_api_key: Optional[str],\n    replicate_api_key: Optional[str],\n    should_extract_assets: bool = True,\n    recorder: Optional[AgentRunRecorder] = None,\n) -> ProviderSession:\n    canonical_tools = canonical_tool_definitions(\n        image_generation_enabled=should_generate_images,\n        # The edit_images tool calls Replicate, so don't offer it without a key.\n        image_editing_enabled=bool(replicate_api_key or REPLICATE_API_KEY),\n        # The extract_assets tool calls Gemini, so don't offer it without a key.\n        asset_extraction_enabled=should_extract_assets and bool(gemini_api_key),\n        # screenshot_preview needs headless Chromium; skip it if it can't launch.\n        screenshot_enabled=is_screenshot_preview_available(),\n    )\n\n    if model in OPENAI_MODELS:\n        if not openai_api_key:\n            raise Exception(\"OpenAI API key is missing.\")\n\n        client = AsyncOpenAI(api_key=openai_api_key, base_url=openai_base_url)\n        return OpenAIProviderSession(\n            client=client,\n            model=model,\n            prompt_messages=prompt_messages,\n            tools=serialize_openai_tools(canonical_tools),\n            recorder=recorder,\n        )\n\n    if model in ANTHROPIC_MODELS:\n        if not anthropic_api_key:\n            raise Exception(\"Anthropic API key is missing.\")\n\n        client = AsyncAnthropic(api_key=anthropic_api_key)\n        return AnthropicProviderSession(\n            client=client,\n            model=model,","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/abi/screenshot-to-code/blob/d026163f586dfa8c5c10d28c36edd59a9d3b0e88/backend/agent/providers/factory.py#L25-L61","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"# backend/.env\nOPENAI_API_KEY=sk-...","handlingStrategy":"validation","validationCode":"import os\nif model in OPENAI_MODELS and not os.environ.get(\"OPENAI_API_KEY\"):\n    raise SystemExit(\"Set OPENAI_API_KEY in backend/.env before using OpenAI models\")","typeGuard":null,"tryCatchPattern":"try:\n    session = create_provider_session(model=model, ...)\nexcept Exception as e:\n    if \"API key is missing\" in str(e):\n        # prompt the user to open Settings / configure .env\n        ...","preventionTips":["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."],"tags":["api-key","openai","config","auth"],"backgroundTag":null,"analyzedSha":"d026163f586dfa8c5c10d28c36edd59a9d3b0e88","analyzedAt":"2026-08-14T22:02:06.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}