From 1fdde0313d8c993f51382ff9580c4b3b5876d2e8 Mon Sep 17 00:00:00 2001 From: Kevin Gordillo Date: Tue, 7 Jul 2026 22:26:41 +0000 Subject: [PATCH 1/2] refactor: remove deprecated controlled generation schema sample file --- .../gemini/ControlledGenerationSchema6.java | 82 ------------------- 1 file changed, 82 deletions(-) delete mode 100644 vertexai/snippets/src/main/java/vertexai/gemini/ControlledGenerationSchema6.java diff --git a/vertexai/snippets/src/main/java/vertexai/gemini/ControlledGenerationSchema6.java b/vertexai/snippets/src/main/java/vertexai/gemini/ControlledGenerationSchema6.java deleted file mode 100644 index 8396c03c3e9..00000000000 --- a/vertexai/snippets/src/main/java/vertexai/gemini/ControlledGenerationSchema6.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package vertexai.gemini; - -// [START generativeaionvertexai_gemini_controlled_generation_response_schema_6] -import com.google.cloud.vertexai.VertexAI; -import com.google.cloud.vertexai.api.GenerateContentResponse; -import com.google.cloud.vertexai.api.GenerationConfig; -import com.google.cloud.vertexai.api.Schema; -import com.google.cloud.vertexai.api.Type; -import com.google.cloud.vertexai.generativeai.ContentMaker; -import com.google.cloud.vertexai.generativeai.GenerativeModel; -import com.google.cloud.vertexai.generativeai.PartMaker; -import com.google.cloud.vertexai.generativeai.ResponseHandler; -import java.io.IOException; - -public class ControlledGenerationSchema6 { - public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "genai-java-demos"; - String location = "us-central1"; - String modelName = "gemini-2.5-flash"; - - controlGenerationWithJsonSchema6(projectId, location, modelName); - } - - // Generate responses that are always valid JSON and comply with a JSON schema - public static String controlGenerationWithJsonSchema6( - String projectId, String location, String modelName) - throws IOException { - // Initialize client that will be used to send requests. This client only needs - // to be created once, and can be reused for multiple requests. - try (VertexAI vertexAI = new VertexAI(projectId, location)) { - GenerationConfig generationConfig = GenerationConfig.newBuilder() - .setResponseMimeType("application/json") - .setResponseSchema(Schema.newBuilder() - .setType(Type.ARRAY) - .setItems(Schema.newBuilder() - .setType(Type.OBJECT) - .putProperties("object", Schema.newBuilder().setType(Type.STRING).build()) - .build()) - .build()) - .build(); - - GenerativeModel model = new GenerativeModel(modelName, vertexAI) - .withGenerationConfig(generationConfig); - - // These images in Cloud Storage are viewable at - // https://storage.googleapis.com/cloud-samples-data/generative-ai/image/office-desk.jpeg - // https://storage.googleapis.com/cloud-samples-data/generative-ai/image/gardening-tools.jpeg - - GenerateContentResponse response = model.generateContent( - ContentMaker.fromMultiModalData( - PartMaker.fromMimeTypeAndData("image/jpeg", - "gs://cloud-samples-data/generative-ai/image/office-desk.jpeg"), - PartMaker.fromMimeTypeAndData("image/jpeg", - "gs://cloud-samples-data/generative-ai/image/gardening-tools.jpeg"), - "Generate a list of objects in the images." - ) - ); - - String output = ResponseHandler.getText(response); - System.out.println(output); - return output; - } - } -} -// [END generativeaionvertexai_gemini_controlled_generation_response_schema_6] \ No newline at end of file From c741542c62f7dc34762be460c0588138179bbb45 Mon Sep 17 00:00:00 2001 From: Kevin Gordillo Date: Tue, 7 Jul 2026 23:09:53 +0000 Subject: [PATCH 2/2] test: remove unused testControlledGenerationWithJsonSchema6 method from SnippetsIT --- .../test/java/vertexai/gemini/SnippetsIT.java | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/vertexai/snippets/src/test/java/vertexai/gemini/SnippetsIT.java b/vertexai/snippets/src/test/java/vertexai/gemini/SnippetsIT.java index 1b496c48c9e..7eb180dab16 100644 --- a/vertexai/snippets/src/test/java/vertexai/gemini/SnippetsIT.java +++ b/vertexai/snippets/src/test/java/vertexai/gemini/SnippetsIT.java @@ -202,26 +202,4 @@ public void testMultimodalNonStreaming() throws Exception { assertThat(output).ignoringCase().contains("no"); } - - private class Obj { - public String object; - } - - @Test - public void testControlledGenerationWithJsonSchema6() throws Exception { - String output = ControlledGenerationSchema6 - .controlGenerationWithJsonSchema6(PROJECT_ID, LOCATION, GEMINI_FLASH); - - Obj[] objects = new Gson().fromJson(output, Obj[].class); - String recognizedObjects = Arrays.stream(objects) - .map(obj -> obj.object.toLowerCase()) - .collect(Collectors.joining(" ")); - - assertThat(recognizedObjects).isNotEmpty(); - assertThat(recognizedObjects).contains("globe"); - assertThat(recognizedObjects).contains("keyboard"); - assertThat(recognizedObjects).contains("passport"); - assertThat(recognizedObjects).contains("pot"); - } - }