You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by li...@apache.org on 2026/07/04 09:16:11 UTC

(seatunnel) branch dev updated: [Fix][Transform-V2] Parse custom LLM array string response (#11235)

This is an automated email from the ASF dual-hosted git repository.

davidzollo pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/seatunnel.git


The following commit(s) were added to refs/heads/dev by this push:
     new 1a891b8a4f [Fix][Transform-V2] Parse custom LLM array string response (#11235)
1a891b8a4f is described below

commit 1a891b8a4fde3c73fe4c8e2c96b6a0ef19ea1414
Author: Jast <sh...@apache.org>
AuthorDate: Sat Jul 4 17:16:05 2026 +0800

    [Fix][Transform-V2] Parse custom LLM array string response (#11235)
---
 .../nlpmodel/llm/remote/custom/CustomModel.java    | 16 ++++--
 .../transform/llm/LLMRequestJsonTest.java          | 67 ++++++++++++++++++++++
 2 files changed, 79 insertions(+), 4 deletions(-)

diff --git a/seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/nlpmodel/llm/remote/custom/CustomModel.java b/seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/nlpmodel/llm/remote/custom/CustomModel.java
index 67cda24062..8999997b8f 100644
--- a/seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/nlpmodel/llm/remote/custom/CustomModel.java
+++ b/seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/nlpmodel/llm/remote/custom/CustomModel.java
@@ -95,13 +95,21 @@ public class CustomModel extends AbstractModel {
         if (response.getStatusLine().getStatusCode() != 200) {
             throw new IOException("Failed to get vector from custom, response: " + responseStr);
         }
+        Object parsedResponse = parseResponse(responseStr);
+        if (parsedResponse instanceof String) {
+            String result = (String) parsedResponse;
+            try {
+                return OBJECT_MAPPER.readValue(
+                        convertData(result), new TypeReference<List<String>>() {});
+            } catch (Exception e) {
+                return Collections.singletonList(convertData(result));
+            }
+        }
         try {
-            return OBJECT_MAPPER.convertValue(
-                    parseResponse(responseStr), new TypeReference<List<String>>() {});
+            return OBJECT_MAPPER.convertValue(parsedResponse, new TypeReference<List<String>>() {});
         } catch (Exception e) {
             String result =
-                    OBJECT_MAPPER.convertValue(
-                            parseResponse(responseStr), new TypeReference<String>() {});
+                    OBJECT_MAPPER.convertValue(parsedResponse, new TypeReference<String>() {});
             return Collections.singletonList(result);
         }
     }
diff --git a/seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/llm/LLMRequestJsonTest.java b/seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/llm/LLMRequestJsonTest.java
index cf29cf3546..81e0055813 100644
--- a/seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/llm/LLMRequestJsonTest.java
+++ b/seatunnel-transforms-v2/src/test/java/org/apache/seatunnel/transform/llm/LLMRequestJsonTest.java
@@ -296,4 +296,71 @@ public class LLMRequestJsonTest {
             mockWebServer.shutdown();
         }
     }
+
+    @Test
+    void testCustomOpenAIContentArrayString() throws IOException {
+        MockWebServer mockWebServer = new MockWebServer();
+        mockWebServer.start();
+        try {
+            String mockBaseUrl = mockWebServer.url("/v1/chat/completions").toString();
+            String jsonResponse =
+                    "{\n"
+                            + "    \"choices\": [\n"
+                            + "        {\n"
+                            + "            \"message\": {\n"
+                            + "                \"role\": \"assistant\",\n"
+                            + "                \"content\": \"[\\\"sfsfsafasfsfasf\\\"]\"\n"
+                            + "            },\n"
+                            + "            \"finish_reason\": \"stop\",\n"
+                            + "            \"index\": 0\n"
+                            + "        }\n"
+                            + "    ]\n"
+                            + "}";
+
+            mockWebServer.enqueue(
+                    new MockResponse()
+                            .setBody(jsonResponse)
+                            .addHeader("Content-Type", "application/json"));
+
+            SeaTunnelRowType rowType =
+                    new SeaTunnelRowType(
+                            new String[] {"content"},
+                            new SeaTunnelDataType[] {BasicType.STRING_TYPE});
+
+            Map<String, String> header = new HashMap<>();
+            header.put("Content-Type", "application/json");
+
+            Map<String, Object> message = new HashMap<>();
+            message.put("role", "user");
+            message.put("content", "${input}");
+
+            Map<String, Object> resultMap = new HashMap<>();
+            resultMap.put("model", "${model}");
+            resultMap.put("messages", Collections.singletonList(message));
+
+            CustomModel model =
+                    new CustomModel(
+                            rowType,
+                            SqlType.STRING,
+                            Collections.singletonList("content"),
+                            "summary",
+                            "custom-model",
+                            mockBaseUrl,
+                            header,
+                            resultMap,
+                            "$.choices[0].message.content");
+            try {
+                SeaTunnelRow row = new SeaTunnelRow(rowType.getFieldTypes().length);
+                row.setField(0, "test");
+
+                List<String> result = model.inference(Collections.singletonList(row));
+
+                Assertions.assertEquals(Collections.singletonList("sfsfsafasfsfasf"), result);
+            } finally {
+                model.close();
+            }
+        } finally {
+            mockWebServer.shutdown();
+        }
+    }
 }