Skip to content

feat(core): support GOOGLE_GEMINI_BASE_URL for custom API endpoints#20681

Open
AvichalDwivedi2205 wants to merge 2 commits intogoogle-gemini:mainfrom
AvichalDwivedi2205:feat/google-gemini-base-url-support
Open

feat(core): support GOOGLE_GEMINI_BASE_URL for custom API endpoints#20681
AvichalDwivedi2205 wants to merge 2 commits intogoogle-gemini:mainfrom
AvichalDwivedi2205:feat/google-gemini-base-url-support

Conversation

@AvichalDwivedi2205
Copy link

Adds support for GOOGLE_GEMINI_BASE_URL environment variable to override the default Gemini API endpoint. This enables local-first workflows with LLM proxies like Ollama and LiteLLM.

  • GOOGLE_GEMINI_BASE_URL takes precedence (matches issue CLI ignores GOOGLE_GEMINI_BASE_URL and forces Cloud Auth/Endpoints #15430 request)
  • GEMINI_API_BASE_URL supported as alias for compatibility
  • URL validation with clear warning on invalid values
  • Endpoint telemetry updated to reflect custom base URL
  • Vitest tests covering all env var combinations and edge cases
  • Configuration docs updated

Closes #15430

Summary

Details

Related Issues

How to Validate

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@AvichalDwivedi2205 AvichalDwivedi2205 requested review from a team as code owners February 28, 2026 13:32
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant enhancement by allowing users to configure custom API endpoints for the Gemini CLI via environment variables. This change is crucial for developers who wish to integrate with local LLM proxies or custom API-compatible services, providing greater flexibility and enabling local-first development workflows. The implementation includes robust URL validation and ensures that telemetry accurately reflects the custom endpoints being used.

Highlights

  • Custom API Endpoint Support: Added support for the GOOGLE_GEMINI_BASE_URL environment variable, allowing users to override the default Gemini API endpoint. This enables integration with local LLM proxies like Ollama and LiteLLM.
  • Alias for Compatibility: Included GEMINI_API_BASE_URL as an alias for GOOGLE_GEMINI_BASE_URL to maintain compatibility, with GOOGLE_GEMINI_BASE_URL taking precedence when both are set.
  • URL Validation and Fallback: Implemented URL validation for custom base URLs. If an invalid URL is provided, the CLI logs a warning and falls back to the default Gemini API endpoint.
  • Telemetry Updates: Updated endpoint telemetry to accurately reflect the use of custom base URLs, ensuring correct reporting of API request destinations.
  • Comprehensive Testing: Added extensive Vitest tests covering various environment variable combinations, precedence rules, and edge cases for URL validation.
  • Documentation Update: Updated the configuration reference documentation to include details about the new GOOGLE_GEMINI_BASE_URL and GEMINI_API_BASE_URL environment variables.
Changelog
  • docs/reference/configuration.md
    • Documented the new GOOGLE_GEMINI_BASE_URL environment variable, explaining its purpose, precedence, validation behavior, and examples for local LLM proxies.
    • Added documentation for GEMINI_API_BASE_URL as a compatibility alias.
  • packages/core/src/core/contentGenerator.test.ts
    • Imported resolveCustomBaseUrl and debugLogger for testing purposes.
    • Added a new test suite for resolveCustomBaseUrl to verify its behavior with various environment variable settings, precedence, and URL validity.
    • Included a new test suite for createContentGenerator to confirm that custom base URLs are correctly passed to GoogleGenAI httpOptions.
  • packages/core/src/core/contentGenerator.ts
    • Imported debugLogger for logging warnings related to invalid URLs.
    • Implemented the resolveCustomBaseUrl function to determine the custom base URL from environment variables, including validation and precedence logic.
    • Modified createContentGenerator to retrieve and apply the resolved custom base URL to the httpOptions of the GoogleGenAI client.
  • packages/core/src/core/loggingContentGenerator.test.ts
    • Added a new test suite for LoggingContentGenerator._getEndpointUrl to ensure that the correct server address and port are reported in telemetry when custom base URLs are configured.
    • Included tests for default endpoint behavior, custom URLs with and without explicit ports, and the GEMINI_API_BASE_URL alias.
  • packages/core/src/core/loggingContentGenerator.ts
    • Imported resolveCustomBaseUrl to utilize the new custom base URL resolution logic.
    • Updated the _getEndpointUrl method to incorporate the custom base URL, parsing its components (hostname, port, protocol) for telemetry reporting.
Activity
  • Updated relevant documentation and README.
  • Added/updated tests.
  • Validated on MacOS using npm run.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for custom API endpoints via the GOOGLE_GEMINI_BASE_URL and GEMINI_API_BASE_URL environment variables. The changes include URL validation, updates to telemetry to log the custom endpoint, comprehensive tests, and documentation updates. The implementation is solid, but I've identified one area for improvement in loggingContentGenerator.ts where redundant URL validation logic can be removed to improve maintainability.

@gemini-cli gemini-cli bot added priority/p3 Backlog - a good idea but not currently a priority. area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Feb 28, 2026
Adds support for GOOGLE_GEMINI_BASE_URL environment variable to override
the default Gemini API endpoint. This enables local-first workflows with
LLM proxies like Ollama and LiteLLM.

- GOOGLE_GEMINI_BASE_URL takes precedence (matches issue google-gemini#15430 request)
- GEMINI_API_BASE_URL supported as alias for compatibility
- URL validation with clear warning on invalid values
- Endpoint telemetry updated to reflect custom base URL
- Vitest tests covering all env var combinations and edge cases
- Configuration docs updated

Closes google-gemini#15430
resolveCustomBaseUrl() already validates the URL and returns undefined
for invalid input, so new URL() is guaranteed not to throw when
customBaseUrl is truthy.
Comment on lines 232 to +249
if (
config.authType === AuthType.USE_GEMINI ||
config.authType === AuthType.USE_VERTEX_AI
) {
let headers: Record<string, string> = { ...baseHeaders };
if (gcConfig?.getUsageStatisticsEnabled()) {
const installationManager = new InstallationManager();
const installationId = installationManager.getInstallationId();
headers = {
...headers,
'x-gemini-api-privileged-user-id': `${installationId}`,
};
}
const httpOptions = { headers };
const customBaseUrl = resolveCustomBaseUrl();
const httpOptions = {
headers,
...(customBaseUrl && { baseUrl: customBaseUrl }),
};
Copy link
Contributor

Choose a reason for hiding this comment

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

In line 232–233 resolveCustomBaseUrl() is called inside the USE_GEMINI || USE_VERTEX_AI branch, so the custom baseUrl is applied to httpOptions for both auth types:

if (config.authType === AuthType.USE_GEMINI ||
    config.authType === AuthType.USE_VERTEX_AI) {
  // ...
  const customBaseUrl = resolveCustomBaseUrl();
  const httpOptions = {
    headers,
    ...(customBaseUrl && { baseUrl: customBaseUrl }), // applied for both
  };

However, in loggingContentGenerator.ts _getEndpointUrl() (L214–235), the Vertex AI check (Case 2, L215) returns before the custom URL check (Case 3, L226) is ever reached:

// Case 2 (L215) — returns early, never reaches Case 3
if (genConfig?.vertexai) {
  return { address: `${location}-aiplatform.googleapis.com`, port: 443 };
}
// Case 3 (L226) — unreachable when vertexai is true
const customBaseUrl = resolveCustomBaseUrl();

So if a user sets GOOGLE_GEMINI_BASE_URL=http://my-proxy:4000 with Vertex AI
auth, the actual request goes to http://my-proxy:4000, but the telemetry
event records us-central1-aiplatform.googleapis.com:443 as the server
address. Since telemetry should reflect where the request actually went, it
should record { address: 'my-proxy', port: 4000 } instead.

One possible solution will, is to Move the custom URL check in _getEndpointUrl() before the Vertex AI check, so telemetry accurately reflects the actual endpoint:

// packages/core/src/core/loggingContentGenerator.ts
// _getEndpointUrl() — replace L214–L235 with custom URL check first:

// Case 2 (moved up): Custom base URL
const customBaseUrl = resolveCustomBaseUrl();
if (customBaseUrl) {
  const parsed = new URL(customBaseUrl);
  const port = parsed.port
    ? parseInt(parsed.port, 10)
    : parsed.protocol === 'https:' ? 443 : 80;
  return { address: parsed.hostname, port };
}

// Case 3 (moved down): Vertex AI default
if (genConfig?.vertexai) { ... }

Let me know what you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! priority/p3 Backlog - a good idea but not currently a priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI ignores GOOGLE_GEMINI_BASE_URL and forces Cloud Auth/Endpoints

2 participants