vercel/ai · error · Error

The Google Vertex AI model function cannot be called with th

Error message

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

What it means

The Vertex provider is a callable function, not a class constructor. The provider function checks `new.target` and throws this Error if it is invoked with `new`. This guards against JavaScript users treating the factory like a class.

Source

Thrown at packages/google-vertex/src/google-vertex-provider-base.ts:398

        fetch: config.fetch,
        webSocket: options.webSocket,
        project: loadGoogleVertexProject(),
        location: loadGoogleVertexLocation(),
      });
    }

    return new GoogleVertexTranscriptionModel(modelId, {
      provider: config.provider,
      headers: config.headers,
      fetch: config.fetch,
      project: loadGoogleVertexProject(),
      location: loadGoogleVertexLocation(),
    });
  };

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

    return createChatModel(modelId);
  };

  provider.specificationVersion = 'v4' as const;
  provider.languageModel = createChatModel;
  provider.interactions = createInteractionsModel;
  provider.embeddingModel = createEmbeddingModel;
  provider.textEmbeddingModel = createEmbeddingModel;
  provider.image = createImageModel;
  provider.imageModel = createImageModel;
  provider.video = createVideoModel;
  provider.videoModel = createVideoModel;
  provider.speech = createSpeechModel;
  provider.speechModel = createSpeechModel;

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Remove the `new` keyword and call the provider directly: vertex('model-id').
  2. Call vertex.chat(modelId) explicitly if you want the chat entry point.
  3. For provider construction itself, call createGoogleVertex(options) without `new`.

Example fix

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

Strategy: type-guard

Validate before calling

if (typeof vertex !== 'function') throw new Error('vertex provider must be called as a function, not constructed.');

Type guard

function isProviderFunction(v: unknown): v is (modelId: string) => LanguageModel {
  return typeof v === 'function';
}

Prevention

When it happens

Trigger: Writing `new vertex('gemini-2.0-flash')` (or `new createGoogleVertex(...)`) in JS/TS — the `if (new.target)` branch throws instead of silently producing a broken object.

Common situations: Plain JavaScript callers mirroring class-style provider code from other libraries; refactoring from an older class-based client (e.g. VertexAI from @google-cloud/vertexai) where `new` was required; accidental `new` added by autocomplete or codemods.

Related errors


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