Fix: fail fast when a Vertex AI Express Mode API key cannot be used - #563
Open
AmaadMartin wants to merge 8 commits into
Open
Fix: fail fast when a Vertex AI Express Mode API key cannot be used#563AmaadMartin wants to merge 8 commits into
AmaadMartin wants to merge 8 commits into
Conversation
added 5 commits
July 28, 2026 23:04
VertexAiSessionService and VertexAiMemoryBankService resolved an Express Mode API key in their constructors and then built the Agent Engine client without it, producing a service that authenticated with ADC against projects/undefined/locations/undefined. The @google-cloud/vertexai Client constructor only accepts project, location and apiEndpoint, so the key can never be sent. Throw a shared, actionable error instead of silently dropping the credential.
…vices Mock the Agent Engine Client in both suites so the default client path can be asserted without network or credentials, and make the pre-existing no-project/location test hermetic by stubbing the express-mode env vars.
Address review feedback: shorten the error text and its doc comment, stop advertising expressModeApiKey in the fallback message now that it can never work, and table the three express-mode throw cases.
…sage Second review pass: GOOGLE_API_KEY is never read when GOOGLE_GENAI_USE_VERTEXAI is unset, the agentEngineId guard already runs first structurally, and enumerating the upstream constructor options in the error would go stale.
added 3 commits
July 31, 2026 15:13
Temporarily takes upstream/main's copy of this file so the merge of upstream/main lands conflict-free; the express mode tests are restored in the following commit.
… merge Re-applies the express mode suite on top of upstream's version of this file, which gained the ttl/expireTime and ApiError 404 tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
N/A — no existing public issue.
2. Or, if no issue exists, describe the change:
Problem:
VertexAiSessionServiceandVertexAiMemoryBankServiceresolve a Vertex AI Express Mode API key in their constructors and then throw it away. Both callgetExpressModeApiKey(...)and store the result, and the session service even accepts that key in place ofprojectId+location, but neither ever readsthis.expressModeApiKeyagain: they buildnew Client({project, location})and the key is never sent.The key cannot be sent, because the Agent Engine client has nowhere to put it.
@google-cloud/vertexaiis pinned at^1.12.0(core/package.json, locked to1.12.0), andnode_modules/@google-cloud/vertexai/build/src/genai/client.d.tsdeclares:There is no
apiKeyoption, and1.12.0is the latest published version, so a dependency bump does not unblock it either. (This is whereadk-jsdiverges fromadk-python, whosevertexai.Clientdoes acceptapi_key=.)What a user actually saw before this change, with
GOOGLE_GENAI_USE_VERTEXAI=trueandGOOGLE_API_KEYset:projectId/location— construction failed inside the SDK withAuthentication is not set up. Please provide either a project and location, or an API key, or a custom base URL., i.e. it asked for an API key the user had already supplied and ADK had already accepted.projectIdbut nolocation— construction succeeded silently. The underlyingApiClientdefaultslocationto'global'and authenticates with Application Default Credentials, so the express key was dropped with no error at all.Solution:
Fail fast with an actionable message instead of dropping the credential. One shared constant,
EXPRESS_MODE_UNSUPPORTED_MESSAGE, lives in the internalcore/src/utils/vertex_ai_utils.ts(not added to any public barrel) and is thrown by both constructors when an express-mode key is in effect and the service has to build its own client and there is no usableprojectId+locationfallback. The message names the cause and both supported ways forward:Testing Plan
Please describe the tests that you ran to verify your changes. This is required for all PRs that are not small documentation or typo fixes.
Unit Tests:
Added an
express modesuite to both service test files, covering: theexpressModeApiKeyoption, an API key resolved fromGOOGLE_API_KEY, a key alongside a project but no location, theprojectId+locationADC path still building the client with exactly{project, location}, and injectedsessions/clientnever building a client at all. Both suites mock the Agent EngineClient(each service imports it from a different specifier) and stub the environment withvi.stubEnvso the cases are hermetic. Every new line and branch is covered.Manual End-to-End (E2E) Tests:
Please provide instructions on how to manually test your changes, including any necessary setup or configuration.
No automated e2e test is possible: reaching this path requires an express-mode API key against a live Agent Engine, and the point of the change is that such a call cannot be authenticated by the current SDK. The construction behavior was verified manually against the real built package (no mocks):
Checklist
Additional context
Notes for reviewers:
projectIdandlocationare both present the behavior is unchanged —new Client({project, location})with ADC — even ifGOOGLE_API_KEYhappens to be set. This is a deliberate divergence fromadk-python, which would prefer the env-derived key; since JS cannot use the key at all, falling back to the working ADC path beats throwing.sessions/clientstill bypass every guard, so the existing test seams are untouched.'Either (Project ID and Location) or an expressModeApiKey is required.'to'Project ID and Location are required.'. Keeping the old text would have sent users straight into the error this PR adds, since anexpressModeApiKeyis never a working alternative for these services.expressModeApiKeyis intentionally kept on both options interfaces, andgetExpressModeApiKey's validation is unchanged — the option becomes functional the day the SDK exposes an API-key option.