Spring AI: ChatClient vs ChatModel with OpenAI demos

Compare Spring AI's two chat APIs, build REST endpoints with OpenAI, and inspect model metadata, token usage and three generations from one call.

Player not loading? Watch on YouTube

Dan Vega compares ChatClient and ChatModel in Spring AI and recommends starting with ChatClient for most applications. Its fluent builder API supports advisors for memory, RAG and logging, plus mapping responses to Java objects. ChatModel is the lower-level interface beneath it. Both ultimately call the same provider API, and Vega notes that ChatClient can also return a ChatResponse for access to metadata.

The walkthrough creates a Spring Boot 4.1.0 project with web and OpenAI dependencies. Vega configures an OpenAI API key through an environment variable, then builds a REST endpoint that makes a blocking request and returns text. A second controller uses ChatModel to create a Prompt and inspect the response's model name, finish reason and token usage. The demonstrated response identifies GPT5 mini as the default model in that setup.

Another example requests three slogan generations in one call with custom temperature settings. Vega initially says ChatClient cannot return multiple generations, then questions that claim and leaves retrieval through ChatClient unresolved.

For version compatibility, he pairs Spring Boot 4 with Spring AI 2.0 and points Spring Boot 3.5 users to Spring AI 1.1.8. He mentions Ollama with an open source model as an alternative, but demonstrates OpenAI rather than a local LLM setup.