Repository navigation
Conversation
|
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. |
There was a problem hiding this comment.
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.
| 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 | ||
| ) |
There was a problem hiding this comment.
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 )| @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) |
There was a problem hiding this comment.
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)| 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' | ||
|
|
There was a problem hiding this comment.
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'|
Thanks, both points were right and are now addressed. 1. 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 2. Test isolation and coverage. The fixture now clears
Without the source change both new cases fail, so they cover the path this review flagged.
|
|
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 |
|
Moved: genkit-ai/genkit-python#142 — same change, paths mapped 1:1 after dropping the 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
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. |
Fixes genkit-ai/genkit-python#207
VertexAI(api_key=...)in Express Mode (an API key without a project) fell back toGOOGLE_CLOUD_LOCATION/GCLOUD_LOCATIONand then tous-central1, so the client was built againsthttps://us-central1-aiplatform.googleapis.com/. Current Gemini models are not served from that region, so the documented Express Mode setup (noproject, nolocation) answeredNOT_FOUND.src/genkit_google_genai/_google.py: the location fallback now resolves toglobalwhenapi_keyis set and no project is configured, which is what makes it Express Mode. An explicitlocation,GOOGLE_CLOUD_LOCATIONorGCLOUD_LOCATIONstill wins, and a project-backed setup keeps theus-central1default. 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, andapi_key+project(not Express Mode).Without the change,
test_api_key_without_project_uses_the_global_endpointfails withus-central1 != global; the other four assertions hold onmaintoo and pin what must not change.ruff checkandruff formatare clean on both files.