Repository navigation
Conversation
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.
|
Heads-up on CI: Local evidence is in the description: |
huangjeff5
left a comment
There was a problem hiding this comment.
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:
self._projectis initiallyNone, soeffective_api_key and not self._projectevaluates toTrueand setsself._location = 'global'.- A few lines later,
if not self._project and (api_key is None or multi_region):resolvesself._projectviacredentials.project_id/google_auth_default(). 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 fromus-central1toglobaljust becauseGEMINI_API_KEYis 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?
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
_locationtous-central1for it — the regional default — and that region does not host every current model, so the documented setup (an API key, no location) answersNOT_FOUND. The Go and JS plugins serve Express Mode from the global endpoint.Change
_locationnow falls back toglobalwhen an API key is present and no project is, and keeps the regional default otherwise:Two details that came out of review on the original PR:
GOOGLE_API_KEYfirst, thenGEMINI_API_KEY. Otherwise an exportedGEMINI_API_KEYwould be Express Mode for the SDK but not for the plugin.api_key.client_kwargskeepsNoneso the SDK keeps owning that resolution; a caller who passescredentialswhile an API key happens to be exported does not start sending both.An explicit
locationargument,GOOGLE_CLOUD_LOCATIONorGCLOUD_LOCATIONstill 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:api_keyalone (the documented Express Mode setup)globalGEMINI_API_KEYfrom the environmentglobalGOOGLE_API_KEYandGEMINI_API_KEYboth setglobal(andGOOGLE_API_KEYis the one read)locationin Express ModeGOOGLE_CLOUD_LOCATIONin Express Modeus-central1(regional default unchanged)us-central1(not Express Mode)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 checkandruff format --checkare 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.