0PricingLogin
Spring Boot 4 Complete Guide · Lesson

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 ChatClient directly — 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.2

All lessons in this course

  1. ChatClient, Prompts, and Structured Output
  2. Embeddings and Vector Store Retrieval
  3. Retrieval-Augmented Generation Pipelines
  4. Tool Calling and Agent Advisors
← Back to Spring Boot 4 Complete Guide