Skip to content

fix(google-genai): use the global endpoint for Vertex AI Express Mode - #142

Open
kcncb wants to merge 1 commit into
genkit-ai:mainfrom
kcncb:fix/express-mode-global-endpoint
Open

kcncb wants to merge 1 commit into
genkit-ai:mainfrom
kcncb:fix/express-mode-global-endpoint

Conversation

@kcncb

@kcncb kcncb commented Nov 18, 2026

Copy link
Copy Markdown

Re-opens genkit-ai/genkit#6908 here, as @MichaelDoyle asked. The diff is the same change with the py/ prefix dropped; nothing was rewritten for the move.

Problem

Express Mode is a Google AI API key with no project. The plugin resolved _location to us-central1 for it — the regional default — and that region does not host every current model, so the documented setup (an API key, no location) answers NOT_FOUND. The Go and JS plugins serve Express Mode from the global endpoint.

Change

_location now falls back to global when an API key is present and no project is, and keeps the regional default otherwise:

explicit_location = location or os.getenv(const.GOOGLE_CLOUD_LOCATION) or os.getenv(const.GCLOUD_LOCATION)
effective_api_key = api_key or os.getenv('GOOGLE_API_KEY') or os.getenv('GEMINI_API_KEY')
self._location = explicit_location or (
    const.GLOBAL_LOCATION if effective_api_key and not self._project else const.DEFAULT_REGION
)

Two details that came out of review on the original PR:

  • The key is read from the same fallback the SDK uses, in the same order: GOOGLE_API_KEY first, then GEMINI_API_KEY. Otherwise an exported GEMINI_API_KEY would be Express Mode for the SDK but not for the plugin.
  • The resolved key is not written back to api_key. client_kwargs keeps None so the SDK keeps owning that resolution; a caller who passes credentials while an API key happens to be exported does not start sending both.

An explicit location argument, GOOGLE_CLOUD_LOCATION or GCLOUD_LOCATION still wins over the Express Mode default, and a project is still not required.

Tests

Seven cases added to tests/vertexai_location_test.py, next to the existing multi-region coverage:

case expected
api_key alone (the documented Express Mode setup) global
GEMINI_API_KEY from the environment global
GOOGLE_API_KEY and GEMINI_API_KEY both set global (and GOOGLE_API_KEY is the one read)
explicit location in Express Mode that location
GOOGLE_CLOUD_LOCATION in Express Mode that location
project, no key us-central1 (regional default unchanged)
key and project us-central1 (not Express Mode)
$ pytest packages/genkit-google-genai/tests/vertexai_location_test.py -q -o addopts=""
72 passed

# with the source change reverted, tests kept
E   AssertionError: assert 'us-central1' == 'global'
3 failed, 69 passed      # the three Express Mode cases; the four guards still pass

The four that pass in both states are the guards: they pin that an explicit location and a project-bearing setup keep the old behaviour.

ruff check and ruff format --check are clean on both files.

Relationship to #50

#50 makes VertexAI() fail fast when there is neither a project nor a key, which is the case this PR deliberately does not treat as Express Mode — the two do not overlap, but they touch the same constructor, so whichever lands second probably needs a rebase. Happy to do that rebase if #50 goes in first.

Express Mode is a Google AI API key with no project. The plugin resolved the
location to `us-central1` for it, the regional default, which does not host
every current model: the documented setup (an API key, no location) answers
NOT_FOUND there. The Go and JS plugins serve Express Mode from the global
endpoint.

`_location` now falls back to `global` when an API key is present and no
project is, keeping the regional default for project/ADC setups. An explicit
`location` argument or `GOOGLE_CLOUD_LOCATION` / `GCLOUD_LOCATION` still wins.

Express Mode is detected from the same fallback the google-genai SDK uses:
`GOOGLE_API_KEY` first, then `GEMINI_API_KEY`. The resolved key is not written
back to `api_key`, so `client_kwargs` keeps `None` and the SDK keeps owning
that resolution — a caller passing both `credentials` and an exported key does
not start sending both.
@kcncb

kcncb commented Nov 18, 2026 •

Copy link
Copy Markdown
Author

Heads-up on CI: Python Checks, Commit Lint and Google GitHub Admin: Actions Workflow Security Scan are all sitting in action_required for this fork PR, so nothing has actually executed here yet — the only green checks are check-changes and cla/google. Approving the workflow run would put the real CI result on this PR.

Local evidence is in the description: pytest packages/genkit-google-genai/tests/vertexai_location_test.py -q gives 72 passed, and with the source change reverted (tests kept) the three Express Mode cases fail as expected. ruff check / ruff format --check are clean. Nothing else is pending on my side; also fine to rebase on top of #50 if that one lands first.

@huangjeff5 huangjeff5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good catch on Express Mode needing global, but there is an ordering issue with ADC / credentials project resolution in VertexAI.__init__:

self._location is computed before self._project is resolved from credentials.project_id or google_auth_default(). If someone calls VertexAI() (or VertexAI(credentials=creds)) on a machine that has ADC credentials and also happens to have GEMINI_API_KEY or GOOGLE_API_KEY set in their environment:

  1. self._project is initially None, so effective_api_key and not self._project evaluates to True and sets self._location = 'global'.
  2. A few lines later, if not self._project and (api_key is None or multi_region): resolves self._project via credentials.project_id / google_auth_default().
  3. genai.Client(vertexai=True, api_key=None, project=<adc_project>, location='global') then authenticates using ADC + project (not Express Mode), silently changing the default location from us-central1 to global just because GEMINI_API_KEY is in the env.

Resolving self._project from credentials / google_auth_default() before falling back self._location to GLOBAL_LOCATION (or checking Express Mode after project resolution) would avoid that mismatch. Could you also add a test covering VertexAI() when both ADC project and GEMINI_API_KEY are present?

This branch has not been deployed

No deployments
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.

2 participants