How to Force Claude 3.5 Sonnet to Output JSON: Developer Guide

How to Force Claude 3.5 Sonnet to Output JSON: Developer Guide

Claude 3.5 Sonnet is arguably today's most capable reasoning model for coding and data extraction. However, when using the Anthropic API, Claude has a habit of wrapping your JSON data in Markdown and adding conversational pleasantries like "Here is the JSON you requested:"

If you are building programmatic workflows, this conversational filler will instantly crash your JSON.parse() logic. Validating Claude's JSON output with a strict JSON Validator is critical.

In this guide, we'll explore the three best methods to force Claude 3.5 Sonnet to return 100% valid, programmatic JSON every single time.


Method 1: The "Prefill" Technique (The Anthropic Special)

Unlike OpenAI, Anthropic's API allows you to prefill the assistant's response. This means you can literally put words into Claude's mouth before it starts generating.

By prefilling the response with a single opening brace { and setting a stop sequence, you fundamentally trap the model into completing the JSON object instead of starting with a conversational greeting.

Example using the Anthropic SDK:

TYPESCRIPT
import Anthropic from '@anthropic-ai/sdk';

const anthropic = new Anthropic();

const msg = await anthropic.messages.create({
  model: "claude-3-5-sonnet-20240620",
  max_tokens: 1000,
  stop_sequences: ["}"], // Stops Claude from adding conversational text after the JSON
  system: "You are a data formatting assistant. Output only valid JSON.",
  messages: [
    { role: "user", content: "Generate a profile for a fictional character named Alex." },
    // Here is the magic prefill:
    { role: "assistant", content: "{" }
  ]
});

const textBlock = msg.content[0];
if (textBlock.type === 'text') {
  // Since we prefilled "{", we need to add it back to the final output!
  // And since we stopped at "}", we add that back too!
  const jsonString = "{" + textBlock.text + "}";
  const profile = JSON.parse(jsonString);
  console.log(profile);
}

Why this works: Language models are completion engines. If the assistant's turn already starts with {, the statistically most probable next token is the rest of the JSON object, not conversational text.


Method 2: Tool Use (Function Calling)

If you need a strict, complex schema, the official and most reliable method is to use Tool Use (Anthropic's version of function calling).

Instead of asking Claude to output JSON in the message body, you provide a tool with a strict JSON schema and tell Claude to "use" the tool.

TYPESCRIPT
const response = await anthropic.messages.create({
  model: "claude-3-5-sonnet-20240620",
  max_tokens: 1000,
  tools: [
    {
      name: "print_character_profile",
      description: "Prints a character profile.",
      input_schema: {
        type: "object",
        properties: {
          name: { type: "string" },
          age: { type: "integer" },
          skills: { type: "array", items: { type: "string" } }
        },
        required: ["name", "age", "skills"]
      }
    }
  ],
  // Force Claude to use the tool
  tool_choice: { type: "tool", name: "print_character_profile" },
  messages: [
    { role: "user", content: "Generate a profile for a character named Alex." }
  ]
});

const toolCall = response.content.find(block => block.type === 'tool_use');
if (toolCall && toolCall.type === 'tool_use') {
  // The output is strictly formatted JSON matching your schema!
  console.log(toolCall.input); 
}

Pros: Guaranteed schema compliance, natively supported by the API. Cons: Slightly higher token overhead.


Method 3: Strict System Prompts

If you are using the web interface (Claude.ai) or a system where you cannot use the API's prefill or tool-use features, you must rely entirely on prompt engineering.

Claude 3.5 Sonnet pays extremely close attention to the prompt. You should use XML tags (which Claude is heavily trained on) to define the rules.

The Ultimate Claude JSON Prompt:

TEXT
<system>
You are an automated data extraction machine. Your sole purpose is to process data and output valid JSON.

<rules>
1. Output ONLY valid JSON.
2. Do NOT output markdown formatting (no `json`).
3. Do NOT include conversational text before or after the JSON.
4. If you violate any of these rules, the system will crash.
</rules>

<schema>
{
  "status": "string",
  "data": ["item1", "item2"]
}
</schema>
</system>

Conclusion: Trust, but Verify

By mastering prefills, tool use, and strict XML prompts, you can turn Claude into a highly reliable, deterministic JSON engine. However, even with Claude 3.5 Sonnet's incredible reasoning capabilities, hallucinated trailing commas or unescaped quotes can occasionally sneak through.

If you are building an automated pipeline, never trust LLM output blindly. For the rare hallucinations that slip through, you can paste Claude's output directly into our Free Online JSON Validator to instantly check for syntax errors, or use our JSON Repair Tool to automatically fix trailing commas and missing quotes.