Lesson 43 — Beginner
Controlling Output — Style, Length & Format
Model apna jawaab khud shape karta hai — kabhi lamba paragraph, kabhi bullet, kabhi hazaaron words ka essay. Is lesson mein hum seekhenge style, length aur format ko instructions se kaise control karein (headings, labels, tables, JSON), aur kab prompt enough nahi hota — tab structured-output/schema enforcement ki zarurat padti hai.
Style, length aur format control
WHAT
Output control matlab prompt mein clearly likhna ki jawaab kaisi ho — tone, maximum length, structure (heading, bullets, table, JSON) aur kya include/exclude karna hai. Model khud guess na kare.
WHEN
Har production use-case mein — product mein UI fix hai toh format fix hona hi chahiye. User-facing text, API payloads, reports — sab jagah output spec likho.
WHERE
Chat replies (tone + length), reports (headings + bullets), APIs (JSON keys), tables (columns) — jahan bhi downstream kuch parse ya render karta hai, wahan format control compulsory hai.
# OUTPUT SPEC — prompt ke end mein apna block likho
Style : professional, lekin Hindi/Hinglish. Slang nahi, emoji nahi.
Length : 3 line se zyada nahi.
Format : pehle ek heading, phir bullet points. Table mat use karo.
Include : order number aur expected date.
Exclude : apologies ya filler lines.
Har constraint ek measurable cheez ho — "chhota answer" vague hai, "3 line se zyada nahi" measurable hai. Model ko number do, tabhi follow hoga.
Headings, labels, table aur JSON
prompt = """Neeche diye order ke liye ek JSON object banao.
Sirf JSON, koi extra text ya explanation nahi.
Keys: order_id (string), status (one of: "packed", "shipped", "delivered"), eta_days (number)
Order: #A118 — aaj packing ho gayi, courier ko kal subah de denge."""
- Headings/bullets: "Ek heading, uske neeche 3 bullets" — structure instruction de do.
- Labels: har field ka naam fix karo ("Priority:
") — parsing mein madad milti hai. - Table: columns ka naam aur order likho — model kabhi column chhod deta hai, kabhi extra add kar deta hai.
- JSON: keys, types aur allowed values teeno batao — sirf "JSON mein do" kaafi nahi, shape bhi chahiye.
Kab prompting enough nahi hai
- Soft control (prompt): style, tone, length, headings — yeh sab prompt se theek chalta hai, kyunki "galat" ka matlab sirf inconsistent hai.
- Hard requirement (schema): JSON structure jo code parse karega — wahan prompt akela 100% guarantee nahi de sakta.
- Jab output kisi program mein jaata hai (API payload, database row, dashboard), tab structured output / JSON schema mode use karo jo providers offer karte hain — wahan model sirf schema-valid hi output de sakta hai.
- Schema support nahi hai toh fallback banao: JSON parse fail ho toh ek repair call ("tumne JSON nahi diya, sirf object do") ya retry.
# Prompt = request. Schema = guarantee. Dono alag layer hain.
resp = call_llm(prompt, response_format={"type": "json_schema", "schema": ORDER_SCHEMA})
try:
order = json.loads(resp.text)
except json.JSONDecodeError:
order = call_llm(prompt + "\nSirf valid JSON object do.", response_format=...) # repair pass
Semantic instruction vs format enforcement
Do tarah ke control hote hain: semantic ("jawaab helpful aur neutral ho", "sensitive topic pe careful") — yeh prompt ki language se hi chalte hain. Aur enforcement ("status sirf teen allowed values mein se ek ho") — yeh schema ya code check se hoti hai. Dono ko mix mat karo.
- Semantic cheezein schema mein daalne ki koshish mat karo — "helpful" ka koi type nahi hota.
- Format ko prompt mein semantic words se na maango — "acha sa likho" ki jagah exact shape likho.
- Output ke baad ek chhota validation step rakho (length check, enum check, regex) — prompt ke baad code ka net hi reliable hai.
Exercise
Question: Response 50 words ke andar chahiye — yeh kis type ka instruction hai: style, length, ya format?
Question: JSON output ki structure guarantee chahiye toh prompt ke alawa kya use karte hain? (schema / tone)
Question: Jawaab ka format batane wali line (jaise "table mein do") ya jawaab ka tone batane wali line ("professional ho") — kaunsi semantic instruction hai?
Common mistakes
- "Chhota answer do" jaisi vague length: Measurable bound do — "max 3 line", "100 words tak", "5 bullets". Vague instruction pe model apni marzi se lamba kar deta hai.
- Prompt se hi JSON guarantee expect karna: Prompt likhne se JSON aksar aata hai, lekin fenced ya extra text ke saath. Code parse karega toh schema/validation layer chahiye — prompt akela kaafi nahi.
- Format aur content ki instruction mila dena: Ek line mein aur tone, aur length, aur JSON keys likh diya — model kuch bhool jaata hai. Alag-alag labelled blocks mein likho (Style / Length / Format / Keys).
Kirane wale ko bolna "ek list banao" aur "bill ka format wahi rakhna — item, qty, rate, total ke columns" — dono alag baatein. Format instruction ke bina woh har baar alag list banayega:
VAGUE : "Items ki list banao"
-> kabhi 5 item, kabhi sab, kabhi rate ke bina
FORMAT SPEC: "4 columns: item | qty | rate | total. Total row last mein."
-> har baar same shape — bill machine padh legi
Code wahi format expect kare = model JSON expect kare. Shape fix karo.- Q: Output ka style control kaise karte hain? — Prompt mein tone/length/format ke labelled instructions; measurable bounds ke saath.
- Q: Prompt aur schema enforcement mein farq? — Prompt semantic soft control deta hai; schema programmatic guarantee deta hai code-parseable output ke liye.
- Q: Kab structured output use karna chahiye? — Jab output kisi program mein parse hota hai (API, DB); insaan ke liye prompt kaafi hai.
- Q: JSON parse fail ho toh kya karo? — Repair pass (sirf JSON maango) ya retry, plus schema mode agar provider support karta hai.
Output control ka toolkit ready. Agla lesson — Prompt Reliability & Debugging — mein seekhenge prompts ko test-set se kaise debug karte hain.