{"record":{"id":"e0baa883477e27d8","repo":"vercel/ai","slug":"the-google-generative-ai-model-function-cannot-be","errorCode":null,"errorMessage":"The Google Generative AI model function cannot be called with the new keyword.","messagePattern":"The Google Generative AI model function cannot be called with the new keyword\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/google/src/google-provider.ts","lineNumber":401,"sourceCode":"    modelIdOrAgent:\n      | GoogleInteractionsModelId\n      | { agent: GoogleInteractionsAgentName }\n      | { managedAgent: string },\n  ) =>\n    new GoogleInteractionsLanguageModel(\n      modelIdOrAgent as GoogleInteractionsModelInput,\n      {\n        provider: `${providerName}.interactions`,\n        baseURL,\n        headers: getHeaders,\n        generateId: options.generateId ?? generateId,\n        fetch: options.fetch,\n      },\n    );\n\n  const provider = function (modelId: GoogleModelId) {\n    if (new.target) {\n      throw new Error(\n        'The Google Generative AI model function cannot be called with the new keyword.',\n      );\n    }\n\n    return createChatModel(modelId);\n  };\n\n  provider.specificationVersion = 'v4' as const;\n  provider.languageModel = createChatModel;\n  provider.chat = createChatModel;\n  provider.generativeAI = createChatModel;\n  provider.embedding = createEmbeddingModel;\n  provider.embeddingModel = createEmbeddingModel;\n  provider.textEmbedding = createEmbeddingModel;\n  provider.textEmbeddingModel = createEmbeddingModel;\n  provider.image = createImageModel;\n  provider.imageModel = createImageModel;\n  provider.video = createVideoModel;","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/google/src/google-provider.ts#L383-L419","documentation":"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.","triggerScenarios":"Writing `new google('gemini-2.0-flash')` or `new createGoogleGenerativeAI(...)('...')` — i.e., invoking the provider function or returned model factory with the `new` keyword.","commonSituations":"Copy-pasting older provider patterns that used classes; TypeScript code where the provider was assumed to be a constructor; transpiled code emitting `new` calls.","solutions":["Call the provider function without `new`: `const model = google('gemini-2.0-flash')`","If constructing a custom provider instance, also call createGoogleGenerativeAI without `new`","Fix any code (or transpilation setting) that emits `new` for this factory"],"exampleFix":"// before\nconst model = new google('gemini-2.0-flash');\n// after\nconst model = google('gemini-2.0-flash');","handlingStrategy":"type-guard","validationCode":"if (typeof google !== 'function') {\n  throw new Error('google provider must be a callable factory');\n}","typeGuard":null,"tryCatchPattern":"let model;\ntry {\n  model = google('gemini-2.0-flash');\n} catch (error) {\n  if (error instanceof Error && error.message.includes('new keyword')) {\n    model = google('gemini-2.0-flash'); // call without new\n  } else throw error;\n}","preventionTips":["Never use `new` with AI SDK provider factories","Rely on TypeScript types — the provider is typed as a function, so `new` fails type-checking","Enable strict TS settings so misuse is caught at compile time"],"tags":["google","typescript","incorrect-usage","provider-factory"],"backgroundTag":"constructor-misuse","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}