alibaba/spring-ai-alibaba · error · RuntimeException

JSON processing failed:

Error message

JSON processing failed: 

What it means

Wraps a Jackson JsonProcessingException thrown while parsing the tool input or schema JSON in the tool-call validator in PromptRunServiceImpl. It means the input arguments (or the schema itself) are not syntactically valid JSON, so validation never ran.

Source

Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/service/impl/PromptRunServiceImpl.java:255

        
        @Override
        public String apply(Map<String, Object> inputMap) {
            try {
                JsonNode schemaNode = objectMapper.readTree(inputSchema);
                JsonNode dataNode = objectMapper.valueToTree(inputMap);
                
                JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
                JsonSchema schema = factory.getSchema(schemaNode);
                Set<ValidationMessage> errors = schema.validate(dataNode);
                
                if (!errors.isEmpty()) {
                    throw new IllegalArgumentException("Tool Calls Invalid input data: " + errors);
                }
                return this.output;
                
            } catch (JsonProcessingException e) {
                log.error("JSON 处理失败: ", e);
                throw new RuntimeException("JSON processing failed: " + e.getMessage(), e);
            } catch (Exception e) {
                log.error("JSON 处理失败: ", e);
                throw new RuntimeException("Schema validation failed: " + e.getMessage(), e);
            }
        }
        
    }
    
}

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Inspect the nested JsonProcessingException message — Jackson reports line/column and the offending token.
  2. Print/log the raw input string before parsing and validate it with a JSON linter.
  3. Ensure the caller passes a parsed JSON object or a syntactically valid JSON string.
  4. If template substitution builds the JSON, escape values or build the tree with ObjectMapper instead of string concatenation.

Example fix

// before: string-concatenated JSON
String json = "{\"q\": " + userInput + "}";
// after: build with Jackson
ObjectNode node = mapper.createObjectNode();
node.put("q", userInput);
Defensive patterns

Strategy: validation

Validate before calling

try { mapper.readTree(inputJson); } catch (JsonProcessingException e) { /* invalid JSON */ }

Type guard

boolean isJsonParsable(String s) {
    try { mapper.readTree(s); return true; }
    catch (JsonProcessingException e) { return false; }
}

Try / catch

catch (JsonProcessingException e) {
    log.error("Invalid JSON in tool input at {}:{}", e.getLocation() != null ? e.getLocation().getLineNr() : -1, e.getLocation() != null ? e.getLocation().getColumnNr() : -1, e);
    throw new RuntimeException("JSON processing failed: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: apply() calls the JSON parser (ObjectMapper readTree or similar) on the tool input string or schema node and Jackson fails to parse malformed input.

Common situations: LLM emitted truncated/quoted-escaped JSON in the tool call; template variable substitution injected raw text into the JSON; invisible characters or single quotes used instead of double quotes.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/62e954e64d0218f0. Report an issue: GitHub.