FreeFoundations ยท 20 min ยท Beginner
๐Ÿ“ฆ

Structured Output

Map LLM responses directly to Java POJOs

๐Ÿ’ก

What Is Structured Output?

Simple overview ยท Real-world examples included

AI normally returns plain text like "The vendor is Acme Corp and the total is $1,234.56". But for real apps, you need that as a proper data structure with separate fields you can save to a database. This topic shows you how to tell the AI "return this exact JSON format" and have Spring AI automatically convert it into a typed Java record. No manual text parsing required. Real example: automatically extract vendor, amount, date and line items from invoice emails.

๐ŸŽฏ What You'll Learn

โ†’Spring AI BeanOutputConverter
โ†’Designing output schemas
โ†’Java records for AI output
โ†’Validation and error handling

๐ŸŒ Real-World Use Case

An accounts payable system that automatically extracts vendor, amount, date from emailed invoices

๐Ÿ“Š How It Works โ€” Interactive Flowchart

Hover nodes for quick tip ยท Click to open detail ยท Press "Run Data Flow" to animate

Click any box to learn what it does
๐Ÿง‘โ€๐Ÿ’ป
Raw Text
Invoice email
Step 1
โš™๏ธ
Spring Boot
Your server
Step 2
๐ŸŒ
AI API
With schema
Step 3
๐Ÿค–
AI Model
Returns JSON
Step 4
โœ…
Java Record
Typed object
Step 5

๐Ÿ› ๏ธ Step-by-Step Implementation

Click any step to expand ยท Check the circle to mark complete ยท Progress saves in your browser

Implementation ProgressClick โ—‹ to mark a step complete
0 / 6
01
Define output record
โ–พ
๐Ÿ’ก WHY AM I DOING THIS?

Java records are perfect for AI output โ€” immutable, concise, auto-generates equals/hashCode.

โ–ถ WHAT HAPPENS IN THIS STEP

Define exactly what fields you want extracted.

๐Ÿ“„ FILES TO CREATE / MODIFY
+InvoiceRecord.java
๐Ÿ“ FOLDER STRUCTURE
model/InvoiceRecord.java
๐Ÿ’ป CODE EXAMPLE
public record InvoiceRecord(
    String vendor,
    String amount,
    String currency,
    String date,
    String invoiceNumber,
    List<String> lineItems
) {}
โš  COMMON MISTAKES
โš Using class instead of record โ€” more boilerplate for no benefit
โ–ถ HOW TO TEST
Compile check: record should compile without explicit constructor
โœ… EXPECTED RESULT
Record compiles cleanly with all fields accessible via accessor methods
02
Create BeanOutputConverter
The converter generates the JSON schema from your record and tells the LLM exactly what structure to return
โ–พ
03
Add validation
LLMs sometimes hallucinate fields
โ–พ
04
Build extraction endpoint
Expose the parser as an API so any system can submit invoice text and get structured data
โ–พ
05
Handle conversion failures
LLMs occasionally return malformed JSON
โ–พ
06
Test with real invoices
Real invoices have quirky formats โ€” PDFs converted to text, tables, foreign currencies
โ–พ

โœ๏ธ How to Write the Prompt for This Topic

System: "Extract invoice data. Reply ONLY with valid JSON matching this schema: {schema}. No explanation, no markdown, just JSON."

๐ŸŽฏ

Job Interview Prep

8 curated questions ยท 0 / 8 mastered
Click answers, then rate yourself to track progress
LEVEL:

๐Ÿ’ก Don't memorise answers word-for-word. Understand the concept, then explain it in your own words using a real example from this topic's project โ€” that's what impresses interviewers.

โšก Live AI Demo

LIVE AI DEMO โ€” CLAUDE HAIKUSign up for 3 free demos/day
0/500

๐Ÿ“ฆ Clone & Run the Sample Project

๐Ÿ“ฆ
github.com / ai-dev-academy / ai-dev-academy-projects

04-structured-output

Invoice Parser โ€” extracts structured fields from invoice text

Spring Boot 3.xSpring AIJacksonJava Records
โšกQUICK START โ€” RUN IN 5 STEPS
1
Clone the full repo
git clone https://github.com/ai-dev-academy/ai-dev-academy-projects.git
2
Enter the topic folder
cd ai-dev-academy-projects/04-structured-output
3
Set up your API key
cp .env.example .env
# Then open .env and add your API key
4
Run the project
mvn spring-boot:run
5
Test the endpoint
curl -X POST http://localhost:8080/ai/chat \
  -H "Content-Type: application/json" \
  -d '"Hello, what is Structured Output?"'
OPEN IN YOUR IDE
โ† DashboardAll Topics โ†’