Skip to content

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

Closed
kcncb wants to merge 3 commits into
genkit-ai:mainfrom
kcncb:fix/vertexai-express-mode-global-location
Closed

kcncb wants to merge 3 commits into
genkit-ai:mainfrom
kcncb:fix/vertexai-express-mode-global-location

Conversation

@kcncb

@kcncb kcncb commented Oct 27, 2026

Copy link
Copy Markdown

Fixes genkit-ai/genkit-python#207

VertexAI(api_key=...) in Express Mode (an API key without a project) fell back to GOOGLE_CLOUD_LOCATION/GCLOUD_LOCATION and then to us-central1, so the client was built against https://us-central1-aiplatform.googleapis.com/. Current Gemini models are not served from that region, so the documented Express Mode setup (no project, no location) answered NOT_FOUND.

  • src/genkit_google_genai/_google.py: the location fallback now resolves to global when api_key is set and no project is configured, which is what makes it Express Mode. An explicit location, GOOGLE_CLOUD_LOCATION or GCLOUD_LOCATION still wins, and a project-backed setup keeps the us-central1 default. This matches the Go and JS plugins, which already send Express Mode requests to the global endpoint.
  • tests/vertexai_location_test.py: five cases covering the Express Mode default, an explicit location, the environment variable, the regional default with a project, and api_key + project (not Express Mode).
$ pytest tests/vertexai_location_test.py -q
73 passed

Without the change, test_api_key_without_project_uses_the_global_endpoint fails with us-central1 != global; the other four assertions hold on main too and pin what must not change.

ruff check and ruff format are clean on both files.

@google-cla

google-cla Bot commented Oct 27, 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.

@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 introduces support for Express Mode (using an API key without a project) in the Google GenAI plugin, defaulting its location to the global endpoint instead of the regional default. The feedback highlights a bug where the Express Mode check fails if the API key is provided via the GEMINI_API_KEY environment variable rather than explicitly passed. To address this, the reviewer suggests resolving the API key from the environment variable during initialization, clearing this environment variable in the test setup for proper isolation, and adding a corresponding test case to verify the fallback behavior.

Comment on lines +1116 to 1122
explicit_location = location or os.getenv(const.GOOGLE_CLOUD_LOCATION) or os.getenv(const.GCLOUD_LOCATION)
# Express Mode (an API key without a project) is served from the global endpoint: the regional
# default does not host every current model, so the documented setup without a location answers
# NOT_FOUND there. The Go and JS plugins use the global endpoint for Express Mode as well.
self._location = explicit_location or (
const.GLOBAL_LOCATION if api_key and not self._project else const.DEFAULT_REGION
)

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

If api_key is not explicitly passed to VertexAI(...) but is configured via the GEMINI_API_KEY environment variable (which is the standard fallback documented in the docstring), api_key remains None during __init__. As a result, the Express Mode check api_key and not self._project evaluates to False, and self._location incorrectly falls back to us-central1 instead of global.\n\nTo fix this, we should resolve api_key from the GEMINI_API_KEY environment variable at the start of __init__ (or right before resolving the location), similar to how it is done in GoogleAI.

        api_key = api_key or os.getenv('GEMINI_API_KEY')\n        explicit_location = location or os.getenv(const.GOOGLE_CLOUD_LOCATION) or os.getenv(const.GCLOUD_LOCATION)\n        # Express Mode (an API key without a project) is served from the global endpoint: the regional\n        # default does not host every current model, so the documented setup without a location answers\n        # NOT_FOUND there. The Go and JS plugins use the global endpoint for Express Mode as well.\n        self._location = explicit_location or (\n            const.GLOBAL_LOCATION if api_key and not self._project else const.DEFAULT_REGION\n        )

Comment on lines +975 to +983
@pytest.fixture(autouse=True)
def _clear_location_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
for var in (
'GOOGLE_CLOUD_LOCATION',
'GCLOUD_LOCATION',
'GOOGLE_CLOUD_PROJECT',
'GCLOUD_PROJECT',
):
monkeypatch.delenv(var, raising=False)

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.

medium

To ensure test isolation and prevent developer environment variables from polluting the test execution, we should also clear GEMINI_API_KEY in the _clear_location_env fixture.

    @pytest.fixture(autouse=True)\n    def _clear_location_env(self, monkeypatch: pytest.MonkeyPatch) -> None:\n        for var in (\n            'GOOGLE_CLOUD_LOCATION',\n            'GCLOUD_LOCATION',\n            'GOOGLE_CLOUD_PROJECT',\n            'GCLOUD_PROJECT',\n            'GEMINI_API_KEY',\n        ):\n            monkeypatch.delenv(var, raising=False)

Comment on lines +985 to +991
def test_api_key_without_project_uses_the_global_endpoint(self) -> None:
"""The documented Express Mode setup omits both project and location."""
with patch('genkit_google_genai._google.genai.client.Client'):
plugin = VertexAI(api_key='test-key')
assert plugin._location == 'global'
assert plugin._client_kwargs['location'] == 'global'

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.

medium

Let's add a test case to verify that the GEMINI_API_KEY environment variable fallback works correctly for Express Mode location resolution.

    def test_api_key_without_project_uses_the_global_endpoint(self) -> None:\n        """The documented Express Mode setup omits both project and location."""\n        with patch('genkit_google_genai._google.genai.client.Client'):\n            plugin = VertexAI(api_key='test-key')\n        assert plugin._location == 'global'\n        assert plugin._client_kwargs['location'] == 'global'\n\n    def test_api_key_env_without_project_uses_the_global_endpoint(self, monkeypatch: pytest.MonkeyPatch) -> None:\n        monkeypatch.setenv('GEMINI_API_KEY', 'test-key')\n        with patch('genkit_google_genai._google.genai.client.Client'):\n            plugin = VertexAI()\n        assert plugin._location == 'global'\n        assert plugin._client_kwargs['location'] == 'global'

@kcncb

kcncb commented Oct 27, 2026

Copy link
Copy Markdown
Author

Thanks, both points were right and are now addressed.

1. GEMINI_API_KEY (and GOOGLE_API_KEY) are now part of the Express Mode detection. google.genai._api_client.get_env_api_key() resolves GOOGLE_API_KEY first and falls back to GEMINI_API_KEY, so the plugin checks the same fallback in the same order:

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
)

I deliberately did not assign the resolved key back to api_key: self._client_kwargs['api_key'] stays None so the SDK keeps owning that resolution, and a caller who passes credentials while GEMINI_API_KEY happens to be exported does not start sending both to the client.

2. Test isolation and coverage. The fixture now clears GOOGLE_API_KEY and GEMINI_API_KEY as well, and two cases cover the new path:

  • test_api_key_from_the_environment_uses_the_global_endpoint — GEMINI_API_KEY set, no project/location → global.
  • test_google_api_key_wins_over_gemini_api_key_in_the_environment — both set → still Express Mode, matching the SDK's precedence.
$ pytest tests/vertexai_location_test.py -q
75 passed

Without the source change both new cases fail, so they cover the path this review flagged.

ruff check / ruff format clean.

@MichaelDoyle

Copy link
Copy Markdown
Contributor

Hi @kcncb — heads up that Genkit Python SDK development has moved to https://github.com/genkit-ai/genkit-python! Could you re-open this PR over there (file paths map 1:1 after stripping the leading py/ prefix; see also genkit-ai/genkit-python#50 on recent Vertex AI updates), and link/close this PR once moved? cc @huangjeff5

@kcncb

kcncb commented Nov 18, 2026

Copy link
Copy Markdown
Author

Moved: genkit-ai/genkit-python#142 — same change, paths mapped 1:1 after dropping the py/ prefix, nothing rewritten.

Re-verified on the new tree rather than assumed:

$ pytest packages/genkit-google-genai/tests/vertexai_location_test.py -q
72 passed
# source reverted, tests kept
3 failed, 69 passed   # the three Express Mode cases

ruff check / ruff format --check clean. I also noted in the new PR body that #50 touches the same constructor, so whichever of the two lands second likely needs a rebase — happy to do it.

Closing this one as requested. Thanks for the pointer, and for taking the time to say where the work should go rather than just closing it.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] google-genai: VertexAI Express Mode without location uses us-central1 instead of the global endpoint

2 participants