{"record":{"id":"2165af0f0be81dca","repo":"danny-avila/LibreChat","slug":"missing-image-gen-oai-api-key-environment-variable","errorCode":null,"errorMessage":"Missing IMAGE_GEN_OAI_API_KEY environment variable.","messagePattern":"Missing IMAGE_GEN_OAI_API_KEY environment variable\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"api/app/clients/tools/structured/OpenAIImageTools.js","lineNumber":77,"sourceCode":" * @param {string} [fields.imageOutputType] - The image output type configuration\n * @param {string} [fields.fileStrategy] - The file storage strategy\n * @returns {Array<ReturnType<tool>>} - Array of image tools\n */\nfunction createOpenAIImageTools(fields = {}) {\n  /** @type {boolean} Used to initialize the Tool without necessary variables. */\n  const override = fields.override ?? false;\n  /** @type {boolean} */\n  if (!override && !fields.isAgent) {\n    throw new Error('This tool is only available for agents.');\n  }\n  const { req } = fields;\n  const imageOutputType = fields.imageOutputType || EImageOutputType.PNG;\n  const appFileStrategy = fields.fileStrategy;\n\n  const getApiKey = () => {\n    const apiKey = process.env.IMAGE_GEN_OAI_API_KEY ?? '';\n    if (!apiKey && !override) {\n      throw new Error('Missing IMAGE_GEN_OAI_API_KEY environment variable.');\n    }\n    return apiKey;\n  };\n\n  let apiKey = fields.IMAGE_GEN_OAI_API_KEY ?? getApiKey();\n  const closureConfig = { apiKey };\n\n  const imageModel = process.env.IMAGE_GEN_OAI_MODEL || 'gpt-image-1';\n\n  let baseURL = 'https://api.openai.com/v1/';\n  if (!override && process.env.IMAGE_GEN_OAI_BASEURL) {\n    baseURL = extractBaseURL(process.env.IMAGE_GEN_OAI_BASEURL);\n    closureConfig.baseURL = baseURL;\n  }\n\n  // Note: Azure may not yet support the latest image generation models\n  if (\n    !override &&","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/danny-avila/LibreChat/blob/5ff282f9006c436e561de1afd39a481bea1ef0d8/api/app/clients/tools/structured/OpenAIImageTools.js#L59-L95","documentation":"Thrown by the closure-scoped getApiKey() inside createOpenAIImageTools when neither `fields.IMAGE_GEN_OAI_API_KEY` nor `process.env.IMAGE_GEN_OAI_API_KEY` is present and override is false. The factory resolves `fields.IMAGE_GEN_OAI_API_KEY ?? getApiKey()`, so an explicitly passed field bypasses the env check entirely.","triggerScenarios":"Building the OpenAI image tools (which calls getApiKey during factory setup) when IMAGE_GEN_OAI_API_KEY is unset in both the fields argument and process.env, and `fields.override` is not true.","commonSituations":"Using OpenAI image generation (gpt-image-1) without setting the dedicated IMAGE_GEN_OAI_API_KEY var (it is separate from OPENAI_API_KEY); pointing the tool at Azure/OpenAI-compatible base URL via IMAGE_GEN_OAI_BASEURL but forgetting the key; secret rotation that left the var empty; deploy without the secret injected.","solutions":["Set `IMAGE_GEN_OAI_API_KEY=<key>` in .env and restart the server.","Pass the key explicitly via `fields.IMAGE_GEN_OAI_API_KEY` if you resolve it from a secret manager at runtime.","Use `override: true` if you are only registering the tool for the manifest and will not call it.","Verify the variable name is exactly IMAGE_GEN_OAI_API_KEY (not IMAGE_GEN_API_KEY or OPENAI_IMAGE_KEY)."],"exampleFix":"// before\nconst tools = createOpenAIImageTools({ req, isAgent: true }); // throws in getApiKey()\n\n// after\n// .env: IMAGE_GEN_OAI_API_KEY=sk-...\nconst tools = createOpenAIImageTools({ req, isAgent: true });\n// or pass directly:\nconst tools = createOpenAIImageTools({ req, isAgent: true, IMAGE_GEN_OAI_API_KEY: key });","handlingStrategy":"validation","validationCode":"function resolveImageGenOaiKey(fields = {}) {\n  const key = fields.IMAGE_GEN_OAI_API_KEY ?? process.env.IMAGE_GEN_OAI_API_KEY;\n  if (!key && !fields.override) {\n    throw new Error('Set IMAGE_GEN_OAI_API_KEY (or pass fields.IMAGE_GEN_OAI_API_KEY).');\n  }\n  return key;\n}","typeGuard":"function hasImageGenOaiKey(fields) {\n  return Boolean(fields?.IMAGE_GEN_OAI_API_KEY ?? process.env.IMAGE_GEN_OAI_API_KEY);\n}","tryCatchPattern":"try {\n  const tools = createOpenAIImageTools(fields);\n} catch (e) {\n  if (/Missing IMAGE_GEN_OAI_API_KEY/.test(e.message)) return null; // skip tool\n  throw e;\n}","preventionTips":["Use the exact var name IMAGE_GEN_OAI_API_KEY; it is distinct from OPENAI_API_KEY.","Inject the key from a secret manager into fields.IMAGE_GEN_OAI_API_KEY at runtime.","Validate the key at startup alongside other tool credentials."],"tags":["openai-image","config","environment-variables","api-key"],"backgroundTag":null,"analyzedSha":"5ff282f9006c436e561de1afd39a481bea1ef0d8","analyzedAt":"2026-08-12T21:38:08.145Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}