{"record":{"id":"47bfe41ea8c6835b","repo":"rohitg00/agentmemory","slug":"api-key-is-required-via-constructor-openai-embed","errorCode":null,"errorMessage":"API key is required (via constructor, OPENAI_EMBEDDING_API_KEY, or OPENAI_API_KEY)","messagePattern":"API key is required \\(via constructor, OPENAI_EMBEDDING_API_KEY, or OPENAI_API_KEY\\)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/providers/embedding/openai.ts","lineNumber":70,"sourceCode":"  readonly name = \"openai\";\n  readonly dimensions: number;\n  private apiKey: string;\n  private baseUrl: string;\n  private model: string;\n  private isAzure: boolean;\n  private azureApiVersion: string;\n\n  constructor(apiKey?: string) {\n    // Separate API key path: caller-passed wins, then OPENAI_EMBEDDING_API_KEY,\n    // then fall back to OPENAI_API_KEY. Allows e.g. a placeholder key for\n    // local endpoints that ignore Authorization (most do).\n    this.apiKey =\n      apiKey ||\n      getEnvVar(\"OPENAI_EMBEDDING_API_KEY\") ||\n      getEnvVar(\"OPENAI_API_KEY\") ||\n      \"\";\n    if (!this.apiKey) {\n      throw new Error(\n        \"API key is required (via constructor, OPENAI_EMBEDDING_API_KEY, or OPENAI_API_KEY)\",\n      );\n    }\n    // Embedding-specific base URL override; falls back to OPENAI_BASE_URL,\n    // then normalizeBaseUrl's default. The chat-LLM path (src/providers/openai.ts)\n    // still reads only OPENAI_BASE_URL, so setting OPENAI_EMBEDDING_BASE_URL\n    // alone moves embeddings to the new endpoint without affecting chat.\n    this.baseUrl = normalizeBaseUrl(\n      getEnvVar(\"OPENAI_EMBEDDING_BASE_URL\") || getEnvVar(\"OPENAI_BASE_URL\"),\n    );\n    this.model = getEnvVar(\"OPENAI_EMBEDDING_MODEL\") || DEFAULT_MODEL;\n    this.dimensions = resolveDimensions(\n      this.model,\n      getEnvVar(\"OPENAI_EMBEDDING_DIMENSIONS\"),\n      \"OPENAI_EMBEDDING_DIMENSIONS\",\n    );\n    this.isAzure = detectAzure(this.baseUrl);\n    this.azureApiVersion =","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/rohitg00/agentmemory/blob/e04ba88819c365c9acf9d6661ea802143e728bd6/src/providers/embedding/openai.ts#L52-L88","documentation":"OpenAIEmbeddingProvider requires an API key and resolves it from the constructor argument, then OPENAI_EMBEDDING_API_KEY, then OPENAI_API_KEY. If all three are empty it throws immediately in the constructor so misconfiguration fails fast rather than at first network call.","triggerScenarios":"Constructing `new OpenAIEmbeddingProvider()` (or via createEmbeddingProvider/createProvider factory) with no apiKey argument while neither OPENAI_EMBEDDING_API_KEY nor OPENAI_API_KEY is set in the environment or ~/.agentmemory/.env.","commonSituations":"Docker container launched without passing env vars; .env file located outside the expected ~/.agentmemory/ path; key set only for the chat provider file scope but not exported in the shell; typo like OPENAI_APIKEY; using the npm package in a fresh machine where credentials were never provisioned.","solutions":["Export OPENAI_API_KEY (or OPENAI_EMBEDDING_API_KEY) in your environment, or add it to ~/.agentmemory/.env","Pass the key explicitly: new OpenAIEmbeddingProvider(process.env.MY_KEY)","Verify the .env file is actually loaded (correct path ~/.agentmemory/.env, correct loader) and the variable name is spelled exactly","If using a keyless local gateway, still set a dummy key plus OPENAI_EMBEDDING_BASE_URL pointing at the gateway"],"exampleFix":"// before\nconst provider = new OpenAIEmbeddingProvider(); // no key anywhere\n// after\n// ~/.agentmemory/.env: OPENAI_API_KEY=sk-...\nconst provider = new OpenAIEmbeddingProvider(); // or new OpenAIEmbeddingProvider(process.env.OPENAI_API_KEY)","handlingStrategy":"validation","validationCode":"function requireOpenAiKey(): string {\n  const key = process.env.OPENAI_EMBEDDING_API_KEY || process.env.OPENAI_API_KEY;\n  if (!key) throw new Error('Set OPENAI_API_KEY (or OPENAI_EMBEDDING_API_KEY) before creating the provider');\n  return key;\n}\nconst provider = new OpenAIEmbeddingProvider(requireOpenAiKey());","typeGuard":"const hasApiKey = (k: string | undefined): k is string => typeof k === 'string' && k.trim().length > 0;","tryCatchPattern":"let provider: OpenAIEmbeddingProvider;\ntry {\n  provider = new OpenAIEmbeddingProvider();\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('API key is required')) {\n    console.error('Missing OpenAI credentials: set OPENAI_API_KEY in env or ~/.agentmemory/.env');\n    process.exit(1);\n  }\n  throw err;\n}","preventionTips":["Provision secrets before service start (entrypoint check for required env vars)","Keep keys in ~/.agentmemory/.env with consistent naming across environments","Validate all required env vars at boot with a fail-fast check","Never rely on a shell variable that isn't exported to child processes"],"tags":["api-key","missing-env-var","openai","configuration"],"backgroundTag":"missing-api-key","analyzedSha":"e04ba88819c365c9acf9d6661ea802143e728bd6","analyzedAt":"2026-08-30T01:07:40.754Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}