Skip to content

feat(vertex-ai-gemini): enhance jsonToStruct to support JSON arrays #2977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package org.springframework.ai.vertexai.gemini;

import com.google.cloud.vertexai.api.Tool.GoogleSearch;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.Candidate;
import com.google.cloud.vertexai.api.Candidate.FinishReason;
Expand All @@ -33,15 +33,16 @@
import com.google.cloud.vertexai.api.FunctionResponse;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.api.GenerationConfig;
import com.google.cloud.vertexai.api.GoogleSearchRetrieval;
import com.google.cloud.vertexai.api.Part;
import com.google.cloud.vertexai.api.SafetySetting;
import com.google.cloud.vertexai.api.Schema;
import com.google.cloud.vertexai.api.Tool;
import com.google.cloud.vertexai.api.Tool.GoogleSearch;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import com.google.cloud.vertexai.generativeai.PartMaker;
import com.google.cloud.vertexai.generativeai.ResponseStream;
import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
Expand Down Expand Up @@ -226,7 +227,8 @@ public VertexAiGeminiChatModel(VertexAI vertexAI, VertexAiGeminiChatOptions defa
this.observationRegistry = observationRegistry;
this.toolExecutionEligibilityPredicate = toolExecutionEligibilityPredicate;

// Wrap the provided tool calling manager in a VertexToolCallingManager to ensure
// Wrap the provided tool calling manager in a VertexToolCallingManager to
// ensure
// compatibility with Vertex AI's OpenAPI schema format.
if (toolCallingManager instanceof VertexToolCallingManager) {
this.toolCallingManager = toolCallingManager;
Expand Down Expand Up @@ -334,8 +336,34 @@ private static String structToJson(Struct struct) {

private static Struct jsonToStruct(String json) {
try {
var structBuilder = Struct.newBuilder();
JsonFormat.parser().ignoringUnknownFields().merge(json, structBuilder);
JsonNode rootNode = ModelOptionsUtils.OBJECT_MAPPER.readTree(json);

Struct.Builder structBuilder = Struct.newBuilder();

if (rootNode.isArray()) {
// Handle JSON array
List<Value> values = new ArrayList<>();

for (JsonNode element : rootNode) {
String elementJson = element.toString();
Struct.Builder elementBuilder = Struct.newBuilder();
JsonFormat.parser().ignoringUnknownFields().merge(elementJson, elementBuilder);

// Add each parsed object as a value in an array field
values.add(Value.newBuilder().setStructValue(elementBuilder.build()).build());
}

// Add the array to the main struct with a field name like "items"
structBuilder.putFields("items",
Value.newBuilder()
.setListValue(com.google.protobuf.ListValue.newBuilder().addAllValues(values).build())
.build());
}
else {
// Original behavior for single JSON object
JsonFormat.parser().ignoringUnknownFields().merge(json, structBuilder);
}

return structBuilder.build();
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void functionCallTestInferredOpenApiSchema() {

assertThat(chatResponse.getMetadata()).isNotNull();
assertThat(chatResponse.getMetadata().getUsage()).isNotNull();
assertThat(chatResponse.getMetadata().getUsage().getTotalTokens()).isGreaterThan(150).isLessThan(310);
assertThat(chatResponse.getMetadata().getUsage().getTotalTokens()).isGreaterThan(150).isLessThan(330);

ChatResponse response2 = this.chatModel
.call(new Prompt("What is the payment status for transaction 696?", promptOptions));
Expand Down Expand Up @@ -201,7 +201,7 @@ public void functionCallUsageTestInferredOpenApiSchemaStream() {
assertThat(chatResponse).isNotNull();
assertThat(chatResponse.getMetadata()).isNotNull();
assertThat(chatResponse.getMetadata().getUsage()).isNotNull();
assertThat(chatResponse.getMetadata().getUsage().getTotalTokens()).isGreaterThan(150).isLessThan(310);
assertThat(chatResponse.getMetadata().getUsage().getTotalTokens()).isGreaterThan(150).isLessThan(330);

}

Expand Down