Mintplex-Labs/anything-llm · error

No OpenAI image generation API key was set.

Error message

No OpenAI image generation API key was set.

What it means

The OpenAiImageGenerator constructor throws immediately when IMAGE_GEN_OPENAI_KEY is unset or empty. Note this is a separate variable from the chat/embedding OPEN_AI_KEY — image generation has its own credential. The key instantiates the OpenAI SDK client (default model gpt-image-1), so selection fails fast at construction.

Solutions

  1. Set IMAGE_GEN_OPENAI_KEY (not OPEN_AI_KEY) in .env to a valid OpenAI key.
  2. Confirm the key's project/org has access to the images API and gpt-image-1.
  3. Restart the server / recreate the container after saving.
  4. Check the variable spelling and value for stray quotes or spaces.

Example fix

# .env — before
IMAGE_GEN_ENGINE=openai
OPEN_AI_KEY=sk-...

# .env — after
IMAGE_GEN_ENGINE=openai
OPEN_AI_KEY=sk-...
IMAGE_GEN_OPENAI_KEY=sk-proj-...
Defensive patterns

Strategy: validation

Validate before calling

if (!process.env.IMAGE_GEN_OPENAI_KEY) {
  throw new Error(
    "OpenAI image engine selected but IMAGE_GEN_OPENAI_KEY is missing (this is separate from OPEN_AI_KEY)."
  );
}
const { OpenAiImageGenerator } = require("server/utils/ImageGenerators/openAi");
const generator = new OpenAiImageGenerator();

Type guard

function hasOpenAiImageGenConfig(env = process.env) {
  return (
    typeof env.IMAGE_GEN_OPENAI_KEY === "string" &&
    env.IMAGE_GEN_OPENAI_KEY.length > 0
  );
}

Try / catch

try {
  new OpenAiImageGenerator();
} catch (err) {
  if (err.message === "No OpenAI image generation API key was set.") {
    // route the user to image-gen settings; do not assume OPEN_AI_KEY covers it
  }
}

Prevention

When it happens

Trigger: Choosing OpenAI image generation while IMAGE_GEN_OPENAI_KEY is unset; setting only OPEN_AI_KEY and assuming image gen shares it; the key was added after the process started; docker env not propagated.

Common situations: Users who configured the OpenAI chat provider and expected image generation to reuse the same key; org keys restricted from the images API.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


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

Appendix: 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 3aec848f28)