published fresh until

How do I get guaranteed JSON from the Claude API?

Summary As of November 2026, use output_config.format for JSON output or strict: true for tools. Handle refusals, truncation and enum/const capitalization exceptions. Python parse helpers retain output_format; SDK v1 beta create/count_tokens calls use output_config

Not for cutting API cost per request; see claude-api-cost-optimize-guide

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

As of November 2026, use output_config.format for Claude JSON output and strict: true for tool inputs, then validate before use. Do first: choose the SDK parse helper and handle refusal, truncation and string-enum capitalization exceptions (structured outputs).

The problem

You want the Claude API to return JSON that always matches your schema, without regex cleanup or retry loops, and your first attempt returns a 400 error or a cut-off object.

What the docs say

Anthropic's structured outputs page describes two features that can be used separately or together:

  • JSON outputs: put output_config.format with type: "json_schema" and a schema on the request. This controls what Claude says.
  • Strict tool use: strict: true on a tool validates tool names and inputs.

The availability section lists all models and all supported cloud platforms for the Messages API; Managed Agents is unavailable. The older output_format parameter and its beta header are deprecated in favour of output_config.format.

Schema limits (a request that uses them returns 400): recursive schemas, external $ref, numerical constraints (minimum, maximum, multipleOf), string length constraints, array constraints beyond minItems of 0 or 1, and additionalProperties set to anything but false (it must be false on objects). enum, const, anyOf, allOf, internal $ref and some string formats are supported (allOf with two or more schemas is unsupported when one directly contains $ref, anyOf or allOf); regex backreferences and lookaround are not.

It can still go wrong: with stop_reason: "refusal" you get HTTP 200, billed tokens and output that may not match the schema; with stop_reason: "max_tokens" the object may be cut off, so retry with a higher limit. The first request with a new schema is slower because the grammar compiles; compiled grammars are cached for 24 hours since last use, and changing the schema structure or the tool set invalidates that cache (changing only names or descriptions does not). Changing output_config.format also invalidates the prompt cache for that conversation. Per-request limits are 20 strict tools, 24 optional parameters and 16 union-type parameters; the latter two totals span strict tool schemas and JSON output schemas. Compilation can also fail on combined complexity or a 180-second timeout (limits).

Capitalization exception: string enum and const values may differ in capitalization, with a normal completion and no special stop reason. Avoid values distinguished only by case; normalize only where your application allows it, then validate (invalid outputs).

SDK helpers: in Python pass a Pydantic model as output_format to client.messages.parse() and read parsed_output; in TypeScript wrap a Zod schema in zodOutputFormat(). The helpers move unsupported constraints into field descriptions and still enforce the original constraints when validating the response. jsonSchemaOutputFormat() takes a plain schema but does not validate by default.

How to describe it to your agent

Use Claude structured outputs for this extraction: build the schema from our Pydantic model and call client.messages.parse(). Check stop_reason before using the result: handle refusal and retry max_tokens with a higher limit. Keep constraints like min/max in the Pydantic model (the SDK validates them) rather than in the raw schema. Do not change the schema between requests unless needed; it invalidates the grammar cache.

Mistakes to check

  • Putting minimum or maxLength in a raw schema (400); use the SDK helper or validate yourself.
  • Forgetting that a guaranteed shape is not guaranteed correct content: validate meaning too.
  • Using output_format on Python SDK v1.0+ client.beta.messages.create() or count_tokens(): it raises TypeError; use output_config. The output_format helper argument to client.messages.parse() remains supported (migration).

Limits

Constrained shape does not guarantee correct content. Unsupported raw-schema constraints return HTTP 400; SDK helpers can transform the schema and apply the original validation locally.

Sources

Started from a post by @Anthropic, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I get guaranteed JSON from the Claude API?
Summary: As of November 2026, use output_config.format for JSON output or strict: true for tools. Handle refusals, truncation and enum/const capitalization exceptions. Python parse helpers retain output_format; SDK v1 beta create/count_tokens calls use `output_config`
Copy for agent
get the whatworks thread claude-api-structured-outputs-schema-limits and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread claude-api-structured-outputs-schema-limits
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks