diff --git a/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/GenerativeModel.java b/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/GenerativeModel.java index a8b3b72858b3..689d00cb3f1e 100644 --- a/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/GenerativeModel.java +++ b/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/GenerativeModel.java @@ -504,6 +504,16 @@ private ApiFuture generateContentAsync(GenerateContentR return vertexAi.getPredictionServiceClient().generateContentCallable().futureCall(request); } + /** + * Removes the role in the system instruction content. + * + * @param systemInstruction a {@link com.google.cloud.vertexai.api.Content} instance + * @return a {@link com.google.cloud.vertexai.api.Content} instance with the role removed + */ + private Content removeRoleInSystemInstructionContent(Content systemInstruction) { + return systemInstruction.toBuilder().clearRole().build(); + } + /** * Builds a {@link com.google.cloud.vertexai.api.GenerateContentRequest} based on a list of * contents and model configurations. @@ -519,7 +529,8 @@ private GenerateContentRequest buildGenerateContentRequest(List content .addAllTools(tools); if (systemInstruction.isPresent()) { - requestBuilder.setSystemInstruction(systemInstruction.get()); + requestBuilder.setSystemInstruction( + removeRoleInSystemInstructionContent(systemInstruction.get())); } return requestBuilder.build(); diff --git a/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/GenerativeModelTest.java b/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/GenerativeModelTest.java index e61f6fe32090..c52f3be380ba 100644 --- a/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/GenerativeModelTest.java +++ b/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/GenerativeModelTest.java @@ -344,6 +344,7 @@ public void testGenerateContentwithSystemInstruction() throws Exception { verify(mockUnaryCallable).call(request.capture()); assertThat(request.getValue().getSystemInstruction().getParts(0).getText()) .isEqualTo(systemInstructionText); + assertThat(request.getValue().getSystemInstruction().getRole()).isEqualTo(""); } @Test