alibaba/spring-ai-alibaba · error · IllegalArgumentException
Failed to parse result
Error message
Failed to parse result
What it means
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.
Source
Thrown at spring-boot-starters/spring-ai-alibaba-starter-builtin-nodes/src/main/java/com/alibaba/cloud/ai/graph/node/code/TemplateTransformer.java:65
public Map<String, Object> transformResponse(String response) throws Exception {
String resultStr = extractResultStrFromResponse(response);
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(resultStr,
mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class));
}
public abstract String getRunnerScript(CodeStyle style);
private String extractResultStrFromResponse(String response) {
Pattern pattern = Pattern.compile(RESULT_TAG + "(.*?)" + RESULT_TAG, Pattern.DOTALL);
Matcher matcher = pattern.matcher(response);
if (matcher.find()) {
return matcher.group(1).trim();
}
else {
throw new IllegalArgumentException("Failed to parse result");
}
}
private String serializeInputs(Map<String, Object> inputs) throws Exception {
ObjectMapper mapper = new ObjectMapper();
String inputsJsonStr = mapper.writeValueAsString(inputs);
return Base64.getEncoder().encodeToString(inputsJsonStr.getBytes(StandardCharsets.UTF_8));
}
private String assembleRunnerScript(String code, Map<String, Object> inputs, CodeStyle style) throws Exception {
String script = getRunnerScript(style);
script = script.replace(CODE_PLACEHOLDER, code);
script = script.replace(INPUTS_PLACEHOLDER, serializeInputs(inputs));
return script;
}
private String getPreloadScript() {
return "";View on GitHub (pinned to f82da0b50f)
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).
Example fix
// before
String result = transformer.extractResultStrFromResponse(response); // IllegalArgumentException
// after
try {
result = transformer.extractResultStrFromResponse(response);
} catch (IllegalArgumentException e) {
log.warn("Template parse failed, using raw response");
result = response.trim();
} Defensive patterns
Strategy: try-catch
Validate before calling
Pattern p = Pattern.compile(templateRegex);
boolean parseable = p.matcher(response).find();
if (!parseable) log.warn("Response missing expected template marker, length={}", response.length()); Type guard
boolean hasTemplateMarker(String response, Pattern pattern) {
return response != null && pattern.matcher(response).find();
} Try / catch
try {
return transformer.extractResultStrFromResponse(response);
} catch (IllegalArgumentException e) {
log.warn("Result template parse failed; raw response: {}", response);
return response.trim(); // fallback
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- ChatClient successfully returned, but the returned json is i
- INVALID_PARAMS
- Invalid data format
- regex cannot be empty
- Invalid memory parameter
AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09).
Data as JSON: /api/errors/d1e0bec430c438f3.
Report an issue: GitHub.