Chapter 7: Output Specification Techniques
Controlling Format, Structure, and Quality of AI Outputs
Learning Objectives
After completing this chapter, you will be able to:
- Specify output formats precisely
- Request structured data outputs (JSON, XML, tables)
- Control output length and detail level
- Define quality constraints
- Validate outputs against specifications
Why Output Specification Matters
The Output Problem
Without specification, AI outputs are:
- Unpredictable in format
- Inconsistent in length
- Variable in structure
- Difficult to process programmatically
The Specification Solution
With clear specifications:
- Outputs are immediately usable
- Processing can be automated
- Consistency is maintained
- Quality is verifiable
Impact Comparison
Figure 7.1: The transformation from unpredictable outputs to consistent, automatable results through proper specification.
Format Specification Methods
Method 1: Format Naming
Specify a well-known format by name:
| Format Name | Description | Example Use |
|---|---|---|
| JSON | Structured data | API responses |
| Markdown | Rich text | Documentation |
| CSV | Tabular data | Spreadsheets |
| XML | Hierarchical data | Configuration |
| YAML | Configuration | DevOps |
| Plain text | Simple output | Logs |
Example:
Return your response as valid JSON.
Method 2: Template Specification
Provide a template to follow:
Format your response using this template:
**Title:** [Title here]
**Summary:** [2-3 sentences]
**Key Points:**
- [Point 1]
- [Point 2]
- [Point 3]
**Recommendation:** [1 sentence]
Method 3: Schema Definition
Define the exact structure:
Return JSON matching this schema:
{
"analysis": {
"summary": "string (50-100 words)",
"confidence": "number (0-1)",
"findings": [
{
"title": "string",
"description": "string",
"severity": "low|medium|high"
}
],
"recommendation": "string"
}
}
Method 4: Example-Based Specification
Show an example of desired output:
Format your response like this example:
PRODUCT: Widget Pro
RATING: 4.5/5
PROS:
• Excellent build quality
• Easy to use
• Great value
CONS:
• Limited color options
• No warranty extension
VERDICT: Recommended for most users
Structured Data Formats
JSON Output
When to Use: APIs, data processing, programmatic use
Specification Pattern:
Return valid JSON with this structure:
{
"status": "success|error",
"data": {
"items": [],
"count": number
},
"metadata": {
"generated_at": "ISO 8601 timestamp",
"version": "1.0"
}
}
Ensure:
- All strings are properly escaped
- No trailing commas
- Valid JSON that parses without errors
Output Example:
{
"status": "success",
"data": {
"items": ["item1", "item2"],
"count": 2
},
"metadata": {
"generated_at": "2024-01-15T10:30:00Z",
"version": "1.0"
}
}
Markdown Tables
When to Use: Documentation, comparisons, structured display
Specification Pattern:
Present the comparison as a Markdown table with columns:
| Feature | Product A | Product B | Winner |
Include at least 5 feature comparisons.
Output Example:
| Feature | Product A | Product B | Winner |
|:--------|:----------|:----------|:-------|
| Price | $99 | $149 | Product A |
| Performance | Fast | Faster | Product B |
| Support | Email only | 24/7 | Product B |
| Storage | 50 GB | 100 GB | Product B |
| Ease of Use | Beginner-friendly | Advanced | Product A |
Structured Lists
When to Use: Action items, checklists, step-by-step processes
Specification Pattern:
Return as a numbered list with sub-items:
1. [Main point]
a. [Detail]
b. [Detail]
2. [Main point]
a. [Detail]
XML Output
When to Use: Configuration, data interchange, legacy systems
Specification Pattern:
Return valid XML with this structure:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>string</status>
<items>
<item>
<name>string</name>
<value>string</value>
</item>
</items>
</response>
Length and Detail Control
Length Specification
| Specification | Example |
|---|---|
| Word count | “Write 200-300 words” |
| Sentence count | “Respond in 3-5 sentences” |
| Paragraph count | “Provide a 2-paragraph summary” |
| Page length | “One page maximum (~500 words)” |
| Character limit | “Maximum 280 characters (tweet length)” |
Detail Level Specification
Detail Levels:
HIGH LEVEL: "Provide an executive summary suitable for a
30-second read. Key points only, no technical details."
MODERATE: "Explain with enough detail for a general audience
to understand. Include relevant examples but skip edge cases."
DETAILED: "Provide comprehensive coverage including edge cases,
exceptions, and technical nuances. Assume expert audience."
Length Control Examples
Short Response:
Summarize this document in exactly 3 bullet points,
each no longer than 15 words.
Constrained Response:
Explain quantum computing in:
- Exactly 100 words
- No technical jargon
- One analogy
Flexible Response:
Provide an analysis of appropriate length:
- Brief for simple findings
- Detailed for complex issues
- Target: 200-500 words based on complexity
Structure and Organization
Section-Based Structure
Organize your response into these sections:
## Overview
[1 paragraph introduction]
## Analysis
[Main content, 2-3 paragraphs]
## Findings
[Bulleted list of 3-5 findings]
## Recommendations
[Numbered list of actions]
## Conclusion
[1 paragraph wrap-up]
Hierarchical Structure
Structure the information hierarchically:
1. Main Topic
1.1 Subtopic A
1.1.1 Detail
1.1.2 Detail
1.2 Subtopic B
2. Main Topic
2.1 Subtopic
Parallel Structure
For each item, provide:
- Name
- Description (1-2 sentences)
- Pros (2-3 bullet points)
- Cons (2-3 bullet points)
- Rating (1-5 stars)
Maintain this exact structure for all items.
Quality Constraints
Content Quality
Quality requirements:
- All claims must be factually accurate
- Include sources for statistics
- Avoid speculation—state uncertainties clearly
- No marketing language or hyperbole
Consistency Constraints
Maintain consistency:
- Use American English throughout
- Numbers under 10 as words, 10+ as numerals
- Dates in MM/DD/YYYY format
- Currency with $ symbol and two decimals
Completeness Constraints
Ensure completeness:
- Address all points raised in the question
- Include both pros and cons
- Consider at least 3 alternatives
- Note any important caveats
Exclusion Constraints
Do NOT include:
- Personal opinions
- Information after knowledge cutoff
- Code in languages other than Python
- References to previous conversations
Output Validation
Self-Validation Instructions
After generating your response:
1. Verify JSON is valid (parseable)
2. Confirm all required fields are present
3. Check that word count is within range
4. Ensure no placeholder text remains
Validation Checklist Pattern
Before finalizing, verify:
- [ ] All sections from the template are present
- [ ] Word count is 200-300 words
- [ ] At least 3 examples are included
- [ ] No technical jargon used
- [ ] Ends with a clear call-to-action
Error Handling Instructions
If you cannot fulfill a requirement:
1. Complete what you can
2. Note the limitation explicitly
3. Suggest an alternative approach
4. Mark incomplete sections with [TODO]
Advanced Output Patterns
Multiple Output Formats
Provide the response in two formats:
FORMAT 1 - Executive Summary:
[3-5 bullet points for quick reading]
FORMAT 2 - Detailed Analysis:
[Full prose explanation with supporting details]
Conditional Formatting
Format based on result:
If findings are CRITICAL:
- Use bold headers
- List immediately at top
- Include remediation steps
If findings are INFORMATIONAL:
- Include in appendix
- Brief mention only
Progressive Detail
Structure with progressive detail:
TL;DR: [One sentence summary]
Key Points: [3-5 bullets]
Full Analysis: [Detailed explanation]
Technical Appendix: [Supporting data and details]
Format Specification Templates
Template: Report Output
## Output Format: Report
### Structure
1. Executive Summary (100 words max)
2. Background (1 paragraph)
3. Methodology (if applicable)
4. Findings (bulleted list)
5. Analysis (2-3 paragraphs)
6. Recommendations (numbered list)
7. Conclusion (1 paragraph)
### Style
- Professional tone
- Third person
- Past tense for findings
- Present tense for recommendations
Template: API Response
## Output Format: API Response
Return valid JSON:
```json
{
"success": boolean,
"data": {
// Primary response content
},
"errors": [
// Array of error objects if success is false
],
"meta": {
"request_id": "string",
"processing_time_ms": number
}
}
Requirements:
- Must parse as valid JSON
- No comments in output
- Null for missing optional fields ```
Template: Comparison Table
## Output Format: Comparison
| Criteria | Option A | Option B | Option C |
|:---------|:---------|:---------|:---------|
| [Criterion 1] | [Value] | [Value] | [Value] |
| [Criterion 2] | [Value] | [Value] | [Value] |
| ... | ... | ... | ... |
**Winner:** [Option] — [1-sentence justification]
Include at least 5 criteria rows.
Use checkmarks (✓) and X marks (✗) where applicable.
Common Output Mistakes
Mistake 1: Format Ambiguity
❌ AMBIGUOUS: "Give me a list"
(Bulleted? Numbered? Comma-separated?)
✅ SPECIFIC: "Provide a numbered list with exactly 5 items,
each item being a single sentence."
Mistake 2: Missing Structure
❌ UNSTRUCTURED: "Tell me about the pros and cons"
(Will be a prose blob)
✅ STRUCTURED: "List pros and cons in this format:
**Pros:**
• [Pro 1]
• [Pro 2]
**Cons:**
• [Con 1]
• [Con 2]"
Mistake 3: Conflicting Specifications
❌ CONFLICTING: "Be comprehensive. Keep it under 50 words."
✅ RESOLVED: "Provide the most essential information only,
in exactly 50 words."
Key Takeaways
- Output specification transforms unpredictable AI responses into usable outputs
- Multiple methods exist: format naming, templates, schemas, examples
- Structured formats (JSON, tables, XML) enable automated processing
- Length and detail can be precisely controlled
- Quality constraints ensure content meets standards
- Validation instructions help catch errors before delivery
Summary
Output specification is the final control point in prompt engineering. By explicitly defining format, structure, length, and quality requirements, you ensure AI outputs are immediately usable rather than requiring manual reformatting. The techniques in this chapter—from simple format naming to complex schema definitions—give you complete control over how AI delivers its responses.
Review Questions
- What are four methods for specifying output format?
- When would you choose JSON over Markdown tables?
- How can you specify both minimum and maximum length?
- What are three types of quality constraints?
- How do self-validation instructions improve output quality?
Practical Exercise
Exercise 7.1: Format Specification
Write output specifications for:
- A product comparison (3 products, 5 criteria)
- An API error response
- A weekly status report
Include format, structure, length, and quality requirements for each.
Exercise 7.2: JSON Schema Design
Design a JSON schema for a “book recommendation” response that includes:
- Book title and author
- Genre tags (array)
- Summary (50-100 words)
- Rating (1-5)
- Similar books (array of titles)
- Confidence score (0-1)