Structured output to records, and validating it
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
get typed data instead of free text.
Planned topics
- Parsing free text with string tricks breaks easily. Ask for structured output instead.
- Spring AI can map the reply into a Java record with
.entity(MyRecord.class); it adds format instructions to the prompt. - The model may still return wrong or missing values. Treat the result as untrusted input.
- Validate with Bean Validation (
@NotBlank,@Size) before use. - On failure, return a clear error or a safe fallback; never pass bad output further.
Draft outline details
Demonstration
record Description(@NotBlank String title, @Size(max = 300) String text) {}
Description d = chatClient.prompt().user(...).call().entity(Description.class);
validator.validate(d); // reject if violationsCheck your understanding
Why validate model output?
Show answer
It is not guaranteed to follow your format or rules.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.