Transform AI responses into type-safe Java objects effortlessly with Spring AI's structured output capabilities. This project demonstrates how to convert natural language responses from Large Language Models (LLMs) into strongly-typed Java objects without writing complex parsing logic.
Working with AI models typically means dealing with unstructured text responses. Converting these responses into usable data structures traditionally requires:
- Writing custom parsing logic
- Handling edge cases and malformed responses
- Maintaining complex regex patterns or string manipulation code
- Implementing error handling for parsing failures
Spring AI eliminates these challenges by providing built-in conversion capabilities that transform AI responses directly into Java objects. This project showcases this functionality through a practical example: converting information about technical authors into structured data.
This application demonstrates structured output conversion by:
- Taking user input for a technical author's name
- Querying an AI model for information about the author
- Automatically converting the response into a strongly-typed
AuthorInfo
object - Displaying the structured information to the user
- Java 17 or higher
- OpenAI API key
- Spring Boot 3.2 or higher
- Spring AI dependencies
src/
├── main/
│ ├── java/
│ │ └── io/spring/examples/
│ │ ├── Application.java # Main application class
│ │ └── AuthorInfo.java # Data structure for author information
│ └── resources/
│ └── application.properties # Configuration properties
The AuthorInfo
record defines our structured output format:
public record AuthorInfo(
String name,
String primaryGenre,
List<String> notableBooks,
int yearsActive,
String specialization
) {}
Spring AI handles the conversion from AI response to Java object automatically:
var authorInfo = chat.prompt()
.user(u -> {
u.text("Please give me information about ${author}");
u.param("author", authorName);
})
.call()
.entity(AuthorInfo.class); // Automatic conversion to AuthorInfo
-
Set your OpenAI API key:
export OPENAI_API_KEY=your-api-key-here
-
Run the application:
./mvnw spring-boot:run
-
Enter author names when prompted:
Who is your favorite technical author? (e.g., 'Rod Johnson', or type 'exit' to quit) USER: Rod Johnson
The application displays structured information about the author:
=== Author Analysis ===
Name: Rod Johnson
Primary Genre: Technical Writing
Notable Books: Expert One-on-One J2EE Design and Development, Expert One-on-One J2EE Development without EJB
Years Active: 20
Specialization: Enterprise Java Development and Spring Framework
The application uses Spring Boot's configuration properties:
spring.application.name=structured-output
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.options.model=gpt-4
Spring AI's structured output converter handles various edge cases:
- Malformed AI responses
- Missing fields in the response
- Type conversion errors
- Type Safety: Get compile-time checking for your AI response structures
- Clean Code: Eliminate boilerplate parsing code
- Reliability: Robust handling of edge cases and malformed responses
- Maintainability: Changes to response structure only require updating the data model
- Integration: Seamless integration with Spring's ecosystem
For more information about Spring AI and structured output conversion, visit:
Built with ❤️ using Spring AI