vercel/ai · error · Error

The Google Generative AI model function cannot be called wit

Error message

The Google Generative AI model function cannot be called with the new keyword.

What it means

The Google provider's default export is a plain function that creates chat models; calling it with `new` is not supported and throws this error immediately. This guards against JS/TS users mistakenly treating the factory as a class constructor.

Source

Thrown at packages/google/src/google-provider.ts:401

    modelIdOrAgent:
      | GoogleInteractionsModelId
      | { agent: GoogleInteractionsAgentName }
      | { managedAgent: string },
  ) =>
    new GoogleInteractionsLanguageModel(
      modelIdOrAgent as GoogleInteractionsModelInput,
      {
        provider: `${providerName}.interactions`,
        baseURL,
        headers: getHeaders,
        generateId: options.generateId ?? generateId,
        fetch: options.fetch,
      },
    );

  const provider = function (modelId: GoogleModelId) {
    if (new.target) {
      throw new Error(
        'The Google Generative AI model function cannot be called with the new keyword.',
      );
    }

    return createChatModel(modelId);
  };

  provider.specificationVersion = 'v4' as const;
  provider.languageModel = createChatModel;
  provider.chat = createChatModel;
  provider.generativeAI = createChatModel;
  provider.embedding = createEmbeddingModel;
  provider.embeddingModel = createEmbeddingModel;
  provider.textEmbedding = createEmbeddingModel;
  provider.textEmbeddingModel = createEmbeddingModel;
  provider.image = createImageModel;
  provider.imageModel = createImageModel;
  provider.video = createVideoModel;

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Call the provider function without `new`: `const model = google('gemini-2.0-flash')`
  2. If constructing a custom provider instance, also call createGoogleGenerativeAI without `new`
  3. Fix any code (or transpilation setting) that emits `new` for this factory

Example fix

// before
const model = new google('gemini-2.0-flash');
// after
const model = google('gemini-2.0-flash');
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof google !== 'function') {
  throw new Error('google provider must be a callable factory');
}

Try / catch

let model;
try {
  model = google('gemini-2.0-flash');
} catch (error) {
  if (error instanceof Error && error.message.includes('new keyword')) {
    model = google('gemini-2.0-flash'); // call without new
  } else throw error;
}

Prevention

When it happens

Trigger: Writing `new google('gemini-2.0-flash')` or `new createGoogleGenerativeAI(...)('...')` — i.e., invoking the provider function or returned model factory with the `new` keyword.

Common situations: Copy-pasting older provider patterns that used classes; TypeScript code where the provider was assumed to be a constructor; transpiled code emitting `new` calls.

Related errors


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/e0baa883477e27d8. Report an issue: GitHub.