androidengineers.Book a session

Structured output and validation

Structured output and validation

articleSelf-paced

Turn model text into data without confusing a schema with truth.

1. Schema design

Use a small explicit schema with required fields, bounded arrays and enumerations. Describe the meaning of missing or unknown data. Avoid returning unrestricted HTML that the UI directly trusts.

2. Parsing and domain validation

First parse the response, then apply business rules such as date ordering and supported currency. A schema-valid itinerary can still contain impossible travel dates.

3. Repair policy

Allow at most a bounded repair attempt for recoverable format failures. Keep the original input and failure category for debugging. Offer manual editing when repeated repair fails.

Worked scenario

The returned arrival date precedes departure. Parsing succeeds, but domain validation rejects the itinerary and asks the user to correct the dates.

Apply it

Define an itinerary data class and validation function. Test unknown fields, missing dates, malformed JSON and impossible dates.

Check your understanding

Typed output is never persisted before domain validation succeeds. Explain the decision and show evidence from your implementation or design. If you cannot demonstrate it yet, revisit the relevant section before continuing.

YOUR LEARNING JOURNEY

0 of 13 available lessons completed

Progress saved in this browser. No account needed.
Structured output and validation | Gemini API for Android | Android Engineers