danny-avila/LibreChat · error · Error
Missing IMAGE_GEN_OAI_API_KEY environment variable.
Error message
Missing IMAGE_GEN_OAI_API_KEY environment variable.
What it means
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.
Source
Thrown at api/app/clients/tools/structured/OpenAIImageTools.js:77
* @param {string} [fields.imageOutputType] - The image output type configuration
* @param {string} [fields.fileStrategy] - The file storage strategy
* @returns {Array<ReturnType<tool>>} - Array of image tools
*/
function createOpenAIImageTools(fields = {}) {
/** @type {boolean} Used to initialize the Tool without necessary variables. */
const override = fields.override ?? false;
/** @type {boolean} */
if (!override && !fields.isAgent) {
throw new Error('This tool is only available for agents.');
}
const { req } = fields;
const imageOutputType = fields.imageOutputType || EImageOutputType.PNG;
const appFileStrategy = fields.fileStrategy;
const getApiKey = () => {
const apiKey = process.env.IMAGE_GEN_OAI_API_KEY ?? '';
if (!apiKey && !override) {
throw new Error('Missing IMAGE_GEN_OAI_API_KEY environment variable.');
}
return apiKey;
};
let apiKey = fields.IMAGE_GEN_OAI_API_KEY ?? getApiKey();
const closureConfig = { apiKey };
const imageModel = process.env.IMAGE_GEN_OAI_MODEL || 'gpt-image-1';
let baseURL = 'https://api.openai.com/v1/';
if (!override && process.env.IMAGE_GEN_OAI_BASEURL) {
baseURL = extractBaseURL(process.env.IMAGE_GEN_OAI_BASEURL);
closureConfig.baseURL = baseURL;
}
// Note: Azure may not yet support the latest image generation models
if (
!override &&View on GitHub (pinned to 5ff282f900)
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).
Example fix
// before
const tools = createOpenAIImageTools({ req, isAgent: true }); // throws in getApiKey()
// after
// .env: IMAGE_GEN_OAI_API_KEY=sk-...
const tools = createOpenAIImageTools({ req, isAgent: true });
// or pass directly:
const tools = createOpenAIImageTools({ req, isAgent: true, IMAGE_GEN_OAI_API_KEY: key }); Defensive patterns
Strategy: validation
Validate before calling
function resolveImageGenOaiKey(fields = {}) {
const key = fields.IMAGE_GEN_OAI_API_KEY ?? process.env.IMAGE_GEN_OAI_API_KEY;
if (!key && !fields.override) {
throw new Error('Set IMAGE_GEN_OAI_API_KEY (or pass fields.IMAGE_GEN_OAI_API_KEY).');
}
return key;
} Type guard
function hasImageGenOaiKey(fields) {
return Boolean(fields?.IMAGE_GEN_OAI_API_KEY ?? process.env.IMAGE_GEN_OAI_API_KEY);
} Try / catch
try {
const tools = createOpenAIImageTools(fields);
} catch (e) {
if (/Missing IMAGE_GEN_OAI_API_KEY/.test(e.message)) return null; // skip tool
throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Missing DALLE_API_KEY environment variable.
- Missing FLUX_API_KEY environment variable.
- Missing AZURE_AI_SEARCH_SERVICE_ENDPOINT, AZURE_AI_SEARCH_IN
- Gemini Image Generation requires one of: user-provided API k
- Missing ${this.envVarApiKey} or ${this.envVarSearchEngineId}
AI-assisted analysis of danny-avila/LibreChat@5ff282f900 (2026-08-12).
Data as JSON: /api/errors/2165af0f0be81dca.
Report an issue: GitHub.