Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

vertexAI generateContent returns HTML not json

It's my first vertexAI node code. 
I'm running this in my windows glcoud cli. 
This works fine when I curl generateContent, but not when using nodeJS library.
 
This is the error:
 
undefined:1
<!DOCTYPE html>
^
 
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
.
.
.
    at async throwErrorIfNotOK (C:\code\node\node_modules\@google-cloud\vertexai\build\src\functions\post_fetch_processing.js:29:27)
    at async generateContent (C:\code\node\node_modules\@google-cloud\vertexai\build\src\functions\generate_content.js:58:5)
    at async run (C:\code\node\testvert.js:19:15)
 
Node.js v22.9.0
 
Here's the relevant snippet:
 
    const {
        VertexAI
    } = require('@google-cloud/vertexai');
    
    const vertexAI = new VertexAI({project: PROJECT_NAME, location: 'asia-south1-a'});
    
    // Instantiate Gemini models
    const jdGenerativeModel = vertexAI.getGenerativeModel({model: 'gemini-1.0-pro'});
    
    async function run(jobDesc) {
        let command = "What is the capital of India?";
        let request = {
            contents: [{role: 'user', parts: command}]
        };
        let result = await jdGenerativeModel.generateContent(command);
        let response = await result.json();
        console.log(response);
    };
    
    run();
 
The error points to this line: jdGenerativeModel.generateContent(command);
 
I've tried cutting down all the unnecessary lines from the original code, down to bare bones. 
Followed the same syntax and flow given in the gcloud documentation pages. 
Uninstalled google cloud app on my local dev box, restarted and reinstalled.
Tried different models like gemini-1.5-pro/flash.  (I only need text processing capabilities)
I'm not sure if it's an authentication issue but I've run gcloud init auth a couple of times. 
It works when I use curl :
 
    "headers":{
    "content-type": "application/json"
    },
    "body":"{\"contents\":[{\"parts\":[{\"text\":\"What is the capital of India?\"}]}]}",
    "method": "POST"
    });
let r1 = await response.json();
console.log(r1.candidates[0].content.parts[0].text);
 
This is from gcloud CLI on my windows machine.
0 2 2,080
2 REPLIES 2

Hi @dhavalg8954,

Welcome to Google Cloud Community!

The error message "SyntaxError: Unexpected token '<', '<!DOCTYPE ...' is not valid JSON" indicates that the response from the generateContent method is not valid JSON. This often occurs when the response is in HTML or another format rather than JSON.

Here’s a breakdown of potential issues and troubleshooting steps:

1. Authentication Issues:

  • Verify Authentication: Even after running gcloud init auth, it’s a good idea to confirm that your authentication is still valid. Run gcloud auth list to check if your account is listed and associated with the correct project.
  • Service Account: If you’re using a service account, ensure it has the necessary permissions to access the Vertex AI API. Check the IAM roles assigned to the service account.

2. API Endpoint and Model:

  • Correct Endpoint: Ensure that you are using the correct endpoint for the Vertex AI API as specified in this documentation.
  • Model Availability: Confirm that the model you’re attempting to use (e.g., gemini-1.0-pro) is available in your region. If it isn’t, you may need to select a different model.

3. Node.js Library Configuration:

  • Project and Location: Check that the PROJECT_NAME and location variables in your Node.js code are correctly set.
  • Library Version: Ensure you are using the latest version of the @google-cloud/vertexai library, as older versions might have compatibility issues.

4. Response Handling:

  • JSON Parsing: The response from the generateContent method may require parsing. Use response.json() to convert the response into a JavaScript object.

Troubleshooting Steps:

  • Verify Authentication: Run gcloud auth list to confirm your authentication status. You can check this documentation for guidance on setting up and managing authentication.
  • Check API Endpoint: Make sure you are using the correct endpoint for the Vertex AI API.
  • Review Model Availability: Ensure the model you are using is accessible in your region.
  • Inspect Response: Use console.log(response) to examine the raw response from the generateContent method, helping you understand its format and identify any issues.

I hope the above information is helpful.

Thank you for the detailed resolution steps. These steps are very useful.

I was able to resolve the problem by changing my PROJECT_NAME variable to
the value auto-generated by Google Cloud, rather than using the name that I
had given it.

The error messaging could be clearer though, like "project xyz not found."