Spring Boot Starter for OpenAI integration providing auto-configuration for chat completion, embeddings, image generation, audio speech synthesis, audio transcription, and content moderation models. Includes high-level ChatClient API and conversation memory support.
Spring Boot Starter for OpenAI integration providing auto-configuration for chat completion, embeddings, image generation, audio speech synthesis, audio transcription, and content moderation models. Includes high-level ChatClient API and conversation memory support.
| Resource | Description |
|---|---|
| Quick Start Guide | Get started in 5 minutes |
| Real-World Scenarios | Production-ready examples |
| Configuration Reference | All configuration options |
| API Reference | Detailed API documentation |
org.springframework.ai:spring-ai-starter-model-openaiMaven:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
<version>1.1.2</version>
</dependency>Gradle:
implementation 'org.springframework.ai:spring-ai-starter-model-openai:1.1.2'| Model | Description | Bean Class |
|---|---|---|
| Chat | Conversational AI with streaming | OpenAiChatModel |
| Embeddings | Text vectorization | OpenAiEmbeddingModel |
| Images | DALL-E image generation | OpenAiImageModel |
| Speech | Text-to-speech synthesis | OpenAiAudioSpeechModel |
| Transcription | Speech-to-text (Whisper) | OpenAiAudioTranscriptionModel |
| Moderation | Content safety | OpenAiModerationModel |
| API | Description | Documentation |
|---|---|---|
| ChatClient | Fluent chat interface | Details |
| ChatMemory | Conversation history | Details |
spring.ai.openai.api-key=sk-...@Service
public class ChatService {
private final ChatClient chatClient;
public ChatService(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
public String chat(String message) {
return chatClient.prompt()
.user(message)
.call()
.content();
}
}The starter automatically configures beans when dependencies are present:
spring.ai.openai.*Disable specific models:
spring.ai.chat-model=none
spring.ai.embedding-model=noneAll models follow consistent patterns:
Synchronous:
ChatResponse response = chatModel.call(prompt);Streaming:
Flux<ChatResponse> stream = chatModel.stream(prompt);With Options:
Prompt prompt = new Prompt(message, options);
ChatResponse response = chatModel.call(prompt);package org.springframework.ai.chat.messages;
public interface Message {
String getContent();
MessageType getMessageType();
Map<String, Object> getMetadata();
}
public class UserMessage implements Message { }
public class SystemMessage implements Message { }
public class AssistantMessage implements Message { }package org.springframework.ai.chat.model;
public class ChatResponse {
public Result getResult();
public List<Result> getResults();
public ChatResponseMetadata getMetadata();
}
public class Result {
public AssistantMessage getOutput();
public ChatGenerationMetadata getMetadata();
}spring.ai.openai.chat.model=gpt-4o-mini
spring.ai.openai.chat.temperature=0.7
spring.ai.openai.chat.max-tokens=1000Available Models: gpt-4o, gpt-4o-mini, gpt-4-turbo, o1, o3, gpt-3.5-turbo
spring.ai.openai.embedding.model=text-embedding-ada-002
spring.ai.openai.embedding.dimensions=1536Models: text-embedding-ada-002, text-embedding-3-small, text-embedding-3-large
spring.ai.openai.image.model=dall-e-3
spring.ai.openai.image.quality=hd
spring.ai.openai.image.size=1024x1024Models: dall-e-2, dall-e-3
| Use Case | Models | Guide |
|---|---|---|
| Chatbot | Chat + Memory | Example |
| Document Q&A | Chat + Embeddings | Example |
| Content Moderation | Moderation | Example |
| Image Generation | Images | Example |
| Translation | Chat | Example |
| Transcription | Transcription + Chat | Example |
| Code Review | Chat | Example |
| Streaming UI | Chat (streaming) | Example |
| Tool Calling | Chat + Functions | Example |
| Data Extraction | Chat | Example |
All models throw OpenAiApiException:
import org.springframework.ai.openai.api.OpenAiApiException;
try {
ChatResponse response = chatModel.call(prompt);
} catch (OpenAiApiException e) {
// Handle: 401 (auth), 429 (rate limit), 400 (invalid), 500+ (server)
}Retry: Automatic exponential backoff for transient errors (429, 500+)
Enable metrics and tracing:
spring.ai.chat.client.observations.log-prompt=true
spring.ai.chat.client.observations.log-completion=trueIntegrates with Micrometer for:
| Document | Description |
|---|---|
| Chat Model | OpenAiChatModel API, options, streaming |
| ChatClient | High-level fluent API, advisors |
| Chat Memory | Conversation history, repositories |
| Document | Description |
|---|---|
| Embedding Model | Vector embeddings, similarity search |
| Image Model | DALL-E image generation |
| Audio Speech | Text-to-speech synthesis |
| Audio Transcription | Speech-to-text (Whisper) |
| Moderation | Content safety and filtering |
| Document | Description |
|---|---|
| Configuration | Complete property reference |
spring-ai-openai on classpath// Low-level API clients
OpenAiApi // Chat & embeddings
OpenAiImageApi // Image generation
OpenAiAudioApi // Speech & transcription
OpenAiModerationApi // Content moderation
// Model implementations
OpenAiChatModel // Singleton, thread-safe
OpenAiEmbeddingModel // Singleton, thread-safe
OpenAiImageModel // Singleton, thread-safe
OpenAiAudioSpeechModel // Singleton, thread-safe
OpenAiAudioTranscriptionModel // Singleton, thread-safe
OpenAiModerationModel // Singleton, thread-safe
// High-level APIs
ChatClient.Builder // Prototype scope
ChatMemory // Singleton (with proper repository)All model beans are thread-safe and can be used concurrently.
maxTokens to control costsOpenAiApiException and retry transient errorstessl i tessl/maven-org-springframework-ai--spring-ai-starter-model-openai@1.1.1