diff --git a/packages/genkit-google-genai/src/genkit_google_genai/_google.py b/packages/genkit-google-genai/src/genkit_google_genai/_google.py index 8af002e7..21305735 100644 --- a/packages/genkit-google-genai/src/genkit_google_genai/_google.py +++ b/packages/genkit-google-genai/src/genkit_google_genai/_google.py @@ -1151,7 +1151,8 @@ def __init__( location: Location of the Google Cloud project. Accepts regions (e.g. 'us-central1'), multi-regions ('us', 'eu'), or 'global'. Falls back to the GOOGLE_CLOUD_LOCATION or GCLOUD_LOCATION - environment variable, then 'us-central1'. + environment variable, then to 'us-central1', or to 'global' in + Express Mode (an api_key without a project). api_key: The API key for authenticating with the Google AI service. If not provided, it defaults to reading from the 'GEMINI_API_KEY' environment variable. @@ -1214,11 +1215,15 @@ def __init__( # Store project and location on the plugin for evaluator registration # and multi-region routing. This avoids reaching into client internals. self._project = project or os.getenv(const.GCLOUD_PROJECT) or os.getenv(const.GOOGLE_CLOUD_PROJECT) - self._location = ( - location - or os.getenv(const.GOOGLE_CLOUD_LOCATION) - or os.getenv(const.GCLOUD_LOCATION) - or const.DEFAULT_REGION + explicit_location = location or os.getenv(const.GOOGLE_CLOUD_LOCATION) or os.getenv(const.GCLOUD_LOCATION) + # The SDK falls back to GOOGLE_API_KEY (then GEMINI_API_KEY) when no api_key is passed, so Express + # Mode has to be detected from that same fallback, in the same order. + effective_api_key = api_key or os.getenv('GOOGLE_API_KEY') or os.getenv('GEMINI_API_KEY') + # 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 effective_api_key and not self._project else const.DEFAULT_REGION ) opts = _plugin_http_options( diff --git a/packages/genkit-google-genai/tests/vertexai_location_test.py b/packages/genkit-google-genai/tests/vertexai_location_test.py index 47e9d9f2..f65389e3 100644 --- a/packages/genkit-google-genai/tests/vertexai_location_test.py +++ b/packages/genkit-google-genai/tests/vertexai_location_test.py @@ -945,3 +945,63 @@ async def test_typed_config_location_stripped(self) -> None: cfg = await model._genkit_to_googleai_cfg(request=request) assert cfg is not None assert not hasattr(cfg, 'location') + + +class TestExpressModeLocation: + """Express Mode (an API key without a project) resolves to the global endpoint.""" + + @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', + 'GOOGLE_API_KEY', + 'GEMINI_API_KEY', + ): + monkeypatch.delenv(var, raising=False) + + 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' + + def test_api_key_from_the_environment_uses_the_global_endpoint(self, monkeypatch: pytest.MonkeyPatch) -> None: + """The SDK falls back to GEMINI_API_KEY, so the plugin has to see Express Mode there too.""" + monkeypatch.setenv('GEMINI_API_KEY', 'env-key') + with patch('genkit_google_genai._google.genai.client.Client'): + plugin = VertexAI() + assert plugin._location == 'global' + + def test_google_api_key_wins_over_gemini_api_key_in_the_environment(self, monkeypatch: pytest.MonkeyPatch) -> None: + """GOOGLE_API_KEY precedes GEMINI_API_KEY in the SDK, so it does here as well.""" + monkeypatch.setenv('GOOGLE_API_KEY', 'google-env-key') + monkeypatch.setenv('GEMINI_API_KEY', 'gemini-env-key') + with patch('genkit_google_genai._google.genai.client.Client'): + plugin = VertexAI() + assert plugin._location == 'global' + + def test_explicit_location_wins_in_express_mode(self) -> None: + with patch('genkit_google_genai._google.genai.client.Client'): + plugin = VertexAI(api_key='test-key', location='europe-west1') + assert plugin._location == 'europe-west1' + + def test_location_environment_variable_wins_in_express_mode(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv('GOOGLE_CLOUD_LOCATION', 'asia-east1') + with patch('genkit_google_genai._google.genai.client.Client'): + plugin = VertexAI(api_key='test-key') + assert plugin._location == 'asia-east1' + + def test_project_without_api_key_keeps_the_regional_default(self) -> None: + """The regional default only applies outside Express Mode.""" + with patch('genkit_google_genai._google.genai.client.Client'): + plugin = VertexAI(project='my-project') + assert plugin._location == 'us-central1' + + def test_api_key_with_a_project_is_not_express_mode(self) -> None: + with patch('genkit_google_genai._google.genai.client.Client'): + plugin = VertexAI(api_key='test-key', project='my-project') + assert plugin._location == 'us-central1'