Claude Structured Outputs Guide

Learn JSON, XML, YAML, and tool use patterns for reliable structured data extraction. Use the live tester with 10 pre-built templates to see responses and validate schemas before writing code.

By Michael Lip · May 28, 2026

Live Structured Output Tester

Select a template, choose your output format, and click Run to see a simulated structured response with schema validation. Edit the prompt and schema freely.

Templates (10 patterns)
Output Format
Prompt Template
Schema / Structure Hint
Simulated Response
Select a template or type a prompt and click Run to see the simulated structured output here.
This tester simulates Claude responses based on the selected template. In production, Claude calls your API with real data.

Structured Output Code Generator

Production-ready code for the selected format and template. Switch tabs for your preferred language.

Why Structured Outputs Matter

Most production Claude integrations need data in a machine-readable format, not conversational prose. A pipeline that extracts product details from reviews, classifies support tickets, converts invoices to database records, or generates configuration files must receive output that a downstream system can parse without ambiguity. Structured outputs are the bridge between Claude's generative capability and reliable software systems.

Claude supports four main approaches to structured output: prompting for JSON, prompting for XML, prompting for YAML, and using the tool use (function calling) API. Each has different tradeoffs in terms of reliability, flexibility, and ease of parsing. Understanding when to use each approach is as important as knowing the implementation details.

JSON Output via Tool Use (Recommended)

The most reliable method for getting JSON from Claude is to define a tool with a JSON Schema and force Claude to call it. When you set tool_choice to {"type": "tool", "name": "your_tool_name"}, Claude is required to produce output that matches your schema. The response arrives as a tool_use content block whose input field contains the structured JSON. Because schema compliance is enforced at the API level, parsing failures are eliminated.

The pattern is: define your output schema as a JSON Schema object under the input_schema field of the tool definition, set tool_choice to force the call, make the API request, and extract response.content[0].input. The result is a Python dict or JavaScript object matching your schema exactly. No JSON parsing edge cases, no markdown fences, no explanatory text to strip out.

For complex schemas with nested objects, arrays, enums, and required fields, tool use is the only approach that provides guarantee-level reliability. For example, extracting structured invoice data with line items, tax calculations, and vendor details benefits enormously from a well-defined tool schema rather than hoping the model formats JSON correctly on every call. The Tool Use Builder on ClaudKit can help you construct these schemas visually.

JSON via System Prompt (Simpler, Less Reliable)

A simpler but less reliable approach is to instruct Claude in the system prompt to respond with JSON only. A well-written system prompt specifies: the exact JSON structure as either a schema or an example, the requirement to return only the JSON object without any surrounding text, and handling instructions for cases where the input does not contain the requested data (typically returning null values or an empty object rather than an error message).

This approach works well for simpler structures and during prototyping. The failure modes are: Claude occasionally includes a markdown code fence around the JSON (requiring you to strip it), the model may include explanatory text before or after the JSON when the request is ambiguous, and deeply nested or highly constrained schemas are more likely to produce structural errors. For production pipelines where reliability matters, migrate to tool use. For quick prototypes and simple structures, system prompt JSON is often sufficient.

XML Extraction Patterns

XML is an underrated output format for Claude. The model is trained on vast quantities of XML-structured text and produces well-formed XML reliably when given a clear structure specification. XML is particularly useful when you need hierarchical data with explicit element semantics, when working with systems that already use XML (RSS feeds, configuration files, SOAP APIs), or when you want to use XML tags as delimiters within a larger response.

The XML delimiter pattern is especially powerful: instruct Claude to wrap specific parts of its output in named XML tags, then extract those sections using simple string parsing or an XML parser. For example, you can ask Claude to think through a problem (wrapped in <thinking> tags) and then produce a final answer (wrapped in <answer> tags). Your application extracts only the answer tag contents, discarding the chain-of-thought reasoning. This pattern gives you structured extraction without requiring the entire response to be machine-readable.

YAML Generation

YAML output from Claude is useful for generating configuration files, infrastructure definitions (Kubernetes manifests, Docker Compose files, GitHub Actions workflows), and any format where human readability of the output is also important. Claude produces clean YAML when given a clear structure specification, but YAML is more whitespace-sensitive than JSON, so prompt clarity about nesting depth is important.

Specify the expected top-level keys and nesting structure explicitly in your prompt. Provide an example of the desired output for complex structures. YAML does not have a standardized schema validation approach as mature as JSON Schema, so application-level validation is more manual. For simple key-value configurations, YAML prompting is fast and ergonomic. For data pipelines where you need guaranteed structure, JSON tool use remains the better choice.

Schema Design Best Practices

Well-designed schemas significantly improve output quality regardless of the output format. Start with the minimum required fields — every additional required field increases the chance that Claude returns null or fabricated values for information not present in the input. Use descriptive property names and add description fields to each property in JSON Schema; Claude reads these descriptions and uses them to understand what each field should contain.

Use enums for categorical fields to avoid synonym proliferation. If you need a sentiment field, define it as "enum": ["positive", "neutral", "negative"] rather than a free string. Use minimum and maximum for numeric fields where range is known. For optional information that may or may not be present in the source text, make fields optional rather than required — Claude will return null for absent data rather than hallucinating plausible values. For complex multi-schema use cases, tools like Kappafy can assist with JSON schema design and validation.

Prompt Patterns for Consistent Structured Output

Several prompt patterns consistently improve structured output quality. Setting temperature to 0 increases determinism and reduces the chance of the model varying its output format across calls. Including a few-shot example of the expected output in the system prompt dramatically improves adherence to complex schemas. Explicitly stating what to do when information is missing ("if the field cannot be determined from the text, use null") prevents hallucination of placeholder values.

For extraction tasks, the prompt pattern "Extract the following fields from the text below" outperforms "Analyze this text and return" — the word "extract" signals to the model that it should find existing information rather than generate new content. For generation tasks (producing structured content from a template), provide the schema inline in the prompt rather than in a separate system prompt, as this keeps the schema and the generation instruction together in context.

Chain-of-thought reasoning improves accuracy for complex extraction tasks. Ask Claude to first identify relevant sections of the input, then extract values from those sections, and finally produce the structured output. The reasoning steps can be wrapped in XML tags and discarded by your application, with only the final structured output passed downstream. For end-to-end pipeline testing of structured output prompts, the Claude Testing Framework guide shows how to set up regression tests across schema changes and model versions.

Validation and Error Handling

Even with tool use, application-level validation is worthwhile. Validate enum values, check numeric ranges, verify that arrays have the expected number of items, and confirm that required string fields are non-empty. In Python, the jsonschema library provides jsonschema.validate(instance, schema) which raises a ValidationError with a descriptive message. In JavaScript, ajv is the standard library for JSON Schema validation with strong TypeScript support.

When validation fails, log the raw response alongside the validation error. Examine whether the failure is due to a model limitation (it could not find the information), a schema design issue (the field was too restrictive), or a prompt clarity issue (the model misunderstood what was being asked). Structured output failures are almost always fixable with prompt or schema adjustments. Implement a fallback path that handles validation failures gracefully — either retry with a clearer prompt, use default values, or surface the error to a human reviewer rather than crashing the pipeline.

Frequently Asked Questions

How do I get Claude to return valid JSON?

The most reliable method is tool use (function calling): define a tool with a JSON Schema, set tool_choice to force Claude to call that tool, and extract the input field from the tool_use block. Claude will always produce valid JSON matching your schema. Alternatively, instruct Claude in the system prompt to respond with JSON only and parse the response.

What is the difference between JSON mode and tool use for structured outputs?

Tool use (function calling) is more reliable for structured outputs. When you define a tool with a JSON Schema and force Claude to call it, the response is guaranteed to match your schema. JSON mode (prompting Claude to output JSON) relies on instruction following and may occasionally include markdown formatting or extra text. For production applications, tool use is the recommended approach.

Can Claude output YAML or XML structured data?

Yes. Claude can output YAML and XML when instructed via the system prompt or user message. For XML, specify the element structure you expect. For YAML, describe the key hierarchy. These are not schema-validated at the API level, so include parsing and validation in your application code.

How do I validate JSON output from Claude?

In Python, use json.loads() to parse and jsonschema.validate() to check against your schema. In JavaScript, use JSON.parse() and a library like ajv. Always wrap parsing in try/catch blocks and log failures alongside the raw response for debugging.

What are the best prompt patterns for consistent structured outputs?

Five patterns improve consistency: (1) Use tool use with forced tool_choice. (2) Provide the exact schema in the system prompt. (3) Use XML tags as delimiters. (4) Set temperature to 0 for deterministic structure. (5) Include a few-shot example of the expected format. Combining tool use with temperature 0 gives the highest consistency.

Developer and creator of the Zovo Tools network. Building free, privacy-first developer tools that run entirely in the browser. No tracking, no sign-ups, no server-side processing. Open source on GitHub.