Skip to main content

Get structured JSON output from Claude

When you need machine-readable output, constrain Claude's response to a JSON schema with output_config.format. The model returns JSON matching the schema, so you can parse it directly instead of coaxing structure out of prose or writing fragile string parsing. Set ANTHROPIC_API_KEY first.

import Anthropic from "npm:@anthropic-ai/sdk";

const client = new Anthropic();
Describe the exact shape you want back. Structured outputs require every object to set additionalProperties: false.
const schema = {
  type: "object",
  properties: {
    name: { type: "string" },
    field: { type: "string" },
    languages: { type: "array", items: { type: "string" } },
  },
  required: ["name", "field", "languages"],
  additionalProperties: false,
};

const response = await client.messages.create({
  model: "claude-opus-4-8",
  max_tokens: 1024,
output_config.format constrains the whole response to the schema.
  output_config: { format: { type: "json_schema", schema } },
  messages: [
    {
      role: "user",
      content:
        "Ada Lovelace was an English mathematician, regarded as the first " +
        "computer programmer.",
    },
  ],
});
The response text is JSON that matches the schema, ready to parse.
const block = response.content.find((b) => b.type === "text");
if (block?.type === "text") {
  const person = JSON.parse(block.text);
  console.log(person);
{ name: "Ada Lovelace", field: "mathematics", languages: ["English"] }
}

Run this example locally using the Deno CLI:

deno run -N -E https://docs.deno.com/examples/scripts/anthropic_structured_output.ts

Did you find what you needed?

Privacy policy