fix: support custom base URL for Gemini provider#520
Conversation
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).
📝 WalkthroughWalkthroughGeminiProvider now accepts and applies a custom base URL via GoogleGenAI's Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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. Comment |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| this.client = new GoogleGenAI({ | ||
| apiKey: provider.apiKey ?? '', | ||
| httpOptions: provider.baseUrl | ||
| ? { baseUrl: provider.baseUrl.replace(/\/+$/, '') } | ||
| : undefined, | ||
| }) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# First, let's look at the actual code in the file
cat -n src/core/llm/gemini.ts | head -70Repository: 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/v1to be lost) [3]
“Configuration requirements” summary
- Gemini API:
apiKeyis required (and “Required on browser runtimes”). [2] - Vertex AI (Node): use
vertexai: trueplusprojectandlocation(and optionallygoogleAuthOptions). [2] httpOptionsis optional and is where you setbaseUrl/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.
| 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.
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
npm run lint:checkandnpm run type:check)npm run test)Summary by CodeRabbit
New Features
Improvements
✏️ Tip: You can customize this high-level summary in your review settings.