{"record":{"id":"42de257451e4b4c9","repo":"vercel/ai","slug":"the-google-vertex-xai-model-function-cannot-be-cal","errorCode":null,"errorMessage":"The Google Vertex xAI model function cannot be called with the new keyword.","messagePattern":"The Google Vertex xAI model function cannot be called with the new keyword\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/google-vertex/src/xai/google-vertex-xai-provider.ts","lineNumber":191,"sourceCode":"    (cachedProvider ??= createOpenAICompatible({\n      name: 'googleVertex.xai',\n      baseURL: loadBaseURL(),\n      fetch: options.fetch,\n      includeUsage: true,\n      supportsStructuredOutputs: true,\n      supportedUrls: () => ({\n        'image/*': [/^https?:\\/\\/.*$/],\n      }),\n      transformRequestBody: transformGoogleVertexXaiRequestBody,\n      convertUsage: convertGoogleVertexXaiUsage,\n    }));\n\n  const createChatModel = (modelId: GoogleVertexXaiModelId) =>\n    getProvider().languageModel(modelId);\n\n  const provider = function (modelId: GoogleVertexXaiModelId) {\n    if (new.target) {\n      throw new Error(\n        'The Google Vertex xAI 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.chatModel = (modelId: GoogleVertexXaiModelId) =>\n    getProvider().chatModel(modelId);\n  provider.embeddingModel = (modelId: string): never => {\n    throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });\n  };\n  provider.textEmbeddingModel = provider.embeddingModel;\n  provider.imageModel = (modelId: string): never => {\n    throw new NoSuchModelError({ modelId, modelType: 'imageModel' });\n  };","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/google-vertex/src/xai/google-vertex-xai-provider.ts#L173-L209","documentation":"The Google Vertex xAI provider export is a callable function, not a class. Calling it with `new` (e.g. `new googleVertex.xai(...)`) is explicitly rejected because the factory creates model instances via closure state and is not designed as a constructor. This guard gives a clear message instead of a confusing runtime failure inside the factory.","triggerScenarios":"Writing `new googleVertex('model-id')` or `new googleVertex.xai('grok-...')` where googleVertex.xai is the provider function defined at packages/google-vertex/src/xai/google-vertex-xai-provider.ts:191. Detected via `new.target` inside the provider function.","commonSituations":"Migrating code that used a class-style provider (`new Vertex(...)`) to the current function-style factory API; copying provider examples from older blog posts; TypeScript users treating the provider as a constructor type.","solutions":["Remove the `new` keyword and call the provider directly: `const model = googleVertex.xai('grok-3')`.","If constructing the provider itself, use the factory `createGoogleVertexXAI({ ... })` or `createGoogleVertex({ ... })` without `new`.","Update older class-based provider examples/snippets to the current function-call style."],"exampleFix":"// before\nconst model = new googleVertex.xai('grok-3-pro');\n// after\nconst model = googleVertex.xai('grok-3-pro');","handlingStrategy":"validation","validationCode":"function isProviderFunction(fn) { return typeof fn === 'function' && !fn.prototype; }\n// always call without new:\nconst model = googleVertex.xai('grok-3-pro');","typeGuard":"function isCallableProvider(v) { return typeof v === 'function' && !(v.prototype && Object.getOwnPropertyNames(v.prototype).length > 0); }","tryCatchPattern":"try {\n  const model = googleVertex.xai(modelId);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('cannot be called with the new keyword')) {\n    const model = googleVertex.xai(modelId); // retry without new\n  } else throw e;\n}","preventionTips":["Treat all AI SDK providers as factory functions, never classes.","Enable lint rule 'no-new' for provider modules.","Update legacy `new Provider()` snippets when upgrading SDK versions."],"tags":["google-vertex","xai","api-misuse","typescript"],"backgroundTag":"provider-called-with-new","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}