ChatClient, Prompts, and Structured Output
Call chat models through ChatClient with prompt templates and typed structured-output mapping.
Why ChatClient?
Spring AI gives you a fluent, high-level API for talking to LLMs: the ChatClient. Instead of hand-building HTTP requests to OpenAI, Anthropic, or Ollama, you describe what you want and let the framework handle transport, retries, and message assembly.
- ChatClient — fluent builder for one-shot or streaming calls.
- Prompt — a list of messages (system, user, assistant) plus options.
- Structured output — map the model's text reply directly into a typed Java object.
It is portable: swap the underlying ChatModel (OpenAI → Anthropic) and your ChatClient code stays the same.
Auto-configuration and dependencies
Add a Spring AI model starter and Spring Boot auto-configures a ChatModel bean plus a ChatClient.Builder you can inject.
- The starter (e.g.
spring-ai-starter-model-openai) reads your API key and model from properties. - You never construct
ChatClientdirectly — you inject the builder and call.build().
Typical configuration in application.yml:
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4o
temperature: 0.2All lessons in this course
- ChatClient, Prompts, and Structured Output
- Embeddings and Vector Store Retrieval
- Retrieval-Augmented Generation Pipelines
- Tool Calling and Agent Advisors