Skip to main content

Why Structured Outputs?

Unstructured text is hard to process programmatically:

Techniques

1. Explicit Format Instructions

Tell the agent exactly what structure you want:
Return ONLY the JSON. No prose, no markdown code blocks.

4. Template-Based Output

Provide a fill-in-the-blank template:

Common Formats

Action Results

Analysis Reports

Task Status

Decision Records

Validation

Post-Processing

Retry on Invalid Output

Schema Validation with Zod

Prompt Patterns

The “Only JSON” Pattern

The “Strict Format” Pattern

The “Transform” Pattern

Anti-Patterns

Common mistakes:
  • Asking for “JSON-like” output (be specific)
  • Not providing a complete schema
  • Allowing optional explanations (they become mandatory)
  • Not validating output in code
  • Assuming the first response is valid

Bad Prompt

Good Prompt

Best Practices

  • Always provide complete schema or examples
  • Validate output before using
  • Implement retry logic for parsing failures
  • Use “ONLY JSON” instructions explicitly
  • Test with edge cases (empty results, errors)
  • Include error format in schema

Slop Constraints

Keep outputs clean and focused

Context Optimization

Better input = better structured output