Search⌘ K
AI Features

Output Format Control and Structured Outputs

Understand how to control Claude's response format by using JSON Outputs to ensure schema-valid structured data on every call. Learn techniques to prevent parsing errors, guide content with precise prompts, enforce tool argument schemas, and use stop sequences for free text bounding. This lesson equips you to build predictable, production-ready AI pipelines that integrate smoothly with downstream systems and avoid common format failures.

An agent that produces freeform prose is difficult to integrate with downstream systems. A pipeline that extracts fields from a response needs predictable structure. A coordinator that reads specialist results needs JSON it can parse without a fragile regex. This lesson covers how to move Claude from “usually returns JSON” to output that is guaranteed to match a schema, by construction, on every call.

By the end of this lesson, we will be able to:

  • Use the JSON Outputs feature (output_config.format) to guarantee schema-valid structured output

  • Use strict tool use to guarantee that a tool call’s arguments match its schema

  • Write system prompt instructions that guide content quality alongside a guaranteed shape

Why “return JSON” in the system prompt is not enough

A system prompt instruction like “Return your answer as a JSON object” works most of the time. It fails in three specific situations:

  • Situation 1: Claude adds a preamble. Instead of starting with {, Claude writes “Here is the JSON object you requested:” followed by the JSON. The response is valid if you strip the first line, but it breaks any parser that calls json.loads() on the raw string.

  • Situation 2: Claude wraps JSON in a markdown code block. Claude returns ```json\n{...}\n``` instead of {...}. This is syntactically correct markdown but not directly parseable as JSON.

  • Situation 3: Claude adds an explanation after the JSON. The JSON is valid but is followed by “Note: the status field will be null if the order has not shipped yet.” The parser sees everything after the closing } as a syntax error.

All three situations share a root cause: a system prompt instruction is a request, not a constraint. Nothing stops Claude from adding text around the JSON except its own judgment about what a helpful answer looks like, and that holds regardless of which model is ...