fix(vertex): resolve project_id from credentials before building request URL#1742
Open
sean-kim05 wants to merge 1 commit into
Open
fix(vertex): resolve project_id from credentials before building request URL#1742sean-kim05 wants to merge 1 commit into
sean-kim05 wants to merge 1 commit into
Conversation
…est URL The Vertex client builds its request URL in `_prepare_options`, which requires a project_id. When one isn't passed explicitly or set via `ANTHROPIC_VERTEX_PROJECT_ID`, it is meant to be resolved from Application Default Credentials — but that resolution only happens in `_ensure_access_token`, which the base client calls from `_prepare_request`, *after* `_prepare_options`. So a client relying on ADC for its project (the common case on GCE/GKE/Cloud Run) failed on every request with "No project_id was given and it could not be resolved from credentials" without ever consulting the credentials. Resolve credentials in `_prepare_options` when project_id is unset, so the ADC project is available before the URL is built. `_ensure_access_token` caches, so the subsequent `_prepare_request` call reuses the loaded credentials without a second network round-trip. Fixed on both the sync and async clients.
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.
Summary
AnthropicVertex/AsyncAnthropicVertexfail on every request withwhenever the project is meant to be resolved from Application Default Credentials rather than passed explicitly or set via
ANTHROPIC_VERTEX_PROJECT_ID.Root cause
The request URL is built in
_prepare_options, which needs aproject_id:Resolution from ADC only happens inside
_ensure_access_token(load_auth()returns(credentials, project_id)fromgoogle.auth.default()), which the base client calls from_prepare_request. But the base client runs them in this order:So the ADC project is never consulted before the URL is built — the error message ("could not be resolved from credentials") is emitted without the credentials ever being loaded. This breaks the common deployment pattern of running on GCE/GKE/Cloud Run with ADC and no explicit project.
Fix
Resolve credentials in
_prepare_optionswhenproject_idis unset, so the ADC project is available before the URL is built._ensure_access_tokencaches the loaded credentials, so the subsequent_prepare_requestcall reuses them without a second network round-trip. Applied symmetrically to the sync and async clients.When
project_idgenuinely can't be resolved (e.g. anaccess_tokenwas supplied without a project, or ADC has no default project), the sameRuntimeErroris still raised — now after credentials were actually consulted.Testing
Added
test_project_id_resolved_from_credentials(sync + async) intests/lib/test_vertex.py: with no explicitproject_idand no env var, a mockedload_authsupplies the project, and the request is asserted to hit/projects/adc-project/.../rawPredict.Verified RED (
RuntimeErrorbefore the fix) → GREEN. Fulltests/lib/test_vertex.pypasses (26 tests);./scripts/lintclean (ruff + pyright + mypy).