AI Tools 2026年4月18日

Claude Structured Outputs完全解説:JSONスキーマ準拠レスポンスを保証する

2025年11月14日betaリリースのClaude Structured Outputsを解説。JSONスキーマへの完全準拠、Strict tool use、対応モデル、「JSONで返して」プロンプト指示との違いを網羅。

TL;DR

  • 「JSONで返してください」指示だけでは稀にフォーマットが崩れる
  • Structured Outputsを使うとJSONスキーマへの完全準拠を保証
  • パースエラーが根本的になくなる → エラーハンドリングが不要に

概要

Claude Structured Outputs は、2025年11月14日にbetaとして公開されたレスポンス形式制御機能です。JSONスキーマを指定すると、Claudeのレスポンスがスキーマに完全に準拠することを保証します。

プロンプトに「JSONで返してください」と書くだけでは、稀に不完全なJSONやフォーマット違反が発生します。Structured Outputsはこの問題を根本的に解決します。


基本的な使い方

import anthropic

client = anthropic.Anthropic()

# 出力スキーマを定義
output_schema = {
    "type": "object",
    "properties": {
        "title": {
            "type": "string",
            "description": "記事タイトル"
        },
        "summary": {
            "type": "string",
            "description": "200字以内のサマリー"
        },
        "tags": {
            "type": "array",
            "items": {"type": "string"},
            "description": "関連タグ(3〜5個)"
        },
        "sentiment": {
            "type": "string",
            "enum": ["positive", "neutral", "negative"],
            "description": "記事のトーン"
        }
    },
    "required": ["title", "summary", "tags", "sentiment"]
}

response = client.beta.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "以下のニュース記事を分析してください:\n\n[記事本文...]"
        }
    ],
    # Structured Outputsを有効化
    output={
        "type": "json_schema",
        "json_schema": {
            "name": "article_analysis",
            "schema": output_schema
        }
    },
    betas=["structured-outputs-2025-11-13"],
)

# スキーマに準拠したJSONが保証される
import json
result = json.loads(response.content[0].text)
print(result["title"])   # 確実に文字列
print(result["tags"])    # 確実にリスト
print(result["sentiment"])  # "positive"/"neutral"/"negative"のみ

Strict tool use(ツール入力の検証)

ツールコールの入力にもスキーマ準拠を強制できます:

response = client.beta.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=[
        {
            "name": "create_user",
            "description": "新規ユーザーを作成します",
            "input_schema": {
                "type": "object",
                "properties": {
                    "email": {"type": "string", "format": "email"},
                    "name": {"type": "string", "minLength": 1},
                    "role": {
                        "type": "string",
                        "enum": ["admin", "editor", "viewer"]
                    }
                },
                "required": ["email", "name", "role"],
                "additionalProperties": False  # 余分なフィールドを禁止
            },
            "strict": True  # Strict tool useを有効化
        }
    ],
    messages=[{"role": "user", "content": "田中省伍をadminとして登録してください"}],
    betas=["structured-outputs-2025-11-13"],
)

「JSONで返して」との比較

項目プロンプト指示のみStructured Outputs
準拠保証なし(99%は正常)完全保証
パースエラー稀に発生発生しない
エラーハンドリング必要不要
enum制約プロンプト次第スキーマで強制
追加プロパティ制御困難additionalProperties: false
設定の手間少ないスキーマ定義が必要

対応モデル

モデル対応開始
Claude Sonnet 4.52025年11月14日
Claude Opus 4.12025年11月14日
Claude Haiku 4.52025年12月4日
Claude Opus 4.7対応

活用シーン

  • データ抽出パイプライン: ドキュメントから構造化データを一括抽出
  • 分類タスク: カテゴリの選択肢をenumで強制
  • フォーム入力の自動補完: 型・必須フィールドを保証
  • API統合: 下流システムに渡すペイロードを確実にバリデーション

参考リンク