{"record":{"id":"aa7139fddc1709b9","repo":"rohitg00/agentmemory","slug":"openai-api-key-is-required-for-the-openai-provider","errorCode":null,"errorMessage":"OPENAI_API_KEY is required for the openai provider","messagePattern":"OPENAI_API_KEY is required for the openai provider","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/providers/index.ts","lineNumber":133,"sourceCode":"      }\n      return new OpenRouterProvider(\n        geminiKey,\n        config.model,\n        config.maxTokens,\n        \"https://generativelanguage.googleapis.com/v1beta/openai/chat/completions\",\n      );\n    }\n    case \"openrouter\":\n      return new OpenRouterProvider(\n        requireEnvVar(\"OPENROUTER_API_KEY\"),\n        config.model,\n        config.maxTokens,\n        \"https://openrouter.ai/api/v1/chat/completions\",\n      );\n    case \"openai\": {\n      const openaiKey = getEnvVar(\"OPENAI_API_KEY\");\n      if (!openaiKey) {\n        throw new Error(\n          \"OPENAI_API_KEY is required for the openai provider\",\n        );\n      }\n      return new OpenAIProvider(\n        openaiKey,\n        config.model,\n        config.maxTokens,\n        config.baseURL,\n      );\n    }\n    case \"noop\":\n      return new NoopProvider();\n    case \"agent-sdk\":\n    default:\n      return new AgentSDKProvider();\n  }\n}\n","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/rohitg00/agentmemory/blob/e04ba88819c365c9acf9d6661ea802143e728bd6/src/providers/index.ts#L115-L151","documentation":"createBaseProvider throws this when the provider config selects the 'openai' provider but no OPENAI_API_KEY environment variable is set. The key is read at provider-construction time via getEnvVar, before any network call, so construction fails fast. It is a deliberate config validation guard, not a runtime/auth failure.","triggerScenarios":"createProvider({ provider: 'openai', ... }) (or config defaulting to openai) with process.env.OPENAI_API_KEY unset or empty string; also triggered indirectly via createFallbackProvider or the providers() helper when the openai entry lacks a key.","commonSituations":"Fresh clone where the key exists only in a local .env file that is not loaded (dotenv not called or wrong path); CI environments that omit the secret; switching providers to 'openai' after previously using openrouter/minimax and forgetting the new env var; typo like OPEN_AI_API_KEY or OPENAI_APIKEY.","solutions":["Export OPENAI_API_KEY in the shell before starting the process: export OPENAI_API_KEY=sk-...","If the key lives in a .env file, ensure it is loaded (dotenv/config import or equivalent) and that the variable name is spelled OPENAI_API_KEY","If openai was chosen by mistake, set config.provider to the provider you actually have a key for (e.g. 'openrouter' with OPENROUTER_API_KEY)","Set the key in your CI/CD secret manager so the deployment environment has it"],"exampleFix":"// before\ncreateProvider({ provider: 'openai', model: 'gpt-4o-mini' }); // throws: OPENAI_API_KEY is required\n\n// after\nif (!process.env.OPENAI_API_KEY) throw new Error('Set OPENAI_API_KEY first');\ncreateProvider({ provider: 'openai', model: 'gpt-4o-mini', apiKeyEnv: 'OPENAI_API_KEY' });","handlingStrategy":"validation","validationCode":"if (!process.env.OPENAI_API_KEY) {\n  throw new Error('OPENAI_API_KEY must be set before using the openai provider');\n}","typeGuard":"function hasOpenAiKey(cfg: { provider: string }): boolean {\n  return cfg.provider !== 'openai' || !!process.env.OPENAI_API_KEY;\n}","tryCatchPattern":"try {\n  const provider = createProvider(config);\n} catch (e) {\n  if ((e as Error).message.includes('OPENAI_API_KEY')) {\n    console.error('Missing OPENAI_API_KEY; check .env loading and var name');\n    process.exit(1);\n  }\n  throw e;\n}","preventionTips":["Load .env at process entry (import 'dotenv/config') and validate required vars on startup","Keep a single provider-factory call site that pre-checks the key for the chosen provider","Document required env vars per provider in README/.env.example","Fail fast in CI with an env sanity-check script before running jobs"],"tags":["config","env-var","openai","provider-init"],"backgroundTag":"missing-env-var","analyzedSha":"e04ba88819c365c9acf9d6661ea802143e728bd6","analyzedAt":"2026-08-30T01:07:40.754Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}