archive2025

Spring AI 1.0 in a small service: ChatClient, tool calling and Ollama

Spring AI reached 1.0 on 20 May 2025. A minimal Spring Boot service that talks to a local Ollama model, returns typed results, and calls a Java method as a tool, plus notes from moving off the milestones.

I have a small internal service that summarises sensor alerts with a local model. It ran on a Spring AI milestone, and the milestones changed artifact names and APIs a few times, so I waited for GA before touching it again. Spring AI 1.0 GA was released on 20 May 2025, and this week I moved the service to 1.0.0. Below are the parts it uses.

Dependencies

Import the BOM and add the Ollama starter. With Maven:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.ai</groupId>
      <artifactId>spring-ai-bom</artifactId>
      <version>1.0.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-ollama</artifactId>
  </dependency>
</dependencies>

The starter name is the first thing that changed for me. Older milestones used spring-ai-ollama-spring-boot-starter. If your build cannot find the old artifact, this is why. The other model starters follow the same spring-ai-starter-model-* pattern.

spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=llama3.1:8b
spring.ai.ollama.chat.options.temperature=0.2

For tool calling, the model must support tools. llama3.1 and qwen2.5 do in Ollama. Some smaller or older models silently ignore the tools, and then you get an answer that sounds right but did not use your data.

ChatClient

ChatClient is the main API. Spring Boot gives you a ChatClient.Builder bean:

@RestController
class SummaryController {

    private final ChatClient chat;

    SummaryController(ChatClient.Builder builder) {
        this.chat = builder
            .defaultSystem("You summarise alerts from home sensors. Be short and factual.")
            .build();
    }

    @PostMapping("/summary")
    String summary(@RequestBody String alerts) {
        return chat.prompt()
            .user(alerts)
            .call()
            .content();
    }
}

Typed results

This is the feature I like most. Ask for a record and Spring AI adds format instructions to the prompt and converts the JSON answer:

record AlertSummary(String severity, List<String> sensors, String text) {}

AlertSummary s = chat.prompt()
    .user(u -> u.text("Summarise these alerts:\n{alerts}").param("alerts", alerts))
    .call()
    .entity(AlertSummary.class);

With an 8B local model this works most of the time, not always. Sometimes the model adds text around the JSON, or invents a severity value. I validate the result and retry once. If that fails, I return the plain text summary. I would not build anything that depends on the conversion always succeeding with a small model.

Tool calling

A tool is a Java method with @Tool. The model can decide to call it, Spring AI runs the method and sends the result back to the model.

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

@Component
class SensorTools {

    private final ReadingRepository readings;

    SensorTools(ReadingRepository readings) {
        this.readings = readings;
    }

    @Tool(description = "Latest temperature in Celsius for a sensor, and when it was measured (UTC)")
    String latestTemperature(@ToolParam(description = "Sensor name, for example 'garage'") String sensor) {
        return readings.latest(sensor)
            .map(r -> r.value() + " C at " + r.time())
            .orElse("unknown sensor: " + sensor);
    }
}
@GetMapping("/ask")
String ask(@RequestParam String q) {
    return chat.prompt()
        .user(q)
        .tools(sensorTools)
        .call()
        .content();
}

.tools(...) adds the tools for this request only. Use defaultTools(...) on the builder to add them to every request.

Some notes from testing:

  • The description texts are what the model sees. Write them for the model: units, time zone, format of the parameters.
  • Return short strings or small objects. An error message (“unknown sensor”) is better than an exception, because the model can tell the user.
  • Tools run with the service’s permissions. My tools only read. A tool that writes or switches something needs its own checks in Java, not in the prompt.
  • Log every tool call. When the answer is wrong, the log shows if the model called the tool with wrong arguments or did not call it at all.

Moving from the milestones

My list of changes, for a small service:

  1. New BOM version and new starter artifact names.
  2. Property names for Ollama stayed the same for me, but check yours against the reference docs.
  3. Old function-calling code (functions registered as beans by name) moved to the @Tool style. This was the largest change, but the new code is shorter.

The service runs on Spring Boot 3.4 and Java 21.

The 1.0 label is the reason I am now comfortable using Spring AI in something I need to maintain. Before GA, every upgrade was a small migration.