Skip to content

fix(amazon-bedrock): resolve globalThis.fetch lazily to support telemetry and fetch patching#14365

Merged
aayush-kapoor merged 2 commits intovercel:mainfrom
heiwen:fix/bedrock-fetch-lazy-eval
May 1, 2026
Merged

fix(amazon-bedrock): resolve globalThis.fetch lazily to support telemetry and fetch patching#14365
aayush-kapoor merged 2 commits intovercel:mainfrom
heiwen:fix/bedrock-fetch-lazy-eval

Conversation

@heiwen
Copy link
Copy Markdown
Contributor

@heiwen heiwen commented Apr 12, 2026

Background

createAmazonBedrock() calls either createSigV4FetchFunction or createApiKeyFetchFunction at provider initialization time. Both functions accepted the underlying fetch implementation via a default parameter:

```ts
fetch: FetchFunction = globalThis.fetch
```

JavaScript default parameters are evaluated at call time — meaning `globalThis.fetch` is read and captured the moment `createAmazonBedrock()` is called. Any patch to `globalThis.fetch` applied after that point is silently ignored for all Bedrock requests.

The most common real-world impact is telemetry instrumentation (e.g. OpenTelemetry, Datadog, custom tracing), which typically patches `globalThis.fetch` at startup to intercept outgoing HTTP requests. Because Bedrock snapshots fetch before those patches are applied, all Bedrock requests bypass instrumentation. Test setups that patch `globalThis.fetch` after imports are equally affected.

Every other provider in the SDK avoids this by deferring the `globalThis.fetch` lookup to request time. The comment in `http-chat-transport.ts` captures the intent explicitly: "avoid caching globalThis.fetch in case it is patched by other libraries".

Summary

This PR makes `createSigV4FetchFunction` and `createApiKeyFetchFunction` resolve `globalThis.fetch` lazily inside the returned closure instead of at function call time.

Changes included:

  • Removed the `= globalThis.fetch` default parameter from both functions, making `fetch` optional
  • Added `const effectiveFetch = fetch ?? globalThis.fetch` inside each returned closure, so the lookup happens at request time
  • Added a patch changeset for `@ai-sdk/amazon-bedrock`

Manual Verification

I manually verified by inspecting the call path from `createAmazonBedrock()` through to the SigV4 and API key fetch wrappers:

  • confirmed the default parameter no longer captures `globalThis.fetch` at provider initialization
  • confirmed each request-time closure now resolves `globalThis.fetch` fresh on every call, consistent with how `post-to-api.ts` and `http-chat-transport.ts` handle this

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)

Related Issue

Fixes: #14364

@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/amazon-bedrock Issues related to the @ai-sdk/amazon-bedrock provider labels Apr 12, 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.

change looks good but how did you verify the change manually? + can you simulate it using unit tests? if so, please add them.

also please sign your commits for this PR to be merged

@heiwen heiwen force-pushed the fix/bedrock-fetch-lazy-eval branch 3 times, most recently from a365179 to 9ae580f Compare April 30, 2026 01:13
@heiwen
Copy link
Copy Markdown
Contributor Author

heiwen commented Apr 30, 2026

Merge conflict solved, commits signed, unit test added.

For manual verification, you can use this snippet:

const { bedrock } = await import('@ai-sdk/amazon-bedrock');

const originalFetch = globalThis.fetch;

globalThis.fetch = async (...args) => {
  console.log('wrapped fetch called');
  return originalFetch(...args);
};

await bedrock('amazon.nova-lite-v1:0').doGenerate({
  prompt: [
    {
      role: 'user',
      content: [{ type: 'text', text: 'hello' }],
    },
  ],
});

// console should show "wrapped fetch called"

@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 Apr 30, 2026
@aayush-kapoor
Copy link
Copy Markdown
Collaborator

please fix the merge conflicts and i'll get this merged

heiwen and others added 2 commits May 1, 2026 09:17
…etry and fetch patching

createSigV4FetchFunction and createApiKeyFetchFunction were capturing
globalThis.fetch at initialization time via a default parameter, causing
telemetry instrumentation (e.g. OpenTelemetry, Datadog) and other patches
applied to globalThis.fetch after provider creation to be silently ignored.
Resolve fetch inside the returned closure instead, matching the pattern
used elsewhere in the SDK.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@heiwen heiwen force-pushed the fix/bedrock-fetch-lazy-eval branch from 9ae580f to d2a5c93 Compare May 1, 2026 01:20
@heiwen
Copy link
Copy Markdown
Contributor Author

heiwen commented May 1, 2026

Merge conflicts solved

@aayush-kapoor aayush-kapoor merged commit d0dbd96 into vercel:main May 1, 2026
17 of 18 checks passed
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: #14914

gr2m pushed a commit that referenced this pull request May 1, 2026
…port telemetry and fetch patching (#14914)

This is an automated backport of #14365 to the release-v6.0 branch. FYI
@heiwen

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Aayush Kapoor <aayushkapoor34@gmail.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/amazon-bedrock Issues related to the @ai-sdk/amazon-bedrock provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: createAmazonBedrock() breaks telemetry by capturing fetch at initialization time

2 participants