You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring AI中反序列化字符串化JSON为对象数组时出现HttpMessageNotReadableException错误的排查求助

Spring AI中反序列化字符串化JSON为对象数组时出现HttpMessageNotReadableException错误的排查求助

大家好,我现在在使用Spring AI做结构化输出,目前LLM返回的是一个对象数组的字符串形式,但在反序列化的时候遇到了下面的错误,尝试直接把字符串转成对应的对象也没成功,想请大家帮忙看看问题出在哪。

错误信息

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.ArrayList<akl.p4p.uoa.data.Test>` from String value (token `JsonToken.VALUE_STRING`)

LLM返回的字符串化JSON内容

{"tests":[{"code":"int arr[] = {0, 5, 10, 15};\nint result = CountBetween(arr, 4, 3, 12);\nprintf(\"%d\", result);","expectedStdOut":"2"},{"code":"int arr[] = {0, 5, 10, 15};\nint result = CountBetween(arr, 4, 12, 3);\nprintf(\"%d\", result);","expectedStdOut":"2"},{"code":"int arr[] = {3, 12, 6, 9};\nint result = CountBetween(arr, 4, 3, 12);\nprintf(\"%d\", result);","expectedStdOut":"2"},{"code":"int arr[] = {4, 4, 4, 4};\nint result = CountBetween(arr, 4, 1, 5);\nprintf(\"%d\", result);","expectedStdOut":"4"},{"code":"int arr[] = {1, 2, 3};\nint result = CountBetween(arr, 0, 0, 10);\nprintf(\"%d\", result);","expectedStdOut":"0"},{"code":"int arr[] = {1, 2, 3};\nint result = CountBetween(arr, -5, 0, 10);\nprintf(\"%d\", result);","expectedStdOut":"0"},{"code":"int arr[] = {1, 2, 3};\nint result = CountBetween(arr, 3, 5, 5);\nprintf(\"%d\", result);","expectedStdOut":"0"},{"code":"int arr[] = {INT_MIN, INT_MIN + 1, 0, INT_MAX - 1, INT_MAX};\nint result = CountBetween(arr, 5, INT_MIN, INT_MAX);\nprintf(\"%d\", result);","expectedStdOut":"3"},{"code":"int result = CountBetween(NULL, 0, 0, 10);\nprintf(\"%d\", result);","expectedStdOut":"0"},{"code":"int result = CountBetween(NULL, 5, 0, 10);\nprintf(\"%d\", result);","expectedStdOut":"0"}]}

相关实体类代码

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Test {
    String code;
    String expectedStdOut;
}

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestResponse {
    List<Test> tests;
}

@Data
@Document("problems")
public class Problem {
    @Id
    private String id;
    private String problemStatement;
    private String modelAnswer;
    private String constraints;
    private List<Test> testSuite;
    private ProgramLanguage programLanguage;
    private ProblemType problemType;
    private String defaultProbe;
}

Controller代码

@PostMapping("test-suite")
public ResponseEntity<Problem> generateProblemTestSuite(@RequestBody Problem problem) throws JsonMappingException, JsonProcessingException {
    String modelAnswer = problem.getModelAnswer();
    String constraints = problem.getConstraints();
    String basePrompt = Prompts.getTestSuiteGenerationPrompt();
    String sysPrompt = basePrompt.replace("//VAR_MODEL_SOLUTION", modelAnswer)
            .replace("//VAR_CONSTRAINTS", constraints);

    String testSuite = aiService.executeOneTimeLLMCall(sysPrompt, JsonSchemaDefinition.getTestCaseSchema());
    System.out.println(testSuite);

    ObjectMapper objectMapper = new ObjectMapper();
    TestResponse testResponse = objectMapper.readValue(testSuite, TestResponse.class);
    problem.setTestSuite(testResponse.getTests());

    return ResponseEntity.ok(problem);
}

JSON Schema定义

private static final String TEST_CASE_SCHEMA = """
        {
          "type": "object",
          "properties": {
            "tests": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "code": { "type": "string" },
                  "expectedStdOut": { "type": "string" }
                },
                "required": ["code", "expectedStdOut"],
                "additionalProperties": false
              }
            }
          },
          "required": ["tests"],
          "additionalProperties": false
        }
        """;

public static String getTestCaseSchema() {
    return TEST_CASE_SCHEMA;
}

Spring AI调用LLM的辅助方法

public String executeOneTimeLLMCall(String systemPrompt, @Nullable String responseSchema) {
    var responseFormat = getResponseType(responseSchema);
    List<Message> history = new ArrayList<>();
    history.add(new SystemMessage(systemPrompt));

    OpenAiChatOptions options = OpenAiChatOptions.builder()
            .model(OpenAiApi.ChatModel.O3)
            .temperature(1D)
            .responseFormat(responseFormat)
            .build();

    String assistantReply = chatClient.prompt().options(options).messages(history).call().content();
    return assistantReply;
}

我的疑问

我已经指定了JSON Schema让LLM返回结构化的JSON,但拿到的字符串在反序列化成TestResponse的时候,为什么会出现“无法从字符串值反序列化为ArrayList”的错误?是不是LLM返回的字符串有隐藏的转义问题,或者Spring AI的响应格式处理哪里没配置对?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 03:10:27