What is Format Constraint?
Format Constraint is a prompting technique where we force AI to provide output in a specific structure or format.
Common formats include:
- JSON
- CSV
- Markdown
- Tables
- Bullet Points
- tabular format
- XML
- Code Blocks
Example 1
The Instruction:
“Act as a data extraction API. Your job is to extract customer complaint data from emails and format it into raw JSON.
STRICT FORMATTING RULES:
- You must respond ONLY with a valid JSON object.
- Do NOT include markdown code blocks (like json … ), introductions, explanations, or notes.
- Use exactly the keys provided in the template below. If information is missing, set the value to null.
JSON TEMPLATE:
{
“customer_name”: “string”,
“order_id”: “string or null”,
“urgency_level”: “LOW, MEDIUM, or HIGH”,
“primary_issue”: “one sentence summary”,
“refund_requested”: boolean
}
EMAIL TO ANALYZE:
‘Hey support, I am Sarah Jenkins. My order #99281 shipped 3 weeks ago but it never arrived. I need this dress for a wedding this weekend, so I’m super stressed. I have tried to connect with you through your website and have reciived no response. I don’t want a replacement anymore, just give me my money back
The output is going to be like json:
{ “customer_name”: “Sarah Jenkins”, “order_id”: “99281”, “urgency_level”: “HIGH”, “primary_issue”: “The shipped order has not arrived after three weeks and is needed for an upcoming weekend event.”, “refund_requested”: true }
EXAMPLE 2
Prompt: Tell me token rates for Openai, Gemini and Anthropic models. Give me the result in tabular format
