Skip to content

generateContent() fails with error: 'The "listener" argument must be of type function. Received an instance of Object' #1393

@markabrahams

Description

@markabrahams

When using @google/genai in Vertex AI mode with service account credentials (googleAuthOptions.credentials), the first API call fails during the OAuth token exchange (the request to get an access token from the token endpoint), e.g. the first call to generateContent().

Environment details

  • Programming language: Node.js
  • OS: Ubuntu 24.04
  • Language runtime version: 24.13.0
  • Package version: 1.44.0

Steps to reproduce

  1. Create a new GoogleGenAI with googleAuthOptions.credentials and a GCP key object.
  2. Call generateContent() with this.

e.g.

const ai = new GoogleGenAI({
  vertexai: true,
  project: '...',
  location: '...',
  googleAuthOptions: {
    credentials: { private_key, etc. ... }
  }
});
ai.models.generateContent({
  model: "gemini-2.5-flash",
  contents: "Hello"
});

Error:

The resulting error is of the form:

Error: The "listener" argument must be of type function. Received an instance of Object
    at Gaxios._request (.../node_modules/gaxios/src/gaxios.ts:227:15)
    at getToken (.../node_modules/google-auth-library/build/src/gtoken/getToken.js:52:26)
    at async TokenHandler.getToken (.../node_modules/google-auth-library/build/src/gtoken/tokenHandler.js:83:27)
    at async JWT.refreshTokenNoCache (.../node_modules/google-auth-library/build/src/auth/jwtclient.js:189:23)
    at async JWT.getRequestMetadataAsync (.../node_modules/google-auth-library/build/src/auth/oauth2client.js:372:17)
    at async JWT.getRequestHeaders (.../node_modules/google-auth-library/build/src/auth/oauth2client.js:336:26)
    at async NodeAuth.addGoogleAuthHeaders (.../node_modules/@google/genai/dist/node/index.cjs:18538:29)
    ...
    at async VertexAiClient.query (...)

Workaround:

Set fetch implementation to Node native:

const ai = new GoogleGenAI({
  vertexai: true,
  project: '...',
  location: '...',
  googleAuthOptions: {
    credentials: { ... },
    clientOptions: {
      transporterOptions: { fetchImplementation: globalThis.fetch }
    }
  }
});

Metadata

Metadata

Assignees

Labels

api: vertex-aiIssues related to the Vertex AI API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions