chinabugotech/hutool · error · AIException

Failed to send DELETE request:

Error message

Failed to send DELETE request: 

What it means

Thrown by the private OllamaServiceImpl.sendDeleteRequest on a transport-level failure of HttpRequest.execute(). Used by deleteModel(modelName) (POST/DELETE to /api/delete). Like other send methods, hutool-http returns HTTP statuses rather than throwing, so a 404 (model not found) does NOT raise this -- only connection/socket/URL/SSL failures do. Cause is preserved.

Source

Thrown at hutool-ai/src/main/java/cn/hutool/ai/model/ollama/OllamaServiceImpl.java:260

	}

	/**
	 * 发送DELETE请求
	 *
	 * @param endpoint 请求端点
	 * @param paramJson 请求参数JSON
	 * @return 响应结果
	 */
	private HttpResponse sendDeleteRequest(String endpoint, String paramJson) {
		try {
			return HttpRequest.delete(config.getApiUrl() + endpoint)
				.header(Header.CONTENT_TYPE, "application/json")
				.header(Header.ACCEPT, "application/json")
				.body(paramJson)
				.timeout(config.getTimeout())
				.execute();
		} catch (Exception e) {
			throw new AIException("Failed to send DELETE request: " + e.getMessage(), e);
		}
	}

	// 构建copyModel请求体
	private String buildCopyModelRequestBody(final String source, final String destination) {
		Map<String, Object> requestBody = new HashMap<>();
		requestBody.put("source", source);
		requestBody.put("destination", destination);
		return JSONUtil.toJsonStr(requestBody);
	}

}

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Ensure the Ollama daemon is running and listening on config.getApiUrl() (default http://localhost:11434).
  2. Verify the port is reachable (curl http://localhost:11434/api/tags).
  3. If Ollama is in Docker, publish the port (-p 11434:11434).
  4. Inspect ex.getCause() for the real ConnectException/SocketTimeoutException.
  5. For 'model not found', check the returned HttpResponse status instead of expecting this exception.

Example fix

// before
AIConfig cfg = new AIConfigBuilder(ModelName.OLLAMA.getValue()).build();
ollama.deleteModel("llama3"); // Ollama not running -> Failed to send DELETE request

// after
AIConfig cfg = new AIConfigBuilder(ModelName.OLLAMA.getValue())
    .setApiUrl("http://localhost:11434")
    .setTimeout(10_000)
    .build();
HttpResponse r = ...; // verify /api/tags reachable first
ollama.deleteModel("llama3");
Defensive patterns

Strategy: validation

Validate before calling

// Verify Ollama is up before calling deleteModel
String base = config.getApiUrl(); // expect http://localhost:11434
try (java.net.Socket s = new java.net.Socket()) {
    java.net.URL u = new java.net.URL(base);
    s.connect(new java.net.InetSocketAddress(u.getHost(), u.getPort() < 0 ? 80 : u.getPort()), 2000);
} catch (Exception ex) {
    throw new IllegalStateException("Ollama unreachable at " + base, ex);
}

Try / catch

try {
    return ollama.deleteModel(name);
} catch (AIException e) {
    if (e.getMessage().startsWith("Failed to send DELETE request")) {
        Throwable root = e.getCause(); // ConnectException if not running
        // ensure Ollama started; model-not-found returns HTTP 404, not this
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling ollama.deleteModel(name) when the Ollama server is not running, config.getApiUrl() is wrong (default http://localhost:11434), the host/port is unreachable, or the connection times out. A nonexistent model returns HTTP 404 via the response, not this exception.

Common situations: Ollama not started locally; wrong host/port (custom Ollama bind); firewall blocking localhost port; Docker container Ollama not exposed; apiUrl typo; remote Ollama unreachable.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/61085d4b90cd7058. Report an issue: GitHub.