09. Output Format Control
Controlling output format matters when the model output feeds into downstream systems. Unstructured text requires parsing; structured output can be consumed directly.
Format control uses three techniques:
- Explicit structure in the prompt: "Return JSON with fields X, Y, Z"
- Demonstrated examples: Show the exact format you want
- Constraint statements: "Do not add explanations"
Return only the JSON object, no additional text:
{
"review_sentiment": "positive",
"key_phrases": ["fast delivery", "quality packaging"],
"product_mentioned": true
}
Adding "Return only" prevents the model from adding explanatory text around the structured output.
Format control is essential for:
- JSON output consumed by applications
- Markdown tables for data transfer
- CSV-compatible structures
- Code generation with specific syntax
Generate a YAML configuration file for a web server with these requirements:
- Listen on port 8080
- Enable compression
- Set max request size to 10MB
- Log errors only
Return ONLY the YAML, no preamble or explanation.
The model may still add markdown code fences (```yaml). If you need raw output, specify:
Return raw YAML without code fences, markdown formatting, or explanatory text.
For complex formats, show a complete example:
Generate a JSON configuration for this deployment:
{
"app": "my-service",
"version": "1.2.3",
"replicas": 3,
"resources": {
"cpu": "500m",
"memory": "256Mi"
},
"env": {
"DATABASE_URL": "postgres://db:5432/app",
"LOG_LEVEL": "info"
}
}
Now generate for:
app: payment-processor
version: 2.0.0
replicas: 5
cpu: 1000m, memory: 512Mi
env: DATABASE_URL, API_KEY (placeholder), LOG_LEVEL: debug
The example establishes format, indentation style, and placeholder conventions.
Local verification checkpoint
Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.
Take a task that produces unstructured output and rewrite the prompt to produce structured output (JSON, YAML, or specific markdown). Validate with three test inputs.