Validate generated data at the boundary | Introduction to Generative AI
Understand how generative AI works, write source-bounded prompts, validate outputs, evaluate grounded answers and design a responsible career-learning assistant.
Prerequisites: No prior AI experience required. Basic familiarity with reading short code examples is helpful; no paid API account is needed.
Validate generated data at the boundary
Applications often need a predictable data shape rather than free-form prose. For example, a course helper might return a summary and an array of topics. A prompt can request JSON, but a request for JSON is not itself a guarantee that every response will be valid JSON or satisfy your business rules. Some providers offer schema-constrained generation; check their current contract and still validate the response in your application.
Validation has several layers. Syntax validation checks whether a response can be parsed. Shape validation checks required keys and value types. Business validation checks meaning: a suggested skill must be supported by the candidate source, and a requested tool action must be allowed for this user. A syntactically valid object can still contain false or inappropriate claims. Keep evidence checks separate from format checks.
Fail safely when validation fails. Return a clear message or a deterministic fallback rather than silently saving malformed data. Do not use eval or execute a generated code fragment to parse a response. Treat unknown fields conservatively, set size limits and avoid logging personal resume content. The JavaScript example below illustrates only basic shape checks; production validation should use a reviewed schema and enforce the application-specific rules.
Worked example
function validateSummary(value) {
if (!value || typeof value.summary !== "string") {
throw new Error("Missing summary");
}
if (!Array.isArray(value.topics) ||
value.topics.some(item => typeof item !== "string")) {
throw new Error("Topics must be strings");
}
return value;
}
const data = JSON.parse(responseText);
validateSummary(data);
Try it yourself
Define a response with summary and topics. Write three invalid examples: broken JSON, wrong value types and a valid-shaped object containing an unsupported factual claim. Explain which check catches each one.
Check your understanding: explain why the example works, state one limitation, and record how you would test it.
1. Understand generative AI
2. Give clear instructions
3. Ground and evaluate answers
4. Build a responsible learning prototype