{"record":{"id":"00567da6bb88d408","repo":"rohitg00/agentmemory","slug":"gemini-api-key-is-required","errorCode":null,"errorMessage":"GEMINI_API_KEY is required","messagePattern":"GEMINI_API_KEY is required","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/providers/embedding/gemini.ts","lineNumber":16,"sourceCode":"import type { EmbeddingProvider } from \"../../types.js\";\nimport { getEnvVar } from \"../../config.js\";\nimport { fetchWithTimeout } from \"../_fetch.js\";\n\nconst BATCH_LIMIT = 100;\nconst MODEL = \"models/gemini-embedding-001\";\nconst API_BASE = `https://generativelanguage.googleapis.com/v1beta/${MODEL}:batchEmbedContents`;\n\nexport class GeminiEmbeddingProvider implements EmbeddingProvider {\n  readonly name = \"gemini\";\n  readonly dimensions = 768;\n  private apiKey: string;\n\n  constructor(apiKey?: string) {\n    this.apiKey = apiKey || getEnvVar(\"GEMINI_API_KEY\") || \"\";\n    if (!this.apiKey) throw new Error(\"GEMINI_API_KEY is required\");\n  }\n\n  async embed(text: string): Promise<Float32Array> {\n    const [result] = await this.embedBatch([text]);\n    return result;\n  }\n\n  async embedBatch(texts: string[]): Promise<Float32Array[]> {\n    const results: Float32Array[] = [];\n\n    for (let i = 0; i < texts.length; i += BATCH_LIMIT) {\n      const chunk = texts.slice(i, i + BATCH_LIMIT);\n      const response = await fetchWithTimeout(`${API_BASE}?key=${this.apiKey}`, {\n        method: \"POST\",\n        headers: { \"Content-Type\": \"application/json\" },\n        body: JSON.stringify({\n          requests: chunk.map((t) => ({\n            model: MODEL,","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/rohitg00/agentmemory/blob/e04ba88819c365c9acf9d6661ea802143e728bd6/src/providers/embedding/gemini.ts#L1-L34","documentation":"The Gemini embedding provider's constructor requires an API key from the `apiKey` argument or the GEMINI_API_KEY env var, and throws at construction if neither yields a non-empty value. This is fail-fast validation so misconfiguration is caught before any network call is attempted.","triggerScenarios":"Instantiating the Gemini embedding provider with no explicit key while GEMINI_API_KEY is unset, empty, or whitespace — e.g. after a fresh checkout, a container image without the secret mounted, or a renamed env variable.","commonSituations":"Google AI Studio key never created or exported; secret not injected into the deployment (k8s secret missing, CI env not configured); using GOOGLE_API_KEY or GEMINI_KEY naming instead of GEMINI_API_KEY; dotenv loading after provider construction at module import time.","solutions":["Export the key: `export GEMINI_API_KEY=your-key` (create one at aistudio.google.com/apikey).","Pass the key explicitly to the constructor once confirmed non-empty: `new GeminiProvider(process.env.GEMINI_API_KEY)`.","Confirm the exact variable name GEMINI_API_KEY and that env loading (dotenv/.env file) happens before the provider module is imported.","If you didn't intend Gemini, configure/construct a different embedding provider instead."],"exampleFix":"// before\nconst provider = new GeminiProvider(); // throws: GEMINI_API_KEY is required\n// after\n// .env: GEMINI_API_KEY=AIza...\nrequire(\"dotenv\").config();\nconst provider = new GeminiProvider(process.env.GEMINI_API_KEY);","handlingStrategy":"validation","validationCode":"function requireEnv(name: string): string {\n  const v = process.env[name];\n  if (!v || v.trim() === \"\") throw new Error(`${name} is not set`);\n  return v.trim();\n}\nconst key = requireEnv(\"GEMINI_API_KEY\"); // run before constructing GeminiProvider","typeGuard":"function hasApiKey(v: unknown): v is string {\n  return typeof v === \"string\" && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  provider = new GeminiProvider();\n} catch (e) {\n  if (e instanceof Error && e.message === \"GEMINI_API_KEY is required\") {\n    console.error(\"Set GEMINI_API_KEY (Google AI Studio) before starting\");\n    process.exit(1);\n  }\n  throw e;\n}","preventionTips":["List GEMINI_API_KEY in .env.example and load .env at the very top of the entrypoint.","Ensure env vars are set before module import — provider constructors can run at import time.","Use the exact variable name GEMINI_API_KEY in deployments and secret managers.","Add a startup assertion that all required provider keys exist."],"tags":["missing-env-var","api-key","configuration","embeddings"],"backgroundTag":"missing-env-var","analyzedSha":"e04ba88819c365c9acf9d6661ea802143e728bd6","analyzedAt":"2026-08-30T01:07:40.754Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}