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 eb66ca48e..703f05cfd 100644 --- a/packages/genkit-google-genai/src/genkit_google_genai/_google.py +++ b/packages/genkit-google-genai/src/genkit_google_genai/_google.py @@ -1144,7 +1144,7 @@ def __init__( location or os.getenv(const.GOOGLE_CLOUD_LOCATION) or os.getenv(const.GCLOUD_LOCATION) - or const.DEFAULT_REGION + or (None if (api_key and not self._project) else const.DEFAULT_REGION) ) opts = _plugin_http_options( @@ -1157,11 +1157,11 @@ def __init__( self._http_client_factory = _loop_local_http_client(http_client_factory) self._base_url_pinned = bool(opts.base_url) self._legacy_response_schema = legacy_response_schema - multi_region = const.is_multi_regional_location(self._location) - if multi_region and not self._base_url_pinned: + if const.is_multi_regional_location(self._location) and not self._base_url_pinned: # Multi-regions ('us', 'eu') are served from dedicated endpoints # that the google-genai SDK does not derive itself. opts.base_url = const.multi_regional_base_url(self._location) + multi_region = const.is_multi_regional_location(self._location) # Resolve the project here rather than leaving it to the SDK: with any # base_url set the SDK skips its own ADC lookup, evaluator registration @@ -1236,7 +1236,7 @@ async def init(self) -> list[Action | BackgroundAction[Any, Any]]: create_vertex_evaluators( list(VertexAIEvaluationMetricType), project=self._project, - location=self._location, + location=self._location or const.DEFAULT_REGION, ) ) @@ -1322,7 +1322,7 @@ def _resolve_evaluator(self, name: str) -> Action | None: [action] = create_vertex_evaluators( [metric_type], project=self._project, - location=self._location, + location=self._location or const.DEFAULT_REGION, ) return action @@ -1449,7 +1449,7 @@ async def list_actions(self) -> list[Action | BackgroundAction[Any, Any]]: create_vertex_evaluators( list(VertexAIEvaluationMetricType), project=self._project, - location=self._location, + location=self._location or const.DEFAULT_REGION, ) ) diff --git a/packages/genkit-google-genai/tests/vertexai_location_test.py b/packages/genkit-google-genai/tests/vertexai_location_test.py index 7e48ad786..eb7fb7c08 100644 --- a/packages/genkit-google-genai/tests/vertexai_location_test.py +++ b/packages/genkit-google-genai/tests/vertexai_location_test.py @@ -204,6 +204,14 @@ def test_api_key_skips_adc_probe(self) -> None: mock_adc.assert_not_called() assert plugin._project is None + def test_api_key_omits_default_location(self) -> None: + """Express mode (api_key) without location leaves location unset rather than defaulting to us-central1.""" + with patch.dict(os.environ, {}, clear=True): + with patch('genkit_google_genai._google.genai.client.Client'): + plugin = VertexAI(api_key='k') + assert plugin._location is None + assert plugin._client_kwargs['location'] is None + def test_explicit_project_skips_adc_probe(self) -> None: """An explicit project short-circuits the ADC probe.""" with patch.dict(os.environ, {}, clear=True):