Spring AI Chat Client provides a fluent API for building AI-powered applications with LLMs, supporting advisors, streaming, structured outputs, and conversation memory
Get started with Spring AI Chat Client in minutes.
Add the dependency to your pom.xml:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-client-chat</artifactId>
<version>1.1.2</version>
</dependency>Important: Spring AI artifacts require special repository configuration:
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
// Obtain ChatModel from Spring context or create manually
ChatModel chatModel = // ... your ChatModel implementation
// Create ChatClient
ChatClient chatClient = ChatClient.create(chatModel);String response = chatClient
.prompt("What is Spring Framework?")
.call()
.content();
System.out.println(response);record Summary(String title, String content, List<String> tags) {}
Summary summary = chatClient
.prompt("Summarize: Spring Framework is a comprehensive framework...")
.call()
.entity(Summary.class);
System.out.println("Title: " + summary.title());import org.springframework.ai.chat.options.ChatOptionsBuilder;
ChatClient client = ChatClient.builder(chatModel)
.defaultSystem("You are a helpful Java programming assistant")
.defaultOptions(ChatOptionsBuilder.builder()
.withTemperature(0.7)
.withMaxTokens(1000)
.build())
.build();import reactor.core.publisher.Flux;
Flux<String> stream = chatClient
.prompt("Tell me a story")
.stream()
.content();
// Print as content arrives
stream.subscribe(System.out::print);With Spring Boot auto-configuration:
@Configuration
class ChatConfig {
@Bean
ChatClient chatClient(ChatClient.Builder builder) {
return builder
.defaultSystem("You are helpful")
.build();
}
}
@RestController
class ChatController {
private final ChatClient chatClient;
ChatController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@PostMapping("/chat")
String chat(@RequestBody String message) {
return chatClient.prompt(message).call().content();
}
}try {
String response = chatClient.prompt("Query").call().content();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}String content = chatClient.prompt("Query").call().content();
String safe = (content != null) ? content : "No response";// GOOD: Create once, reuse many times
private final ChatClient chatClient = ChatClient.create(chatModel);
public String handleRequest(String query) {
return chatClient.prompt(query).call().content();
}Problem: NoSuchBeanDefinitionException for ChatModel
Problem: Repository not found errors
Problem: Null response content
See Reference Documentation for complete API details.