Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions sdks/typescript/src/providers/ai-sdk-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,42 @@ 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'
);
});
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'
);
});
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'
);
Expand Down
Loading