From 7897690a3be55f8d0ddf3640209bc3e7a2b64cb3 Mon Sep 17 00:00:00 2001 From: Julien Perrochet Date: Fri, 19 Apr 2024 00:02:23 +0200 Subject: [PATCH] [fix] ServiceOutputParser fails if class has no fields --- .../dev/langchain4j/service/ServiceOutputParser.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/langchain4j/src/main/java/dev/langchain4j/service/ServiceOutputParser.java b/langchain4j/src/main/java/dev/langchain4j/service/ServiceOutputParser.java index 092943f1a..d4fb07cdb 100644 --- a/langchain4j/src/main/java/dev/langchain4j/service/ServiceOutputParser.java +++ b/langchain4j/src/main/java/dev/langchain4j/service/ServiceOutputParser.java @@ -119,20 +119,16 @@ public class ServiceOutputParser { } private static String jsonStructure(Class structured, Set> visited) { - StringBuilder jsonSchema = new StringBuilder(); - - jsonSchema.append("{\n"); + List descriptions = new ArrayList<>(); for (Field field : structured.getDeclaredFields()) { String name = field.getName(); if (name.equals("__$hits$__") || java.lang.reflect.Modifier.isStatic(field.getModifiers())) { // Skip coverage instrumentation field. continue; } - jsonSchema.append(format("\"%s\": (%s),\n", name, descriptionFor(field, visited))); + descriptions.add(format("\"%s\": (%s)", name, descriptionFor(field, visited))); } - jsonSchema.delete(jsonSchema.lastIndexOf(","), jsonSchema.lastIndexOf(",")+1); - jsonSchema.append("}"); - return jsonSchema.toString(); + return "{\n" + String.join(",\n", descriptions) + "\n}"; } private static String descriptionFor(Field field, Set> visited) {