Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Problem calling Gemini API from Java application.

I have set up a GCP with Vertex AI and enabled all the Api:s, but I can't get the model to respond in my Java application. Every call I make to gemini ends with a response error 500.

The weird thing is that it is working from a Python environment.

This is working:

import vertexai
from vertexai.generative_models import GenerativeModel
model = GenerativeModel(model_name="gemini-1.0-pro")

vertexai.init(project="PROJECT_ID", location="LOCATION")

responses = model.generate_content("Hello, can you hear me?", stream=True)
for response in responses:
    print(response.text)

This is not working:

VertexAI vertexAi = new VertexAI("PROJECT_ID", "LOCATION");
    try {
        GenerativeModel model = new GenerativeModel("gemini-1.0-pro", vertexAi);
        var response = model.generateContent("Hello, can you hear me?");
        System.out.println(response);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }

 Anyone knows what could be the problem?

0 1 687
1 REPLY 1

I have tried your python snippet I successfully called for a request. But for Java I have tried this documentation using the same model and also successfully call for Gemini response, Can you try again?:

nceniza_1-1713276882745.png