{"record":{"id":"d1e0bec430c438f3","repo":"alibaba/spring-ai-alibaba","slug":"failed-to-parse-result","errorCode":null,"errorMessage":"Failed to parse result","messagePattern":"Failed to parse result","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-boot-starters/spring-ai-alibaba-starter-builtin-nodes/src/main/java/com/alibaba/cloud/ai/graph/node/code/TemplateTransformer.java","lineNumber":65,"sourceCode":"\n\tpublic Map<String, Object> transformResponse(String response) throws Exception {\n\t\tString resultStr = extractResultStrFromResponse(response);\n\t\tObjectMapper mapper = new ObjectMapper();\n\t\treturn mapper.readValue(resultStr,\n\t\t\t\tmapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class));\n\t}\n\n\tpublic abstract String getRunnerScript(CodeStyle style);\n\n\tprivate String extractResultStrFromResponse(String response) {\n\t\tPattern pattern = Pattern.compile(RESULT_TAG + \"(.*?)\" + RESULT_TAG, Pattern.DOTALL);\n\t\tMatcher matcher = pattern.matcher(response);\n\n\t\tif (matcher.find()) {\n\t\t\treturn matcher.group(1).trim();\n\t\t}\n\t\telse {\n\t\t\tthrow new IllegalArgumentException(\"Failed to parse result\");\n\t\t}\n\t}\n\n\tprivate String serializeInputs(Map<String, Object> inputs) throws Exception {\n\t\tObjectMapper mapper = new ObjectMapper();\n\t\tString inputsJsonStr = mapper.writeValueAsString(inputs);\n\t\treturn Base64.getEncoder().encodeToString(inputsJsonStr.getBytes(StandardCharsets.UTF_8));\n\t}\n\n\tprivate String assembleRunnerScript(String code, Map<String, Object> inputs, CodeStyle style) throws Exception {\n\t\tString script = getRunnerScript(style);\n\t\tscript = script.replace(CODE_PLACEHOLDER, code);\n\t\tscript = script.replace(INPUTS_PLACEHOLDER, serializeInputs(inputs));\n\t\treturn script;\n\t}\n\n\tprivate String getPreloadScript() {\n\t\treturn \"\";","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-boot-starters/spring-ai-alibaba-starter-builtin-nodes/src/main/java/com/alibaba/cloud/ai/graph/node/code/TemplateTransformer.java#L47-L83","documentation":"TemplateTransformer.extractResultStrFromResponse applies a regex to the model response and throws IllegalArgumentException('Failed to parse result') when no match is found. It expects the LLM output to contain a specific template marker/structure (e.g. fenced or tagged result section) and the response deviated from it.","triggerScenarios":"Calling extractResultStrFromResponse with a response that lacks the expected template pattern — model returned plain text, refused, output in a different format, or truncated the marker.","commonSituations":"Prompt drift causing the model to skip the required delimiters; model answering in a different language/style; temperature too high producing malformed output; response truncated by max_tokens cutting off the closing marker.","solutions":["Log the raw response to see how it deviates from the expected template.","Strengthen the prompt to require the exact output format with delimiters.","Lower temperature or increase max_tokens so the response is deterministic and complete.","Catch IllegalArgumentException and add a fallback parse (e.g. treat whole response as result)."],"exampleFix":"// before\nString result = transformer.extractResultStrFromResponse(response); // IllegalArgumentException\n// after\ntry {\n    result = transformer.extractResultStrFromResponse(response);\n} catch (IllegalArgumentException e) {\n    log.warn(\"Template parse failed, using raw response\");\n    result = response.trim();\n}","handlingStrategy":"try-catch","validationCode":"Pattern p = Pattern.compile(templateRegex);\nboolean parseable = p.matcher(response).find();\nif (!parseable) log.warn(\"Response missing expected template marker, length={}\", response.length());","typeGuard":"boolean hasTemplateMarker(String response, Pattern pattern) {\n    return response != null && pattern.matcher(response).find();\n}","tryCatchPattern":"try {\n    return transformer.extractResultStrFromResponse(response);\n} catch (IllegalArgumentException e) {\n    log.warn(\"Result template parse failed; raw response: {}\", response);\n    return response.trim(); // fallback\n}","preventionTips":["Pin the output format in the prompt with explicit delimiters and an example","Set temperature low (0-0.2) for format-sensitive extraction","Raise max_tokens so responses are never truncated mid-marker","Log raw responses to detect format drift early"],"tags":["regex","parsing","llm-output","template"],"backgroundTag":"unexpected-response-shape","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}