|
16 | 16 |
|
17 | 17 | package org.springframework.ai.vertexai.gemini; |
18 | 18 |
|
19 | | -import com.google.cloud.vertexai.api.Tool.GoogleSearch; |
20 | 19 | import java.util.ArrayList; |
21 | 20 | import java.util.Collection; |
22 | 21 | import java.util.List; |
23 | 22 | import java.util.Map; |
24 | 23 |
|
25 | 24 | import com.fasterxml.jackson.annotation.JsonInclude; |
26 | 25 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| 26 | +import com.fasterxml.jackson.databind.JsonNode; |
27 | 27 | import com.google.cloud.vertexai.VertexAI; |
28 | 28 | import com.google.cloud.vertexai.api.Candidate; |
29 | 29 | import com.google.cloud.vertexai.api.Candidate.FinishReason; |
|
33 | 33 | import com.google.cloud.vertexai.api.FunctionResponse; |
34 | 34 | import com.google.cloud.vertexai.api.GenerateContentResponse; |
35 | 35 | import com.google.cloud.vertexai.api.GenerationConfig; |
36 | | -import com.google.cloud.vertexai.api.GoogleSearchRetrieval; |
37 | 36 | import com.google.cloud.vertexai.api.Part; |
38 | 37 | import com.google.cloud.vertexai.api.SafetySetting; |
39 | 38 | import com.google.cloud.vertexai.api.Schema; |
40 | 39 | import com.google.cloud.vertexai.api.Tool; |
| 40 | +import com.google.cloud.vertexai.api.Tool.GoogleSearch; |
41 | 41 | import com.google.cloud.vertexai.generativeai.GenerativeModel; |
42 | 42 | import com.google.cloud.vertexai.generativeai.PartMaker; |
43 | 43 | import com.google.cloud.vertexai.generativeai.ResponseStream; |
44 | 44 | import com.google.protobuf.Struct; |
| 45 | +import com.google.protobuf.Value; |
45 | 46 | import com.google.protobuf.util.JsonFormat; |
46 | 47 | import io.micrometer.observation.Observation; |
47 | 48 | import io.micrometer.observation.ObservationRegistry; |
@@ -226,7 +227,8 @@ public VertexAiGeminiChatModel(VertexAI vertexAI, VertexAiGeminiChatOptions defa |
226 | 227 | this.observationRegistry = observationRegistry; |
227 | 228 | this.toolExecutionEligibilityPredicate = toolExecutionEligibilityPredicate; |
228 | 229 |
|
229 | | - // Wrap the provided tool calling manager in a VertexToolCallingManager to ensure |
| 230 | + // Wrap the provided tool calling manager in a VertexToolCallingManager to |
| 231 | + // ensure |
230 | 232 | // compatibility with Vertex AI's OpenAPI schema format. |
231 | 233 | if (toolCallingManager instanceof VertexToolCallingManager) { |
232 | 234 | this.toolCallingManager = toolCallingManager; |
@@ -334,8 +336,34 @@ private static String structToJson(Struct struct) { |
334 | 336 |
|
335 | 337 | private static Struct jsonToStruct(String json) { |
336 | 338 | try { |
337 | | - var structBuilder = Struct.newBuilder(); |
338 | | - JsonFormat.parser().ignoringUnknownFields().merge(json, structBuilder); |
| 339 | + JsonNode rootNode = ModelOptionsUtils.OBJECT_MAPPER.readTree(json); |
| 340 | + |
| 341 | + Struct.Builder structBuilder = Struct.newBuilder(); |
| 342 | + |
| 343 | + if (rootNode.isArray()) { |
| 344 | + // Handle JSON array |
| 345 | + List<Value> values = new ArrayList<>(); |
| 346 | + |
| 347 | + for (JsonNode element : rootNode) { |
| 348 | + String elementJson = element.toString(); |
| 349 | + Struct.Builder elementBuilder = Struct.newBuilder(); |
| 350 | + JsonFormat.parser().ignoringUnknownFields().merge(elementJson, elementBuilder); |
| 351 | + |
| 352 | + // Add each parsed object as a value in an array field |
| 353 | + values.add(Value.newBuilder().setStructValue(elementBuilder.build()).build()); |
| 354 | + } |
| 355 | + |
| 356 | + // Add the array to the main struct with a field name like "items" |
| 357 | + structBuilder.putFields("items", |
| 358 | + Value.newBuilder() |
| 359 | + .setListValue(com.google.protobuf.ListValue.newBuilder().addAllValues(values).build()) |
| 360 | + .build()); |
| 361 | + } |
| 362 | + else { |
| 363 | + // Original behavior for single JSON object |
| 364 | + JsonFormat.parser().ignoringUnknownFields().merge(json, structBuilder); |
| 365 | + } |
| 366 | + |
339 | 367 | return structBuilder.build(); |
340 | 368 | } |
341 | 369 | catch (Exception e) { |
|
0 commit comments