Mintplex-Labs/anything-llm · error

No OpenAI API key was set.

Error message

No OpenAI API key was set.

What it means

The OpenAiEmbedder constructor throws immediately when the OPEN_AI_KEY environment variable is unset or empty. The key is required to instantiate the OpenAI SDK client used for /v1/embeddings calls; there is no default or fallback, so engine selection fails fast at construction time.

Solutions

  1. Set OPEN_AI_KEY in server .env (or via the OpenAI settings in the UI) to a valid sk-... key.
  2. Restart the server or recreate the container so the env is loaded.
  3. Verify the key works: curl https://api.openai.com/v1/embeddings with the Authorization header.
  4. Check the .env line for quotes, spaces, or an = typo.

Example fix

# .env — before
EMBEDDING_ENGINE=openai

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

Strategy: validation

Validate before calling

if (!process.env.OPEN_AI_KEY) {
  throw new Error(
    "OpenAI embedder selected but OPEN_AI_KEY is missing — set it before use."
  );
}
const { OpenAiEmbedder } = require("server/utils/EmbeddingEngines/openAi");
const embedder = new OpenAiEmbedder();

Type guard

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

Try / catch

try {
  new OpenAiEmbedder();
} catch (err) {
  if (err.message === "No OpenAI API key was set.") {
    // render a settings error pointing at OPEN_AI_KEY instead of crashing the job
  }
}

Prevention

When it happens

Trigger: Selecting the OpenAI embedding engine while OPEN_AI_KEY is unset; adding the key after the process started without a restart; a typo in the variable name; docker run without the -e flag / env_file entry; the key was cleared by re-saving provider settings.

Common situations: New installs that previously only used a local LLM; key rotation where .env was updated on one node but not others; deployments that copy .env selectively.

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/7280421e0ebbf282. Report an issue: GitHub.

Appendix: source

Thrown at server/utils/EmbeddingEngines/openAi/index.js:5

const { toChunks, reportEmbeddingProgress } = require("../../helpers");

class OpenAiEmbedder {
  constructor() {
    if (!process.env.OPEN_AI_KEY) throw new Error("No OpenAI API key was set.");
    this.className = "OpenAiEmbedder";
    const { OpenAI: OpenAIApi } = require("openai");
    this.openai = new OpenAIApi({
      apiKey: process.env.OPEN_AI_KEY,
    });
    this.model = process.env.EMBEDDING_MODEL_PREF || "text-embedding-ada-002";

    // Limit of how many strings we can process in a single pass to stay with resource or network limits
    this.maxConcurrentChunks = 500;

    // https://platform.openai.com/docs/guides/embeddings/embedding-models
    this.embeddingMaxChunkLength = 8_191;
  }

  log(text, ...args) {
    console.log(`\x1b[36m[${this.className}]\x1b[0m ${text}`, ...args);
  }

View on GitHub (pinned to 3aec848f28)