JuliusBrussee/caveman · error

cannot safely resolve Qwen's effective OPENAI_API_KEY

Error message

cannot safely resolve Qwen's effective OPENAI_API_KEY

What it means

When rendering Qwen's managed-mode config overlay, caveman must know whether OPENAI_API_KEY is effectively available so it can render optional key fields correctly. `qwenEffectiveOpenAIKeyAvailable()` returned null, meaning it could not safely determine the key's presence (ambiguous env/config resolution), so the CLI throws rather than guessing and rendering a wrong config.

Solutions

  1. Export OPENAI_API_KEY explicitly in the shell running the caveman command (`export OPENAI_API_KEY=sk-...`) so the effective availability is unambiguous, then retry
  2. If you don't intend to use an OpenAI-compatible key, explicitly unset OPENAI_API_KEY in that shell/environment so availability resolves to false instead of ambiguous
  3. Re-run the caveman command from the same interactive shell where Qwen normally runs, avoiding wrapper scripts that mutate the env

Example fix

// before
$ caveman qwen enable   # OPENAI_API_KEY only set inside ~/.qwen config
Error: cannot safely resolve Qwen's effective OPENAI_API_KEY
// after
$ export OPENAI_API_KEY=sk-...
$ caveman qwen enable
Defensive patterns

Strategy: try-catch

Validate before calling

const key = process.env.OPENAI_API_KEY;
if (key === undefined) console.warn('OPENAI_API_KEY not exported in this shell; Qwen managed-mode enable may fail key resolution');

Type guard

function openAIKeyResolvable(): boolean {
  return typeof process.env.OPENAI_API_KEY === 'string' && process.env.OPENAI_API_KEY.length > 0;
}

Try / catch

try {
  await caveman.qwen.enable();
} catch (err) {
  if (err instanceof Error && err.message.includes("cannot safely resolve Qwen's effective OPENAI_API_KEY")) {
    // export (or explicitly unset) OPENAI_API_KEY in this shell and retry
  } else throw err;
}

Prevention

When it happens

Trigger: Running the enable/render path for the Qwen agent in managed mode when the effective OPENAI_API_KEY cannot be resolved — e.g. the key is set in a context caveman cannot inspect (exported in the user's interactive shell only, set only in Qwen's own config file, or conflicting/partially-set sources) making the availability probe ambiguous (returns null).

Common situations: OPENAI_API_KEY set in ~/.qwen config but not exported to the environment (or vice versa) so caveman can't tell if Qwen will see it; key exported only inside a wrapper script/alias; using a keyless Qwen setup (OAuth/device login) where the env-var check doesn't apply cleanly.

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 JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/34621086bbc9999d. Report an issue: GitHub.

Appendix: source

Thrown at packages/cli/src/index.ts:9115

  if (!getObject(overlay, ["mcp", "servers", "caveman"])) return overlay;
  const next = cloneJsonValue(overlay);
  const mcp = getObject(next, ["mcp"])!;
  const servers = getObject(next, ["mcp", "servers"])!;
  delete servers["caveman"];
  if (Object.keys(servers).length === 0) delete mcp["servers"];
  if (Object.keys(mcp).length === 0) delete next["mcp"];
  return next;
}

function applyConfigFileInjection(env: NodeJS.ProcessEnv, agent: AgentProfile, inj: ConfigFileInjection, gw: string, modeGw = gw, mcpMode: McpSurfaceMode = "auto", agentArgs: string[] = [], upstreams?: PublishedUpstreams) {
  const baseConfig = readBaseConfig(inj);
  const mode = wrapMode(modeGw);
  const staticOverlay = mode === "managed" && inj.config_overlay.managed !== undefined ? inj.config_overlay.managed : inj.config_overlay.local;
  const builder = overlayBuilders[agent.id];
  const renderOptions: RenderDeepOptions = {};
  if (agent.id === "qwen" && mode === "managed") {
    const available = qwenEffectiveOpenAIKeyAvailable();
    if (available === null) throw new Error("cannot safely resolve Qwen's effective OPENAI_API_KEY");
    renderOptions.optionalOpenAIKeyEnvAvailable = available;
  }
  let rawOverlay = renderDeep(
    builder ? builder(agent, baseConfig, { mode, gatewayUrl: gw, env, upstreams }) : staticOverlay,
    gw,
    env,
    renderOptions,
  );
  if (agent.id === "qwen" && mcpMode === "auto") {
    const ownedMcp = ownedMcpRegistration(agent.id, agentArgs);
    if (ownedMcp) {
      // Qwen merges system settings last. Mirror only a still-journaled native
      // entry into this temporary system overlay so project settings cannot
      // replace the recovery server after Caveman has claimed it is available.
      rawOverlay = deepMerge(rawOverlay, { mcpServers: { caveman: qwenMcpEntry(ownedMcp) } });
    } else {
      // Qwen can advertise MCP schemas even when enterprise policy blocks calls.
      // Suppress the exact server in this highest-precedence temporary overlay

View on GitHub (pinned to 3ee70a1026)