Mintplex-Labs/anything-llm · critical · Error

No OpenAI image generation API key was set.

Error message

No OpenAI image generation API key was set.

What it means

Thrown synchronously in the OpenAiImageGenerator constructor before building the OpenAI client. IMAGE_GEN_OPENAI_KEY is required because the OpenAI SDK needs it for every authenticated request. Unlike the embedding engines (which reuse the main OpenAI key), image generation uses a dedicated key variable so image and chat keys can differ.

Source

Thrown at server/utils/ImageGenerators/openAi/index.js:6

const { BaseImageGenerator } = require("../base");

class OpenAiImageGenerator extends BaseImageGenerator {
  constructor() {
    if (!process.env.IMAGE_GEN_OPENAI_KEY)
      throw new Error("No OpenAI image generation API key was set.");
    const { OpenAI: OpenAIApi } = require("openai");
    // gpt-image-1 is the default since OpenAI has deprecated the dall-e models.
    // It does not support 512x512, so base.js upsizes to the nearest supported
    // square (1024x1024) automatically.
    super({
      client: new OpenAIApi({
        apiKey: process.env.IMAGE_GEN_OPENAI_KEY,
      }),
      model: process.env.IMAGE_GEN_MODEL_PREF || "gpt-image-1",
      className: "OpenAiImageGenerator",
    });
  }

  imageFieldName(count) {
    return count > 1 ? "image[]" : "image";
  }
}

View on GitHub (pinned to 526360e320)

Solutions

  1. Set IMAGE_GEN_OPENAI_KEY to a valid OpenAI API key with image-generation access in .env.
  2. Note this is distinct from the main OPEN_AI_KEY — set the dedicated image key variable.
  3. Restart the server after setting the variable.
  4. Confirm the key's organization has access to the default model (gpt-image-1).

Example fix

# before
IMAGE_GEN_PROVIDER=openai
# (no image key)

# after
IMAGE_GEN_PROVIDER=openai
IMAGE_GEN_OPENAI_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx
Defensive patterns

Strategy: validation

Validate before calling

function hasOpenAiImageKey() {
  return typeof process.env.IMAGE_GEN_OPENAI_KEY === 'string'
    && process.env.IMAGE_GEN_OPENAI_KEY.trim().length > 0;
}
if (!hasOpenAiImageKey()) {
  throw new Error('Set IMAGE_GEN_OPENAI_KEY before using OpenAI image generation.');
}

Type guard

function isOpenAiImageKeySet() {
  return !!process.env.IMAGE_GEN_OPENAI_KEY && process.env.IMAGE_GEN_OPENAI_KEY.trim().length > 0;
}

Try / catch

try {
  generator = new OpenAiImageGenerator();
} catch (e) {
  if (/No OpenAI image generation API key/.test(e.message)) {
    throw new Error('Configure IMAGE_GEN_OPENAI_KEY (separate from OPEN_AI_KEY) and restart.');
  }
  throw e;
}

Prevention

When it happens

Trigger: Instantiating new OpenAiImageGenerator() when process.env.IMAGE_GEN_OPENAI_KEY is unset or empty.

Common situations: Selecting OpenAI as the image provider without pasting an image-capable API key; using the chat key variable name instead of IMAGE_GEN_OPENAI_KEY; key expired or revoked on the OpenAI dashboard; container env missing the variable.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/d3ff669f92c1dcd3. Report an issue: GitHub.