From 64b700a8124b004e9bcaa00510fb73777a5b9432 Mon Sep 17 00:00:00 2001 From: Adnan Rashid Hussain Date: Mon, 15 Jun 2026 12:11:22 -0700 Subject: [PATCH] fix(typescript-sdk): prevent bundlers from requiring unused @ai-sdk providers --- .../src/providers/ai-sdk-provider.ts | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/sdks/typescript/src/providers/ai-sdk-provider.ts b/sdks/typescript/src/providers/ai-sdk-provider.ts index a7ef437..f7a497f 100644 --- a/sdks/typescript/src/providers/ai-sdk-provider.ts +++ b/sdks/typescript/src/providers/ai-sdk-provider.ts @@ -83,14 +83,22 @@ export class VercelAIProvider implements LLMProvider { /** * Get the configured language model. - * Uses dynamic imports so consumers only need to install the provider packages they use. + * + * Uses dynamic imports so consumers only need to install the provider packages + * they actually use. The `webpackIgnore` / `turbopackIgnore` / `@vite-ignore` + * magic comments stop bundlers (Next.js, webpack, Vite/Rollup, etc.) from + * statically resolving the uninstalled providers at build time — without them, + * bundling fails with "Module not found: Can't resolve '@ai-sdk/...'" even + * though the import is guarded at runtime. */ private async getModel() { const apiKey = this.config.apiKey; switch (this.config.type) { case 'openai': { - const { createOpenAI } = await import('@ai-sdk/openai').catch(() => { + const { createOpenAI } = await import( + /* webpackIgnore: true */ /* turbopackIgnore: true */ /* @vite-ignore */ '@ai-sdk/openai' + ).catch(() => { throw new Error( 'To use the OpenAI provider, install its adapter: npm install @ai-sdk/openai' ); @@ -98,7 +106,9 @@ export class VercelAIProvider implements LLMProvider { return createOpenAI(apiKey ? { apiKey } : {})(this.model); } case 'anthropic': { - const { createAnthropic } = await import('@ai-sdk/anthropic').catch(() => { + const { createAnthropic } = await import( + /* webpackIgnore: true */ /* turbopackIgnore: true */ /* @vite-ignore */ '@ai-sdk/anthropic' + ).catch(() => { throw new Error( 'To use the Anthropic provider, install its adapter: npm install @ai-sdk/anthropic' ); @@ -106,7 +116,9 @@ export class VercelAIProvider implements LLMProvider { return createAnthropic(apiKey ? { apiKey } : {})(this.model); } case 'google': { - const { createGoogleGenerativeAI } = await import('@ai-sdk/google').catch(() => { + const { createGoogleGenerativeAI } = await import( + /* webpackIgnore: true */ /* turbopackIgnore: true */ /* @vite-ignore */ '@ai-sdk/google' + ).catch(() => { throw new Error( 'To use the Google provider, install its adapter: npm install @ai-sdk/google' );