<claudexml/>

Structured output with Claude XML and <format> tags

How to coerce Claude into reliable JSON or other schemas without using tool use.

When you don't want to set up the API's tool-use or JSON-mode features, you can still get reliable structured output with XML scaffolding and a <format> block.

How to apply the pattern

  1. Show the schema in <format>. Use a JSON skeleton with placeholder values that indicate type and allowed enum.
  2. Say "return only this object". Otherwise Claude often wraps it in markdown fences.
  3. Ask for the output inside a wrapper tag. <result>...</result> makes extraction bulletproof.
  4. Validate and retry on parse failure. JSON.parse failures should trigger a single retry with a stricter prompt.

Worked example

<instructions>
Classify the support ticket. Return exactly one JSON object matching <format>.
Return only the JSON inside <result> tags. No prose, no markdown fences.
</instructions>

<format>
{
  "category": "billing | bug | feature_request | account | other",
  "priority": "low | medium | high | urgent",
  "needs_human": true,
  "summary": "string, max 140 chars"
}
</format>

<ticket>{{ user_ticket }}</ticket>

Return your output inside <result> tags.

Tips

  • For production reliability, the API's tool-use feature is stronger than XML coercion. Use XML when you can't or don't want to define a tool.
  • Add `"summary": "string, max 140 chars"` directly in the schema — Claude often respects inline constraints.
Cite this page
Structured output with Claude XML and <format> tags. claudexml.com. https://claudexml.com/patterns/structured-output/