Skip to content

Commit 8064028

Browse files
yyyu-googlecopybara-github
authored andcommitted
fix: schema handling in transformer
PiperOrigin-RevId: 745790224
1 parent ce9e7b0 commit 8064028

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

examples/src/main/java/com/google/genai/examples/GenerateContentWithFunctionCall.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*/
4040
package com.google.genai.examples;
4141

42+
import com.google.common.collect.ImmutableList;
4243
import com.google.common.collect.ImmutableMap;
4344
import com.google.genai.Client;
4445
import com.google.genai.types.FunctionDeclaration;
@@ -64,7 +65,13 @@ public static void main(String[] args) {
6465
Schema.builder()
6566
.type("string")
6667
.description("The location to get the weather for.")
68+
.build(),
69+
"unit",
70+
Schema.builder()
71+
.type("string")
72+
.description("The unit to return the weather in, e.g. 'celsius'.")
6773
.build()))
74+
.required(ImmutableList.of("location", "unit"))
6875
.build())
6976
.build();
7077

examples/src/main/java/com/google/genai/examples/GenerateContentWithResponseSchema.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
package com.google.genai.examples;
4141

4242
import com.google.common.collect.ImmutableMap;
43+
import com.google.common.collect.ImmutableList;
4344
import com.google.genai.Client;
4445
import com.google.genai.types.GenerateContentConfig;
4546
import com.google.genai.types.GenerateContentResponse;
@@ -53,12 +54,23 @@ public class GenerateContentWithResponseSchema {
5354
public static void main(String[] args) {
5455
Client client = new Client();
5556

56-
Schema schema =
57-
Schema.builder()
58-
.type("object")
59-
.properties(
60-
ImmutableMap.of(
61-
"name", Schema.builder().type("string").description("Your Name").build()))
57+
Schema schema = Schema.builder()
58+
.type("ARRAY")
59+
.items(
60+
Schema.builder()
61+
.type("OBJECT")
62+
.properties(
63+
ImmutableMap.of(
64+
"recipe_name",
65+
Schema.builder().type("STRING").build(),
66+
"ingredients",
67+
Schema.builder()
68+
.type("ARRAY")
69+
.items(Schema.builder().type("STRING").build())
70+
.build()
71+
))
72+
.required(ImmutableList.of("recipe_name", "ingredients"))
73+
.build())
6274
.build();
6375
GenerateContentConfig config =
6476
GenerateContentConfig.builder()
@@ -68,7 +80,7 @@ public static void main(String[] args) {
6880
.build();
6981

7082
GenerateContentResponse response =
71-
client.models.generateContent("gemini-2.0-flash-001", "Tell me your name", config);
83+
client.models.generateContent("gemini-2.0-flash-001", "List a few popular cookie recipes.", config);
7284

7385
System.out.println("Response: " + response.text());
7486
}

src/main/java/com/google/genai/Transformers.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.genai.types.Tool;
2929
import com.google.genai.types.VoiceConfig;
3030
import java.util.ArrayList;
31+
import java.util.HashMap;
3132
import java.util.List;
3233
import java.util.Map;
3334
import org.jspecify.annotations.Nullable;
@@ -263,20 +264,18 @@ private static Schema processSchema(ApiClient apiClient, Schema schema) {
263264
}
264265

265266
if (schema.items().isPresent()) {
266-
schema = schema.toBuilder().items(processSchema(apiClient, schema.items().get())).build();
267+
Schema items = processSchema(apiClient, schema.items().get());
268+
schema = schema.toBuilder().items(items).build();
267269
}
268270

269271
if (schema.properties().isPresent()) {
272+
Map<String, Schema> properties = new HashMap<>();
270273
for (Map.Entry<String, Schema> entry : schema.properties().get().entrySet()) {
271274
String propertyName = entry.getKey();
272275
Schema propertySchema = entry.getValue();
273-
schema =
274-
schema.toBuilder()
275-
.properties(
276-
ImmutableMap.of(
277-
propertyName, processSchema(apiClient, propertySchema).toBuilder().build()))
278-
.build();
276+
properties.put(propertyName, processSchema(apiClient, propertySchema));
279277
}
278+
schema = schema.toBuilder().properties(ImmutableMap.copyOf(properties)).build();
280279
}
281280

282281
return schema;

src/test/java/com/google/genai/TransformerTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,28 @@ public void testTSchema_Items_success() {
8888
assertEquals("STRING", transformedSchema.items().get().type().get());
8989
assertEquals("ARRAY", transformedSchema.type().get());
9090
}
91+
92+
@Test
93+
public void testTSchema_Required_sucess() {
94+
Schema schema = Schema.builder()
95+
.type("ARRAY")
96+
.items(
97+
Schema.builder()
98+
.type("OBJECT")
99+
.properties(
100+
ImmutableMap.of(
101+
"recipe_name",
102+
Schema.builder().type("STRING").build(),
103+
"ingredients",
104+
Schema.builder()
105+
.type("ARRAY")
106+
.items(Schema.builder().type("STRING").build())
107+
.build()
108+
))
109+
.required(ImmutableList.of("recipe_name", "ingredients"))
110+
.build())
111+
.build();
112+
Schema transformedSchema = Transformers.tSchema(GEMINI_API_CLIENT, schema);
113+
assertEquals(schema, transformedSchema);
114+
}
91115
}

0 commit comments

Comments
 (0)