Skip to content

fix(core): honor GOOGLE_CLOUD_LOCATION for Vertex AI with API key#28142

Open
abhay-codes07 wants to merge 2 commits into
google-gemini:mainfrom
abhay-codes07:fix/vertex-ai-regional-endpoint-27984
Open

fix(core): honor GOOGLE_CLOUD_LOCATION for Vertex AI with API key#28142
abhay-codes07 wants to merge 2 commits into
google-gemini:mainfrom
abhay-codes07:fix/vertex-ai-regional-endpoint-27984

Conversation

@abhay-codes07

Copy link
Copy Markdown

Summary

When authenticating to Vertex AI with an API key (GOOGLE_API_KEY), the configured GOOGLE_CLOUD_LOCATION is silently ignored and requests are routed to the global endpoint (https://aiplatform.googleapis.com/) instead of the regional one.

The root cause is in @google/genai: as soon as an API key is present it clears project/location and short-circuits to the global endpoint. Meanwhile /about still displays the configured region and /model lists region-appropriate models, so the displayed configuration and the actual routing disagree with no warning. For users who pick Vertex AI specifically for data residency (e.g. GDPR), this undermines exactly the expectation that made them set a location.

Fix

In createContentGenerator, when a Vertex AI API key is used together with a non-global GOOGLE_CLOUD_LOCATION, derive the regional endpoint ourselves and pass it explicitly via httpOptions.baseUrl (which overrides the library's global default through patchHttpOptions):

https://<location>-aiplatform.googleapis.com/

This matches the exact URL format the library already uses for the project/location flow.

Precedence is preserved:

  • An explicit base URL (config baseUrl) and GOOGLE_VERTEX_BASE_URL still take priority and are left untouched.
  • location === 'global' and an unset location fall back to the existing global behavior (no regression).
  • The API-key-less project/location flow is unchanged (the library already honors the region there).

Testing

Added unit tests in contentGenerator.test.ts covering:

  • regional endpoint derived from GOOGLE_CLOUD_LOCATION (both httpOptions.baseUrl and apiEndpoint)
  • whitespace trimming around the location value
  • global location → global endpoint (no derived baseUrl)
  • no location configured → no derived baseUrl
  • GOOGLE_VERTEX_BASE_URL taking precedence over the derived endpoint
  • no derivation when no API key is used

All 59 tests in the file pass; eslint and tsc --noEmit are clean for the affected package.

Fixes #27984

When authenticating to Vertex AI with an API key, @google/genai forces
the global endpoint and silently ignores GOOGLE_CLOUD_LOCATION, routing
requests to aiplatform.googleapis.com regardless of the configured
region. This breaks data-residency expectations for users who select a
specific location, while /about still displays the configured region.

Derive the regional endpoint
(https://<location>-aiplatform.googleapis.com/) when a Vertex AI API key
is used together with a non-global GOOGLE_CLOUD_LOCATION. Explicit
overrides (config base URL and GOOGLE_VERTEX_BASE_URL) continue to take
precedence, and the API-key-less project/location flow is unchanged.

Fixes google-gemini#27984
@abhay-codes07 abhay-codes07 requested a review from a team as a code owner June 25, 2026 14:21
@google-cla

google-cla Bot commented Jun 25, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added the size/m A medium sized PR label Jun 25, 2026
@github-actions

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 122
  • Additions: +122
  • Deletions: -0
  • Files changed: 2

@gemini-code-assist

Copy link
Copy Markdown
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 addresses an issue where Vertex AI requests were defaulting to the global endpoint when authenticated via an API key, effectively ignoring the user-configured GOOGLE_CLOUD_LOCATION. By explicitly deriving the regional endpoint and passing it to the generator, the change ensures that data residency requirements are met while maintaining backward compatibility for existing configurations.

Highlights

  • Vertex AI Regional Routing: Implemented logic to manually derive and set the regional endpoint URL when using an API key, ensuring GOOGLE_CLOUD_LOCATION is respected for data residency.
  • Precedence Preservation: Ensured that explicit base URL configurations and existing global default behaviors remain unchanged and take priority over the new regional derivation.
  • Comprehensive Testing: Added unit tests to verify regional endpoint derivation, whitespace handling, and correct fallback behavior for global or unset locations.
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 the 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 counterproductive. 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.

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.

@gemini-cli gemini-cli Bot added the area/security Issues related to security label Jun 25, 2026

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

Copy link
Copy Markdown
Contributor

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 deriving regional endpoints from the GOOGLE_CLOUD_LOCATION environment variable when using Vertex AI with an API key, along with comprehensive unit tests. The reviewer suggests validating the GOOGLE_CLOUD_LOCATION value against a regular expression to prevent potential URL injection or malformed URL issues.

Comment on lines +361 to +364
const location = process.env['GOOGLE_CLOUD_LOCATION']?.trim();
if (location && location !== 'global') {
baseUrl = `https://${location}-aiplatform.googleapis.com/`;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Constructing the baseUrl by directly interpolating the GOOGLE_CLOUD_LOCATION environment variable without validation can lead to malformed URLs or potential URL injection vulnerabilities if the environment variable contains unexpected characters (e.g., @, :, /, ?, #).

Since Google Cloud region names strictly consist of alphanumeric characters and hyphens (e.g., us-central1, europe-west3), we should validate the location string against a regular expression (like /^[a-z0-9-]+$/i) before using it to construct the URL. If the location is invalid, we should throw an error to fail fast and prevent security or routing issues.

        const location = process.env['GOOGLE_CLOUD_LOCATION']?.trim();
        if (location && location !== 'global') {
          if (!/^[a-z0-9-]+$/i.test(location)) {
            throw new Error('Invalid GOOGLE_CLOUD_LOCATION: ' + location);
          }
          baseUrl = 'https://' + location + '-aiplatform.googleapis.com/';
        }

@abhay-codes07

Copy link
Copy Markdown
Author

@googlebot I signed it!

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

Labels

area/security Issues related to security size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vertex AI with API key ignores GOOGLE_CLOUD_LOCATION and uses the global endpoint (data residency)

1 participant