Quick revision
The short answer
We call chatClient.prompt().user(text).call().entity(InvoiceData.class). Spring AI's structured output converter derives a JSON schema from the record, adds format instructions to the prompt, and parses the model's JSON answer into an InvoiceData. Some providers can also enforce the schema natively, depending on the provider and version, but either way we validate the result, for example with Bean Validation, because the model can still return wrong values; for generic types such as a list we pass a ParameterizedTypeReference.
What to remember
In this answer, we extracted an InvoiceData record with chatClient.prompt().user(text).call().entity(InvoiceData.class). We saw that the BeanOutputConverter derives a JSON schema from the record, adds format instructions, and parses the JSON answer with Jackson, and that some providers can enforce the schema natively, depending on version. We validated the result with Bean Validation and a plausibility check, sent failures to review, and used ParameterizedTypeReference for a list of invoice lines.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We call chatClient.prompt().user(text).call().entity(InvoiceData.class). Spring AI's structured output converter derives a JSON schema from the record, adds format instructions to the prompt, and parses the model's JSON answer into an InvoiceData. Some providers can also enforce the schema natively, depending on the provider and version, but either way we validate the result, for example with Bean Validation, because the model can still return wrong values; for generic types such as a list we pass a ParameterizedTypeReference.
Why this question is asked
Extracting structured data from messy text is one of the most practical uses of a language model: invoices, emails, contracts and support tickets all contain fields that a regular expression cannot find reliably. The hard part is not getting an answer but getting a Java object we can trust. Interviewers ask this to see whether we know the Spring AI API and, more importantly, whether we validate what comes back. In this answer, we will write the extraction, look at what Spring AI does behind entity(), discuss native structured output, add validation and a plausibility check, and handle lists.
1The Starting Point
We receive invoice text, perhaps from an email or an OCR step that turned a scanned PDF into text, and we want a record we can store and process.
A record is a good target because it is a plain, immutable carrier of data, and Spring AI can derive a schema from its components. BigDecimal is the right type for money, since a double cannot represent 134.50 exactly in every calculation, and LocalDate holds a date without a time zone.
2Calling entity()
The extraction itself is one chain on the ChatClient. Instead of content(), which returns text, we call entity() with the class we want.
Notice that the system message still matters. The schema tells the model the shape of the answer, but only our instructions say which of several amounts is the total, or that 14 March should become 2026-03-14.
3What Happens Behind entity()
Behind entity() is a structured output converter, the BeanOutputConverter. It does two jobs. Before the call, it generates a JSON schema from InvoiceData, with vendor as a string, total as a number and date as a string in date format, and appends instructions to the prompt telling the model to answer with JSON that matches the schema and nothing else. After the call, it takes the response text, removes a surrounding Markdown code fence if there is one, and uses Jackson to deserialise the JSON into the record.
7 messages in order. Play them, or step through them one at a time.
The converter writes the instructions before the call and parses the answer after it.
4Native Structured Output
Format instructions are still only instructions; the model follows them with high probability, not always. Several providers offer native structured output, where the JSON schema is sent as part of the request and the provider constrains generation so that the output matches it. That makes broken or incomplete JSON very rare. Spring AI can use these modes for supported providers, through provider-specific options or a setting on the request, and the details have been changing between releases, so check the structured output documentation for your provider and Spring AI version.
But there is a catch even with native modes: they guarantee the shape, not the truth. A schema can say total is a number; it cannot make the model pick 134.50 instead of the 89.50 on the toner line.
5Validating What Comes Back
So we treat the result as untrusted input. Bean Validation annotations on the record express the basic rules, and a plausibility check catches values that are well-formed but wrong. For invoices we often have a strong check available: the line items should add up to the total.
Notice the different responses. A parse failure or a validation error sends the document to a human review queue, or triggers one retry first; an implausible amount triggers a business rule. We never store a half-parsed object, and we keep the source text with the result so a reviewer can compare them.
6Generic Types Such as Lists
A class literal cannot express List<InvoiceLine>, because Java erases generic types at runtime. For those cases entity() accepts a ParameterizedTypeReference, an anonymous subclass that keeps the full generic type so the converter can build the right schema and Jackson can deserialise the list.
7Follow-up questions
What if the invoice has no date at all?
The record should say what is allowed. If a missing date is acceptable, we make it nullable and tell the model in the system message to return null when a field is absent, instead of inventing one. Models tend to fill every field, so an explicit instruction that null is allowed reduces made-up values.
Should I use a low temperature for extraction?
Yes. Extraction has one right answer, so a temperature close to zero makes results more stable. It does not make them correct or fully repeatable, so validation stays.
Can I map onto a Map instead of a record?
Yes, entity() also accepts a ParameterizedTypeReference for Map<String, Object>, but we lose the schema's precision and the compiler's help. A record is better whenever we know the fields.
8Conclusion
In this answer, we extracted an InvoiceData record with chatClient.prompt().user(text).call().entity(InvoiceData.class). We saw that the BeanOutputConverter derives a JSON schema from the record, adds format instructions, and parses the JSON answer with Jackson, and that some providers can enforce the schema natively, depending on version. We validated the result with Bean Validation and a plausibility check, sent failures to review, and used ParameterizedTypeReference for a list of invoice lines.
Open with
I would define the target as a record, InvoiceData with a String vendor, a BigDecimal total and a LocalDate date, and call chatClient.prompt(), with a system message saying which amount counts as the total and that dates should be ISO format, then .user with the invoice text, .call() and .entity(InvoiceData.class).
Then, if they want more
Behind that, Spring AI's structured output converter generates a JSON schema from the record, appends format instructions to the prompt, and after the call strips any code fence and deserialises the JSON with Jackson into the record.
Some providers support native structured output, where the schema is sent with the request and the output is constrained to it; I would use that where my provider and Spring AI version support it, because it makes malformed JSON rare.
But neither approach guarantees correct values, so I validate the record with Bean Validation, things like NotBlank, Positive and PastOrPresent, and add a business check such as line items summing to the total. Anything that fails to parse or validate goes to a human review queue, maybe after one retry, rather than into the database.
For generic results like a list of invoice lines, I pass a ParameterizedTypeReference of List of InvoiceLine, because a class literal cannot carry the generic type.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring AI reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Using Spring AI, how would you get a typed Java object back from a model, for example...": https://scrivio-interview-library.netlify.app/spring-boot/spring-ai/1e60f25d4a/