Originally published at claudeguide.io/claude-json-structured-output
Claude Structured Output: Getting Reliable JSON Every Time
Getting Claude to reliably return valid JSON requires three things: a JSON-only instruction in the system prompt, an example of the exact schema you want, and a validation step that handles the occasional malformed response in 2026. Without all three, Claude sometimes wraps JSON in markdown code blocks, adds explanatory text before or after, or returns slightly wrong field names. This guide covers the complete production pattern.
The simplest reliable JSON pattern
System prompt:
SYSTEM_PROMPT = """You are a data extraction assistant.
CRITICAL: You must respond with ONLY valid JSON. No explanation, no markdown
code blocks, no text before or after the JSON. Just the raw JSON object.
If you cannot extract the requested data, return: {"error": "reason"}
"""
User message with schema example:
USER_TEMPLATE = """Extract the following information from the text and return as JSON.
Required schema:
{{
"name": "string",
"email": "string or null",
"company": "string or null",
"phone": "string or null"
}}
Text to extract from:
{text}"""
Complete implementation:
python
import anthropic
import json
client = anthropic.Anthropic()
def extract_contact(text: str) -
[→ Get the Agent SDK Cookbook — $49](https://shoutfirst.gumroad.com/l/ogxhmy?utm_source=claudeguide&utm_medium=article&utm_campaign=claude-json-structured-output)
*30-day money-back guarantee. Instant download.*
Top comments (0)