Skip to content

Comments

fix: support custom base URL for Gemini provider#520

Merged
kevin-on merged 2 commits intomainfrom
fix/issue-512
Jan 25, 2026
Merged

fix: support custom base URL for Gemini provider#520
kevin-on merged 2 commits intomainfrom
fix/issue-512

Conversation

@kevin-on
Copy link
Collaborator

@kevin-on kevin-on commented Jan 25, 2026

Fixes #512

Description

The Gemini provider was throwing an error when a custom base URL was set, causing the chat interface to crash. The @google/genai SDK actually supports custom base URLs via the httpOptions.baseUrl configuration.

This change removes the error and passes the custom base URL to the SDK, enabling users to use custom Gemini-compatible endpoints (like proxies or self-hosted alternatives).

Checklist before requesting a review

  • I have reviewed the guidelines for contributing to this repository.
  • I have performed a self-review of my code
  • I have performed a code linting check and type check (by running npm run lint:check and npm run type:check)
  • I have run the test suite (by running npm run test)
  • I have tested the functionality manually

Summary by CodeRabbit

  • New Features

    • Support for configuring a custom base URL for AI service requests, improving deployment flexibility.
  • Improvements

    • Base URL handling now trims trailing slashes and accepts custom values; no other runtime behavior changes to message parsing or error semantics.

✏️ Tip: You can customize this high-level summary in your review settings.

Fixes #512

The Gemini provider was throwing an error when a custom base URL was set,
causing the chat interface to crash. The @google/genai SDK actually supports
custom base URLs via the httpOptions.baseUrl configuration.

This change removes the error and passes the custom base URL to the SDK,
enabling users to use custom Gemini-compatible endpoints (like proxies or
self-hosted alternatives).
@coderabbitai
Copy link

coderabbitai bot commented Jan 25, 2026

📝 Walkthrough

Walkthrough

GeminiProvider now accepts and applies a custom base URL via GoogleGenAI's httpOptions.baseUrl, trimming trailing slashes. No other functional behavior (message parsing, token usage, error handling, or public API signatures) was changed.

Changes

Cohort / File(s) Summary
Custom Base URL Support
src/core/llm/gemini.ts
Constructor reads httpOptions.baseUrl and uses it for GoogleGenAI requests; trailing slashes are trimmed. No changes to public signatures or message/token/error logic.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I hopped along the config trail,
Found Gemini a brand-new trail,
A URL trimmed, no errors stay,
Now custom routes can come and play,
Hooray — a cozy routing tale!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: enabling custom base URL support for the Gemini provider, which directly addresses issue #512.
Description check ✅ Passed The description follows the template structure, includes the linked issue reference (#512), explains the problem and solution, and confirms all checklist items are completed.
Linked Issues check ✅ Passed The PR directly addresses issue #512 by removing the custom base URL error and enabling the SDK to accept httpOptions.baseUrl configuration, allowing users to use custom Gemini-compatible endpoints.
Out of Scope Changes check ✅ Passed The changes are narrowly focused on supporting custom base URLs in the Gemini provider; no extraneous modifications to message parsing, token usage, or error handling are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/core/llm/gemini.ts`:
- Around line 204-221: The block handling multipart parts (switch on part.type)
has inconsistent indentation/spacing that breaks Prettier; run the project's
formatter (e.g., execute prettier --write src/core/llm/gemini.ts) or reformat
the switch block (and similar blocks around the
parseImageDataUrl/GeminiProvider.validateImageType usage and return objects) to
match the project's Prettier rules so spacing/braces align with the rest of the
file.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/core/llm/gemini.ts`:
- Around line 53-58: The ternary that sets httpOptions for the GoogleGenAI
client uses provider.baseUrl as-is, which treats whitespace-only strings as
truthy; trim provider.baseUrl first and only pass httpOptions when the trimmed
value is non-empty. Update the initialization of this.client (GoogleGenAI) to
compute a normalizedBaseUrl = provider.baseUrl?.trim(), verify normalizedBaseUrl
&& normalizedBaseUrl.length > 0, and then set httpOptions: { baseUrl:
normalizedBaseUrl.replace(/\/+$/, '') } only in that case; otherwise leave
httpOptions undefined.

Comment on lines +53 to +58
this.client = new GoogleGenAI({
apiKey: provider.apiKey ?? '',
httpOptions: provider.baseUrl
? { baseUrl: provider.baseUrl.replace(/\/+$/, '') }
: undefined,
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

# First, let's look at the actual code in the file
cat -n src/core/llm/gemini.ts | head -70

Repository: glowingjade/obsidian-smart-composer

Length of output: 2442


🌐 Web query:

@google/genai baseUrl httpOptions configuration requirements trailing slash

💡 Result:

In @google/genai, you configure request routing + low-level HTTP behavior via GoogleGenAIOptions.httpOptions, whose type is types.HttpOptions. [1][2]

Where baseUrl goes

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({
  apiKey: process.env.GEMINI_API_KEY,          // Gemini API
  // vertexai: true, project: "...", location: "...", // Vertex AI
  httpOptions: {
    baseUrl: "https://your-proxy.example.com/", // custom endpoint / gateway
    headers: { "X-Foo": "bar" },
    timeout: 120_000, // ms
    // extraBody: {...}
  },
});

HttpOptions supports: apiVersion, baseUrl, headers, timeout (ms), and extraBody. [1]

Trailing-slash requirement (important)

If your baseUrl includes a path prefix (e.g. https://example.com/api/v1), add a trailing slash (.../api/v1/). This is because URL resolution with a “base” is not string concatenation—a base URL without a trailing slash treats the last path segment like a “file” and will drop it when resolving relative paths. [3]

So, prefer:

  • https://example.com/api/v1/ (good)
  • not https://example.com/api/v1 (often causes /api/v1 to be lost) [3]

“Configuration requirements” summary

  • Gemini API: apiKey is required (and “Required on browser runtimes”). [2]
  • Vertex AI (Node): use vertexai: true plus project and location (and optionally googleAuthOptions). [2]
  • httpOptions is optional and is where you set baseUrl/headers/timeout. [1][2]

Sources: [1] types.HttpOptions reference, [2] client.GoogleGenAIOptions reference, [3] MDN on resolving relative URLs with new URL(url, base) and how missing trailing slash changes the effective base path.


Guard against whitespace-only baseUrl values.

If provider.baseUrl is a whitespace-only string like ' ', the current ternary check passes it through because whitespace is truthy. The replace(/\/+$/, '') only removes trailing slashes, leaving the whitespace intact. This results in an invalid URL being passed to the SDK and can cause crashes. Trim and validate that the normalized value is non-empty before setting httpOptions.

💡 Proposed fix
-    this.client = new GoogleGenAI({
-      apiKey: provider.apiKey ?? '',
-      httpOptions: provider.baseUrl
-        ? { baseUrl: provider.baseUrl.replace(/\/+$/, '') }
-        : undefined,
-    })
+    const normalizedBaseUrl =
+      provider.baseUrl?.trim().replace(/\/+$/, '') || undefined
+    this.client = new GoogleGenAI({
+      apiKey: provider.apiKey ?? '',
+      httpOptions: normalizedBaseUrl ? { baseUrl: normalizedBaseUrl } : undefined,
+    })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
this.client = new GoogleGenAI({
apiKey: provider.apiKey ?? '',
httpOptions: provider.baseUrl
? { baseUrl: provider.baseUrl.replace(/\/+$/, '') }
: undefined,
})
const normalizedBaseUrl =
provider.baseUrl?.trim().replace(/\/+$/, '') || undefined
this.client = new GoogleGenAI({
apiKey: provider.apiKey ?? '',
httpOptions: normalizedBaseUrl ? { baseUrl: normalizedBaseUrl } : undefined,
})
🤖 Prompt for AI Agents
In `@src/core/llm/gemini.ts` around lines 53 - 58, The ternary that sets
httpOptions for the GoogleGenAI client uses provider.baseUrl as-is, which treats
whitespace-only strings as truthy; trim provider.baseUrl first and only pass
httpOptions when the trimmed value is non-empty. Update the initialization of
this.client (GoogleGenAI) to compute a normalizedBaseUrl =
provider.baseUrl?.trim(), verify normalizedBaseUrl && normalizedBaseUrl.length >
0, and then set httpOptions: { baseUrl: normalizedBaseUrl.replace(/\/+$/, '') }
only in that case; otherwise leave httpOptions undefined.

@kevin-on kevin-on merged commit 8d1ce6e into main Jan 25, 2026
2 checks passed
@kevin-on kevin-on deleted the fix/issue-512 branch January 25, 2026 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Error: Gemini does not support custom base URL

1 participant