spring-projects/spring-ai · warning

Failed to parse tool arguments JSON: + argumentsJson

Error message

Failed to parse tool arguments JSON: + argumentsJson

What it means

A warning logged when building an Anthropic tool_use input from a tool call whose arguments JSON cannot be parsed or converted to JsonValue. The method returns the (possibly empty/partial) inputBuilder anyway, so the tool call may be sent with missing arguments. It indicates malformed argumentsJson coming from the model or from a previous round-trip.

Source

Thrown at models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/AnthropicChatModel.java:1250

	 * string and creates the proper SDK input format.
	 * @param argumentsJson the JSON string containing tool call arguments
	 * @return a ToolUseBlockParam.Input with the parsed arguments
	 */
	private ToolUseBlockParam.Input buildToolInput(String argumentsJson) {
		ToolUseBlockParam.Input.Builder inputBuilder = ToolUseBlockParam.Input.builder();
		if (argumentsJson != null && !argumentsJson.isEmpty()) {
			try {
				var jsonMapper = tools.jackson.databind.json.JsonMapper.builder().build();
				java.util.Map<String, Object> arguments = jsonMapper.readValue(argumentsJson,
						new tools.jackson.core.type.TypeReference<java.util.Map<String, Object>>() {
						});
				for (java.util.Map.Entry<String, Object> entry : arguments.entrySet()) {
					inputBuilder.putAdditionalProperty(entry.getKey(), JsonValue.from(entry.getValue()));
				}
			}
			catch (Exception e) {
				if (logger.isWarnEnabled()) {
					logger.warn("Failed to parse tool arguments JSON: " + argumentsJson, e);
				}
			}
		}
		return inputBuilder.build();
	}

	/**
	 * Converts a Spring AI {@link ToolDefinition} to an Anthropic SDK {@link Tool}.
	 * <p>
	 * Spring AI provides the input schema as a JSON string, but the SDK expects a
	 * structured {@code Tool.InputSchema} built via the builder pattern.
	 * <p>
	 * Conversion: parses the JSON schema to a Map, extracts "properties" (added via
	 * {@code putAdditionalProperty()}), extracts "required" fields (added via
	 * {@code addRequired()}), then builds the Tool with name, description, and schema.
	 * @param toolDefinition the tool definition with name, description, and JSON schema
	 * @return the Anthropic SDK Tool
	 * @throws RuntimeException if the JSON schema cannot be parsed

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Validate/repair the tool arguments JSON before returning the ToolCall from your tool callback chain
  2. Log and inspect the raw argumentsJson string that fails to parse
  3. Ensure your function-calling response handling stores arguments as canonical JSON
  4. Upgrade spring-ai-anthropic for more tolerant argument parsing
  5. If you control the model prompt, constrain the tool schema so arguments are always valid JSON

Example fix

// before
String argumentsJson = toolCall.arguments(); // may be malformed
// after
String argumentsJson = toolCall.arguments();
try {
    JsonValue.from(argumentsJson); // validate early
}
catch (Exception e) {
    argumentsJson = "{}"; // or repair/default before sending
}
Defensive patterns

Strategy: validation

Validate before calling

boolean isValidToolArgsJson(String args) {
    if (args == null || args.isBlank()) return false;
    try { JsonValue.from(args); return true; }
    catch (Exception e) { return false; }
}

Try / catch

try {
    chatModel.call(new Prompt(prompt, toolOptions));
}
catch (Exception e) {
    logger.warn("Tool-call round trip failed, possibly malformed arguments", e);
    // retry with repaired/default arguments or without the tool result
}

Prevention

When it happens

Trigger: Converting a Spring AI ToolCall whose arguments() string is not valid JSON, or whose parsed values cannot be converted by JsonValue.from (e.g. exotic nested types) while reconstructing a follow-up Anthropic request with tool results.

Common situations: Model emitted truncated or non-JSON tool arguments; custom ToolCallingChatOptions implementations producing hand-built argument strings; echoing back tool calls whose arguments were serialized by a different schema; double-encoded JSON strings.

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 spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/45bc98c81c17ea9c. Report an issue: GitHub.