fix: reuse google auth per provider instance#14102
Open
heiwen wants to merge 1 commit intovercel:mainfrom
Open
fix: reuse google auth per provider instance#14102heiwen wants to merge 1 commit intovercel:mainfrom
heiwen wants to merge 1 commit intovercel:mainfrom
Conversation
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.
Background
@ai-sdk/google-vertexused a module-levelGoogleAuthcache in the shared Node auth helper. That cache was invalidated by object reference equality, which caused unnecessaryGoogleAuthrecreation in two cases:googleAuthOptionswould recreate auth when requests alternated between them.googleAuthOptionswas omitted,generateAuthToken(options || {})created a new empty object on every call, so auth could be recreated on every request even for a single provider.Because
GoogleAuthcaches the resolved auth client on the instance, recreating it discards warm auth state and can add avoidable auth overhead.Summary
This PR removes the module-level
GoogleAuthsingleton from@ai-sdk/google-vertex's shared Node auth helper and replaces it with a per-provider auth token generator.Changes included:
generateAuthToken(options)helper withcreateAuthTokenGenerator(options), which creates oneGoogleAuthinstance and returns a reusable token generator closure.@ai-sdk/google-vertex@ai-sdk/google-vertex/anthropic@ai-sdk/google-vertex/maasgoogleAuthOptions@ai-sdk/google-vertexManual Verification
I manually verified the change by inspecting the Node auth flow and confirming the auth generator is now created once during provider construction rather than on each request path.
Concretely:
createAuthTokenGenerator(options.googleAuthOptions)once at provider initializationundefined -> {}invalidation path is goneI also verified behavior through automated coverage:
pnpm --filter @ai-sdk/google-vertex test:nodepnpm type-check:fullChecklist
pnpm changesetin the project root)Future Work
A follow-up PR could evaluate whether the Edge auth path should also cache tokens or auth state more aggressively, since it uses a different implementation and is not addressed here.
Related Issues
Fixes #14101