Skip to content

fix: reuse google auth per provider instance#14102

Merged
aayush-kapoor merged 1 commit intovercel:mainfrom
heiwen:reuse-google-auth-per-provider-instance
May 1, 2026
Merged

fix: reuse google auth per provider instance#14102
aayush-kapoor merged 1 commit intovercel:mainfrom
heiwen:reuse-google-auth-per-provider-instance

Conversation

@heiwen
Copy link
Copy Markdown
Contributor

@heiwen heiwen commented Apr 3, 2026

Background

@ai-sdk/google-vertex used a module-level GoogleAuth cache in the shared Node auth helper. That cache was invalidated by object reference equality, which caused unnecessary GoogleAuth recreation in two cases:

  1. Multiple provider instances with equivalent googleAuthOptions would recreate auth when requests alternated between them.
  2. When googleAuthOptions was 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 GoogleAuth caches 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 GoogleAuth singleton from @ai-sdk/google-vertex's shared Node auth helper and replaces it with a per-provider auth token generator.

Changes included:

  • Replaced the shared generateAuthToken(options) helper with createAuthTokenGenerator(options), which creates one GoogleAuth instance and returns a reusable token generator closure.
  • Updated the Node provider entry points for:
    • @ai-sdk/google-vertex
    • @ai-sdk/google-vertex/anthropic
    • @ai-sdk/google-vertex/maas
  • Added regression tests covering:
    • repeated calls with omitted googleAuthOptions
    • repeated calls on a single provider instance
    • per-provider generator creation for the Node provider variants
  • Added a patch changeset for @ai-sdk/google-vertex

Manual 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:

  • confirmed each Node provider now creates createAuthTokenGenerator(options.googleAuthOptions) once at provider initialization
  • confirmed request-time header/fetch code now calls the returned generator with no options, so the undefined -> {} invalidation path is gone
  • confirmed the shared helper no longer stores module-level auth state

I also verified behavior through automated coverage:

  • pnpm --filter @ai-sdk/google-vertex test:node
  • pnpm type-check:full

Checklist

  • All commits are signed (PRs with unsigned commits cannot be merged)
  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

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

@tigent tigent Bot added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label bug Something isn't working as documented provider/google-vertex Issues related to the @ai-sdk/google-vertex provider labels Apr 3, 2026
Copy link
Copy Markdown
Collaborator

@aayush-kapoor aayush-kapoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for the PR! changes look good, you'll just need to resolve the conflicts and sign your commits please.

@heiwen heiwen force-pushed the reuse-google-auth-per-provider-instance branch 2 times, most recently from e847339 to bb137a4 Compare April 30, 2026 01:05
@heiwen
Copy link
Copy Markdown
Contributor Author

heiwen commented Apr 30, 2026

Conflicts resolved and commits signed.

@aayush-kapoor
Copy link
Copy Markdown
Collaborator

apologies for the back and forth but there's a change coming in #14857 that will cause this to conflict again. i'll ask you to resolve it again since that will be merged first. thank you

@heiwen heiwen force-pushed the reuse-google-auth-per-provider-instance branch from bb137a4 to 20b3dcb Compare May 1, 2026 01:26
@heiwen
Copy link
Copy Markdown
Contributor Author

heiwen commented May 1, 2026

No Problem. Merge conflicts solved.

@aayush-kapoor aayush-kapoor merged commit 96d056d into vercel:main May 1, 2026
17 of 18 checks passed
@aayush-kapoor aayush-kapoor added the backport Admins only: add this label to a pull request in order to backport it to the prior version label May 1, 2026
github-actions Bot added a commit that referenced this pull request May 1, 2026
@github-actions github-actions Bot removed the backport Admins only: add this label to a pull request in order to backport it to the prior version label May 1, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 1, 2026

⚠️ Backport to release-v6.0 created but has conflicts: #14916

gr2m pushed a commit that referenced this pull request May 1, 2026
## Background

the change in #14102 didn't pull from
latest main and didn't account for the latest vertex/xai package
created. and thus `main` on that path was using the wrong import

## Summary

use correct import
aayush-kapoor added a commit that referenced this pull request May 1, 2026
This is an automated backport of #14102 to the release-v6.0 branch. FYI
@heiwen
This backport has conflicts that need to be resolved manually.

### `git cherry-pick` output

```
Auto-merging packages/google-vertex/src/anthropic/google-vertex-anthropic-provider-node.test.ts
CONFLICT (content): Merge conflict in packages/google-vertex/src/anthropic/google-vertex-anthropic-provider-node.test.ts
Auto-merging packages/google-vertex/src/anthropic/google-vertex-anthropic-provider-node.ts
Auto-merging packages/google-vertex/src/google-vertex-auth-google-auth-library.ts
CONFLICT (content): Merge conflict in packages/google-vertex/src/google-vertex-auth-google-auth-library.ts
Auto-merging packages/google-vertex/src/google-vertex-provider.test.ts
CONFLICT (content): Merge conflict in packages/google-vertex/src/google-vertex-provider.test.ts
Auto-merging packages/google-vertex/src/google-vertex-provider.ts
CONFLICT (content): Merge conflict in packages/google-vertex/src/google-vertex-provider.ts
Auto-merging packages/google-vertex/src/maas/google-vertex-maas-provider-node.test.ts
Auto-merging packages/google-vertex/src/maas/google-vertex-maas-provider-node.ts
error: could not apply 96d056d... fix: reuse google auth per provider instance (#14102)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
```

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Aayush Kapoor <aayushkapoor34@gmail.com>
Co-authored-by: Aayush Kapoor <83492835+aayush-kapoor@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label bug Something isn't working as documented provider/google-vertex Issues related to the @ai-sdk/google-vertex provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@ai-sdk/google-vertex Node auth helper recreates GoogleAuth too often due to reference-based cache invalidation

2 participants