Skip to content

原文(日本語に翻訳)

--json-schema とワークフローの agent({schema}) による構造化出力を修正:StructuredOutputが成功した後にモデルが無限に再呼び出しできなくなり、フォローアップターンでも確実に構造化出力が返されるようになった。

原文(英語)

Fixed --json-schema and workflow agent({schema}) structured output: the model can no longer re-call StructuredOutput indefinitely after a successful call, and follow-up turns now reliably return structured output.

概要

--json-schema フラグやワークフロースクリプトの agent({schema}) を使用した構造化出力に関する2つの重要なバグが修正されました。まず、StructuredOutput ツールが成功した後もモデルが繰り返し呼び出し続ける無限ループの問題が解消されました。また、フォローアップターン(会話の2回目以降)でも構造化出力が確実に返されるようになりました。

基本的な使い方

CLIでのJSON Schema使用

bash
# JSONスキーマを指定してClaude Codeを実行
claude -p "製品一覧を分析してください" --json-schema '{"type":"object","properties":{"products":{"type":"array","items":{"type":"object","properties":{"name":{"type":"string"},"score":{"type":"number"}}}}}}'

ワークフロースクリプトでのagent({schema})使用

javascript
// workflow.js
const PRODUCT_SCHEMA = {
  type: "object",
  properties: {
    products: {
      type: "array",
      items: {
        type: "object",
        properties: {
          name: { type: "string" },
          score: { type: "number" },
          recommendation: { type: "string" }
        }
      }
    }
  }
};

const result = await agent("製品一覧を分析してください", {
  schema: PRODUCT_SCHEMA
});
// 修正後: resultは確実に構造化されたオブジェクトが返される

実践例

データ抽出パイプラインでの活用

javascript
// 複数ステップのデータ処理で確実に構造化データを取得
const EXTRACTION_SCHEMA = {
  type: "object",
  properties: {
    entities: {
      type: "array",
      items: {
        type: "object",
        properties: {
          name: { type: "string" },
          type: { type: "string" },
          confidence: { type: "number" }
        }
      }
    }
  }
};

// ステップ1: 初回の構造化出力
const step1 = await agent("テキストからエンティティを抽出してください", {
  schema: EXTRACTION_SCHEMA
});

// ステップ2: フォローアップでも確実に構造化出力が返される(修正前はここで失敗していた)
const step2 = await agent("抽出されたエンティティの信頼スコアを再評価してください", {
  schema: EXTRACTION_SCHEMA
});

バッチ処理での並列構造化出力

javascript
const results = await parallel(items.map(item => () =>
  agent(`「${item.name}」を分析してください`, {
    schema: ANALYSIS_SCHEMA
  })
));
// 修正後: すべての並列エージェントが確実に構造化データを返す

CLIでのバッチ処理スクリプト

bash
#!/bin/bash
# 複数ファイルを構造化出力で分析
for file in *.py; do
  claude -p "このファイルのコード品質を分析してください: $(cat $file)" \
    --json-schema '{"type":"object","properties":{"score":{"type":"number"},"issues":{"type":"array","items":{"type":"string"}}}}' \
    >> analysis_results.jsonl
done

注意点

  • この修正により、ワークフロースクリプトでの agent({schema}) を使ったデータパイプラインの信頼性が大幅に向上します
  • 修正前は成功した構造化出力の後に追加のターンを行うと、構造化されていない応答が返ることがありました
  • --json-schema はJSON Schema Draft 7以降の形式に対応しています
  • 構造化出力はモデルに特定のフォーマットを強制するため、通常の会話よりも若干応答時間が長くなる場合があります

関連情報