The one rule
Do not ask for “a neat answer” when your code needs data. Ask for a strict shape, then validate that shape before using it.
Structured output turns a prompt from a chat into an API contract. Your app becomes easier to test.
Why structure helps
Hackathon AI features often fail because the model returns a paragraph when your UI expects a tag, score, or list. JSON is usually the simplest format for app output.
Use structure when you need to store results, filter them, show cards, trigger actions, or compare test cases.
Design the output
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Prompt pattern
Here is a prompt for a student project that classifies hostel maintenance complaints. The app can show the category, urgency, and a short message to the warden.
You are helping a college hostel maintenance desk.
Task: Read the student's complaint and return JSON only.
Return this exact JSON shape:
{
"category": "electricity | plumbing | cleaning | internet | furniture | other",
"urgency": "low | medium | high",
"summary": "string, max 20 words",
"needsHumanReview": true
}
Rules:
- Use only one category.
- Set urgency to high only if safety, water leakage, no electricity, or blocked toilet is mentioned.
- If the complaint is unclear, use category "other" and set needsHumanReview to true.
- Return valid JSON only. No markdown. No extra text.
Complaint:
"Fan in room B-214 is sparking and smells burnt."Parse and validate
Even a good prompt is not enough. Your code must reject bad output and handle it safely.
This small JavaScript check is fine for a demo. For a larger app, use a validation library such as Zod.
const allowedCategories = ["electricity", "plumbing", "cleaning", "internet", "furniture", "other"];
const allowedUrgency = ["low", "medium", "high"];
function parseComplaintResult(modelText) {
let data;
try {
data = JSON.parse(modelText);
} catch {
return { ok: false, error: "Model did not return valid JSON" };
}
if (!allowedCategories.includes(data.category)) return { ok: false, error: "Bad category" };
if (!allowedUrgency.includes(data.urgency)) return { ok: false, error: "Bad urgency" };
if (typeof data.summary !== "string" || data.summary.length > 140) return { ok: false, error: "Bad summary" };
if (typeof data.needsHumanReview !== "boolean") return { ok: false, error: "Bad review flag" };
return { ok: true, data };
}Common mistake
Do not ask for JSON and then accept whatever comes back. Models can add notes, miss fields, or invent new labels.
Also avoid vague fields like “priority”: “urgent”. Use allowed values your UI already understands.
Make failure visible
In your demo, show what happens when validation fails. A simple fallback message is better than a broken screen.
For example: “Could not classify this complaint. Sending to manual review.” This proves you thought beyond the happy path.
Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done