Skip to content
Ameen Abozaid
Planned lessonJNR-056-L03

Structured output to records, and validating it

  • Lesson 3 of 7
  • Junior
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

java
record Description(@NotBlank String title, @Size(max = 300) String text) {}
Description d = chatClient.prompt().user(...).call().entity(Description.class);
validator.validate(d); // reject if violations

Check 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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.