MagicTools
Claude GuidesApril 13, 2026548 views2 min read

Structured Output and Multimodal: Formatted Responses and Vision

Getting JSON Output

The most reliable way to get structured JSON from Claude is to provide a clear schema:

Analyze the sentiment of the following user review and output in JSON format:
{
  "sentiment": "positive | negative | neutral",
  "confidence": 0.0-1.0,
  "keywords": ["keyword array"],
  "summary": "one-sentence summary"
}

Review: The steak at this restaurant was excellent, but the wait time was way too long and the service was mediocre.

Claude will return results precisely matching the schema.

Prefilling to Guarantee Format

When using the API, prefilling the assistant response guarantees output format with 100% reliability:

messages = [
    {"role": "user", "content": "Analyze the sentiment of this text"},
    {"role": "assistant", "content": "{"}  # prefill
]

Claude continues from {, ensuring pure JSON output without preambles like "Sure, here is the analysis:".

XML Tags for Structured Sections

For complex outputs with multiple sections, XML tags are an excellent structuring tool:

Output the code review results in the following XML format:

<review>
  <issues>
    <issue severity="high|medium|low">
      <description>Issue description</description>
      <location>File and line number</location>
      <fix>Suggested fix</fix>
    </issue>
  </issues>
  <summary>Overall assessment</summary>
  <score>1-10</score>
</review>

XML tags support nesting and attributes, making them more flexible than JSON for expressing hierarchical relationships.

Handling Edge Cases

Prevent common issues in structured output with explicit constraints:

Output requirements:
- Always return valid JSON, even if input data is unusual
- Use null for fields that cannot be analyzed, not empty strings
- Return [] for empty arrays, do not omit the field
- Do not add any explanatory text outside the JSON

These constraints ensure your code can reliably parse Claude's output.

Multimodal: Image Understanding

Claude has powerful vision capabilities. When sending images via API, combine them with text prompts for analysis:

Analyze the UI in this screenshot:
1. List all visible UI components
2. Identify design guideline violations
3. Provide improvement suggestions

Output as a JSON array with component, issue, and suggestion fields per item.

Common Image Analysis Scenarios

Scenario Prompt Example
OCR text extraction "Extract all text from the image, preserving layout"
Chart data reading "Read data from this bar chart and output as a table"
UI description "Describe the layout structure and interactive elements"
Document parsing "Extract key info from this invoice: date, amount, vendor"

Combining Vision with Structured Output

Merging vision capabilities with structured output is the most powerful application pattern:

Analyze this product screenshot and output in JSON format:
{
  "product_name": "name",
  "price": "price",
  "rating": "rating",
  "key_features": ["feature list"],
  "visible_issues": ["UI issues"]
}

This combination is ideal for building automated data extraction pipelines.

Frequently Asked Questions

Can Claude guarantee valid JSON output 100% of the time?

In most cases yes, especially with the prefilling technique. However, in edge cases such as very long outputs being truncated, JSON may be incomplete. Always wrap JSON parsing in try-catch blocks in your code and implement retry logic.

What image types can Claude process?

Claude supports JPEG, PNG, GIF, and WebP formats. It can understand photos, screenshots, charts, scanned documents, and handwritten notes. Accuracy decreases with very small text, blurry images, or highly abstract artwork.

Is structured output better than free-form output?

It depends on the use case. If output needs to be parsed by code (API integrations, data pipelines), always use structured output. If the content is meant for human reading (articles, emails, reports), free-form is usually more natural. You can also combine both — have Claude place free-form text within specific JSON fields.

Are there limitations when sending images?

A single API request can include up to 20 images. Images consume token quota, with higher resolution images using more tokens. It is recommended to compress images before sending — keep key details clear without requiring ultra-high resolution.

Related Articles

Anthropic Deleted 80% of Claude Code's System Prompt. The Evals Didn't Move.

Anthropic removed over 80% of Claude Code's system prompt for Claude Opus 5 and Fable 5 with no measurable loss on coding evals. The popular reading — 'write shorter prompts' — is wrong. What got deleted was constraints, not information. Here is a usable dividing line, the API-layer mechanics the official post left out (cache prefixes, defer_loading, runtime injection), and the two real disagreements from 343 comments on Hacker News.

prompt-engineeringclaude-code+5
ai-tutorialsJul 27, 202613 min
71

Prompt Engineering for Beginners: 10 Techniques That Actually Work

Two people ask the same AI the same question and get completely different results. The difference is prompt quality. This guide covers 10 concrete, practical techniques for writing prompts that consistently get useful, accurate outputs from any large language model.

prompt-engineeringchatgpt+3
ai-promptsApr 22, 20268 min
95

System Prompt Design: Make Claude Understand Exactly What You Need

Deep dive into system prompt design principles and best practices. Learn to control Claude's behavior through role definition, constraints, and output formatting.

prompt-engineeringclaude+2
claudeApr 13, 20262 min
764

The Complete Guide to Prompt Engineering with Claude

Master the core principles and practical techniques of prompt engineering with Claude. From basics to advanced strategies for writing precise, effective prompts.

prompt-engineeringclaude+2
claudeApr 13, 20264 min
559

Published by MagicTools