Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix(guardrails/vision-bridge): env override for non-Anthropic endpoint (#2232)

diegosouzapw7fb5505b124c
diff --git a/.env.example b/.env.example
index dd624b0227..9c6089d7ec 100644
--- a/.env.example
+++ b/.env.example
@@ -544,6 +544,22 @@ GITHUB_OAUTH_CLIENT_ID=Iv1.b507a08c87ecfe98
# to bypass the random-UUID fallback. Leave empty to keep the legacy behavior.
# BLACKBOX_WEB_VALIDATED_TOKEN=
+# ── Vision Bridge OpenAI-compatible endpoint override (issue #2232) ──
+# Used by: src/lib/guardrails/visionBridgeHelpers.ts. By default the
+# vision-bridge guardrail sends non-Anthropic image-description calls to
+# `https://api.openai.com/v1`, which fails with 401 if your operator doesn't
+# have an OpenAI key or wants to use a different vision model
+# (e.g., `google/gemini-2.0-flash` via the Gemini OpenAI-compat endpoint, or
+# any model registered in OmniRoute via the self-loop endpoint).
+#
+# Set these two env vars to point the bridge at any OpenAI-compatible URL:
+# - VISION_BRIDGE_BASE_URL=http://localhost:20128/v1 (OmniRoute self-loop)
+# - VISION_BRIDGE_BASE_URL=https://generativelanguage.googleapis.com/v1beta/openai
+# - VISION_BRIDGE_BASE_URL=https://openrouter.ai/api/v1
+# Anthropic models (anthropic/*) keep their dedicated path and are unaffected.
+# VISION_BRIDGE_BASE_URL=
+# VISION_BRIDGE_API_KEY=
+
# ─────────────────────────────────────────────────────────────────────────────
# ⚠️ GOOGLE OAUTH (Antigravity, Gemini CLI) & OTHER PROVIDERS — REMOTE SERVERS
# ─────────────────────────────────────────────────────────────────────────────
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 65a1357cb7..7f8dbd15ab 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,6 +35,7 @@
- **fix(providers/qoder):** disambiguate the "Local CLI runtime is not installed" error when a user pastes a Personal Access Token but the connection is in OAuth/CLI-flavored mode. The test route now surfaces a single actionable message ("switch this connection to API Key auth") instead of cascading CLI + 401 errors. (#2247)
- **fix(dashboard/api-manager):** route custom OpenAI-/Anthropic-compatible provider IDs through `getProviderDisplayName` so the model grouping label shows `Compatible (openai)` instead of leaking the raw synthetic `openai-compatible-chat-<uuid>` value. (#2021)
- **fix(providers/blackbox-web):** add `BLACKBOX_WEB_VALIDATED_TOKEN` env override and 403 token-error disambiguation. Blackbox `/api/chat` started rejecting requests whose `validated` field didn't match the frontend `tk` token, even with a valid cookie + active subscription. Operators with the real token can now set the env var; otherwise the previous random-UUID fallback still ships, and a 403 with a token-specific body now surfaces a one-line "set BLACKBOX_WEB_VALIDATED_TOKEN" hint instead of the generic "cookie expired" message. (#2252)
+- **fix(guardrails/vision-bridge):** add `VISION_BRIDGE_BASE_URL` + `VISION_BRIDGE_API_KEY` env overrides so non-Anthropic vision-bridge calls can be routed through OmniRoute's own `/v1` self-loop, Google's Gemini OpenAI-compat endpoint, OpenRouter, or any other OpenAI-compatible URL — instead of being hardcoded to `https://api.openai.com/v1` (which failed with 401 for users without an OpenAI key, even when they configured `visionBridgeModel: "google/gemini-2.0-flash"`). Anthropic models keep their dedicated path. (#2232)
- **docs(security):** document the ToS-violation hot spot of `ANTIGRAVITY_CREDITS=always` in `STEALTH_GUIDE.md`, including why it draws Google abuse detection more aggressively than free-tier-only usage and the recommended posture (`=retry`, Auto-Combo spread, per-connection RPM caps). (#2246)
### Changed
diff --git a/docs/reference/ENVIRONMENT.md b/docs/reference/ENVIRONMENT.md
index 4481bbb02b..e468b054ce 100644
--- a/docs/reference/ENVIRONMENT.md
+++ b/docs/reference/ENVIRONMENT.md
@@ -369,6 +369,8 @@ Built-in credentials for **localhost development**. For remote deployments, regi
| `QODER_CLI_WORKSPACE` | Qoder | Workspace ID for Qoder CLI. |
| `OMNIROUTE_QODER_WORKSPACE` | Qoder | Alias for `QODER_CLI_WORKSPACE`. |
| `BLACKBOX_WEB_VALIDATED_TOKEN` | Blackbox Web | Frontend `tk` token to send as `validated` on `/api/chat`. Required when Blackbox enforces token matching; otherwise OmniRoute falls back to a random UUID. See issue #2252. |
+| `VISION_BRIDGE_BASE_URL` | Vision Bridge guardrail | OpenAI-compatible base URL for non-Anthropic vision-bridge calls. Defaults to the legacy OpenAI URL env or api.openai.com. Point at OmniRoute's `/v1` self-loop or any OpenAI-compat endpoint (Gemini OpenAI-compat, OpenRouter). Issue #2232. |
+| `VISION_BRIDGE_API_KEY` | Vision Bridge guardrail | API key for the URL above. Overrides per-provider OpenAI / Google env vars for non-Anthropic vision-bridge calls. Anthropic models keep their dedicated Anthropic key path. Issue #2232. |
> [!WARNING]
> **Google OAuth** (Antigravity, Gemini CLI) credentials **only work on localhost**. For remote servers:
diff --git a/src/lib/guardrails/visionBridgeHelpers.ts b/src/lib/guardrails/visionBridgeHelpers.ts
index 32fd9bf151..938ffbd600 100644
--- a/src/lib/guardrails/visionBridgeHelpers.ts
+++ b/src/lib/guardrails/visionBridgeHelpers.ts
@@ -13,18 +13,57 @@ const PROVIDER_API_KEY_MAP: Record<string, string> = {
};
/**
- * Resolve API key based on model provider.
+ * Resolve API key based on model provider (issue #2232).
+ *
+ * Priority:
+ * 1. `explicitKey` argument (caller override)
+ * 2. `VISION_BRIDGE_API_KEY` env var — operator-set, takes precedence over
+ * per-provider env vars. Used when the operator wants every vision-bridge
+ * call to go through a single OpenAI-compatible endpoint (e.g.,
+ * OmniRoute itself, OpenRouter, a Gemini-OpenAI-compat URL).
+ * 3. Per-provider env var (`ANTHROPIC_API_KEY`, `GOOGLE_API_KEY`,
+ * `OPENAI_API_KEY`) based on the `provider/` prefix in the model id.
+ * 4. `OPENAI_API_KEY` as final fallback when the prefix is unrecognized.
+ *
* @param model - Model identifier (e.g., "anthropic/claude-3-haiku", "openai/gpt-4o-mini")
* @param explicitKey - Explicit API key passed as argument (takes precedence)
* @returns Resolved API key string
*/
export function resolveProviderApiKey(model: string, explicitKey?: string): string {
if (explicitKey) return explicitKey;
+ const isAnthropic = model.startsWith("anthropic/");
+ // VISION_BRIDGE_API_KEY only applies to the OpenAI-compatible branch — the
+ // Anthropic branch keeps its dedicated key, since the wire format differs.
+ if (!isAnthropic) {
+ const bridgeKey = (process.env.VISION_BRIDGE_API_KEY || "").trim();
+ if (bridgeKey) return bridgeKey;
+ }
const provider = model.includes("/") ? model.split("/")[0] : "";
const envVar = PROVIDER_API_KEY_MAP[provider] || "OPENAI_API_KEY";
return process.env[envVar] || "";
}
+/**
+ * Resolve the OpenAI-compatible base URL for non-Anthropic vision bridge calls
+ * (issue #2232).
+ *
+ * Priority:
+ * 1. `VISION_BRIDGE_BASE_URL` env var — operator-set, e.g. point this at
+ * OmniRoute's own `/v1` so the vision model can be any provider
+ * registered in OmniRoute (`google/gemini-2.0-flash`,
+ * `openrouter/...`, etc.) instead of being limited to OpenAI/Anthropic.
+ * 2. `OPENAI_API_URL` env var (legacy)
+ * 3. `https://api.openai.com/v1` (default — works only when the operator
+ * actually has an OpenAI account and OPENAI_API_KEY set)
+ */
+export function resolveVisionBridgeBaseUrl(): string {
+ const explicit = (process.env.VISION_BRIDGE_BASE_URL || "").trim();
+ if (explicit) return explicit.replace(/\/+$/, "");
+ const legacy = (process.env.OPENAI_API_URL || "").trim();
+ if (legacy) return legacy.replace(/\/+$/, "");
+ return "https://api.openai.com/v1";
+}
+
export interface ImagePart {
messageIndex: number;
partIndex: number;
@@ -217,8 +256,11 @@ export async function callVisionModel(
}),
});
} else {
- // OpenAI-compatible path (default)
- const baseUrl = process.env.OPENAI_API_URL || "https://api.openai.com/v1";
+ // OpenAI-compatible path (default) — issue #2232: honor
+ // VISION_BRIDGE_BASE_URL so the vision-bridge call can be routed through
+ // OmniRoute itself or any other OpenAI-compatible endpoint instead of
+ // hardcoded api.openai.com.
+ const baseUrl = resolveVisionBridgeBaseUrl();
response = await fetch(`${baseUrl}/chat/completions`, {
method: "POST",
diff --git a/tests/unit/vision-bridge-env-override.test.ts b/tests/unit/vision-bridge-env-override.test.ts
new file mode 100644
index 0000000000..bebc771a18
--- /dev/null
+++ b/tests/unit/vision-bridge-env-override.test.ts
@@ -0,0 +1,131 @@
+/**
+ * Issue #2232 — Vision Bridge env override.
+ *
+ * Operators configured `visionBridgeModel: "gemini/gemini-2.0-flash"` (or
+ * other non-anthropic prefixes) and watched every request fail with
+ * `Vision API error 401: You didn't provide an API key` from OpenAI, because
+ * the helper hardcoded `https://api.openai.com/v1` for any model that
+ * wasn't `anthropic/*`.
+ *
+ * The fix adds two new env vars:
+ * - VISION_BRIDGE_BASE_URL: alternate OpenAI-compatible base URL (e.g.
+ * OmniRoute self-loop, Google's Gemini OpenAI-compat endpoint).
+ * - VISION_BRIDGE_API_KEY: alternate API key for that endpoint.
+ *
+ * These tests cover the helpers in isolation; the integration with the
+ * guardrail's `callVisionModel` is exercised by the existing
+ * vision-bridge-settings-schema tests.
+ */
+
+import test from "node:test";
+import assert from "node:assert/strict";
+
+const { resolveProviderApiKey, resolveVisionBridgeBaseUrl } =
+ await import("../../src/lib/guardrails/visionBridgeHelpers.ts");
+
+const ENV_KEYS = [
+ "VISION_BRIDGE_API_KEY",
+ "VISION_BRIDGE_BASE_URL",
+ "OPENAI_API_URL",
+ "OPENAI_API_KEY",
+ "ANTHROPIC_API_KEY",
+ "GOOGLE_API_KEY",
+];
+
+const ORIGINAL_ENV: Record<string, string | undefined> = {};
+for (const k of ENV_KEYS) ORIGINAL_ENV[k] = process.env[k];
+
+function clearEnv() {
+ for (const k of ENV_KEYS) delete process.env[k];
+}
+
+function restoreEnv() {
+ for (const k of ENV_KEYS) {
+ if (ORIGINAL_ENV[k] === undefined) delete process.env[k];
+ else process.env[k] = ORIGINAL_ENV[k];
+ }
+}
+
+test.afterEach(restoreEnv);
+
+// ─── resolveVisionBridgeBaseUrl ───────────────────────────────────────────
+
+test("#2232 — VISION_BRIDGE_BASE_URL takes precedence over OPENAI_API_URL", () => {
+ clearEnv();
+ process.env.VISION_BRIDGE_BASE_URL = "http://localhost:20128/v1";
+ process.env.OPENAI_API_URL = "https://oai.example.com/v1";
+ assert.equal(resolveVisionBridgeBaseUrl(), "http://localhost:20128/v1");
+});
+
+test("#2232 — falls back to OPENAI_API_URL when VISION_BRIDGE_BASE_URL is unset", () => {
+ clearEnv();
+ process.env.OPENAI_API_URL = "https://oai.example.com/v1";
+ assert.equal(resolveVisionBridgeBaseUrl(), "https://oai.example.com/v1");
+});
+
+test("#2232 — defaults to api.openai.com when both env vars are unset", () => {
+ clearEnv();
+ assert.equal(resolveVisionBridgeBaseUrl(), "https://api.openai.com/v1");
+});
+
+test("#2232 — trailing slashes on VISION_BRIDGE_BASE_URL are stripped", () => {
+ clearEnv();
+ process.env.VISION_BRIDGE_BASE_URL = "http://localhost:20128/v1///";
+ assert.equal(resolveVisionBridgeBaseUrl(), "http://localhost:20128/v1");
+});
+
+test("#2232 — whitespace-only VISION_BRIDGE_BASE_URL falls through to OPENAI_API_URL", () => {
+ clearEnv();
+ process.env.VISION_BRIDGE_BASE_URL = " ";
+ process.env.OPENAI_API_URL = "https://oai.example.com/v1";
+ assert.equal(resolveVisionBridgeBaseUrl(), "https://oai.example.com/v1");
+});
+
+// ─── resolveProviderApiKey ────────────────────────────────────────────────
+
+test("#2232 — VISION_BRIDGE_API_KEY wins for non-anthropic models", () => {
+ clearEnv();
+ process.env.VISION_BRIDGE_API_KEY = "stub-vision-bridge-key";
+ process.env.OPENAI_API_KEY = "stub-openai-key";
+ process.env.GOOGLE_API_KEY = "stub-google-key";
+ assert.equal(resolveProviderApiKey("gemini/gemini-2.0-flash"), "stub-vision-bridge-key");
+ assert.equal(resolveProviderApiKey("openrouter/nvidia/foo"), "stub-vision-bridge-key");
+ assert.equal(resolveProviderApiKey("openai/gpt-4o"), "stub-vision-bridge-key");
+});
+
+test("#2232 — Anthropic models ignore VISION_BRIDGE_API_KEY (wire format differs)", () => {
+ clearEnv();
+ process.env.VISION_BRIDGE_API_KEY = "stub-vision-bridge-key";
+ process.env.ANTHROPIC_API_KEY = "stub-anthropic-key";
+ assert.equal(resolveProviderApiKey("anthropic/claude-3-haiku"), "stub-anthropic-key");
+});
+
+test("#2232 — explicit apiKey wins over VISION_BRIDGE_API_KEY", () => {
+ clearEnv();
+ process.env.VISION_BRIDGE_API_KEY = "stub-vision-bridge-key";
+ assert.equal(
+ resolveProviderApiKey("gemini/gemini-2.0-flash", "stub-explicit-key"),
+ "stub-explicit-key"
+ );
+});
+
+test("#2232 — without VISION_BRIDGE_API_KEY, falls back to provider-specific env", () => {
+ clearEnv();
+ process.env.GOOGLE_API_KEY = "stub-google-key";
+ process.env.OPENAI_API_KEY = "stub-openai-key";
+ assert.equal(resolveProviderApiKey("google/gemini-pro"), "stub-google-key");
+ assert.equal(resolveProviderApiKey("openai/gpt-4o"), "stub-openai-key");
+});
+
+test("#2232 — empty VISION_BRIDGE_API_KEY falls through to provider-specific env", () => {
+ clearEnv();
+ process.env.VISION_BRIDGE_API_KEY = " ";
+ process.env.OPENAI_API_KEY = "stub-openai-key";
+ assert.equal(resolveProviderApiKey("openai/gpt-4o"), "stub-openai-key");
+});
+
+test("#2232 — unrecognized prefix without VISION_BRIDGE_API_KEY falls back to OPENAI_API_KEY", () => {
+ clearEnv();
+ process.env.OPENAI_API_KEY = "stub-openai-fallback";
+ assert.equal(resolveProviderApiKey("nonexistent/foo-bar"), "stub-openai-fallback");
+});