

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# モデルから検証済みの JSON 結果を取得する
<a name="structured-output"></a>

構造化出力は Amazon Bedrock の機能であり、モデルレスポンスがユーザー定義の JSON スキーマとツール定義に準拠し、本番 AI デプロイでのカスタム解析および検証メカニズムの必要性が軽減されます。

## 利点
<a name="structured-output-benefits"></a>

構造化出力は、本番稼働用 AI アプリケーションの重要な課題に対処します。
+ **スキーマのコンプライアンスを確保** – プロンプトベースのアプローチからエラー率と再試行ループを排除
+ **開発の複雑さの軽減** – カスタム解析および検証ロジックが不要
+ **運用コストの削減** – 失敗したリクエストと再試行を削減
+ **本稼働の信頼性** – 予測可能で機械読み取り可能な出力を必要とする AI アプリケーションの信頼性の高いデプロイを可能にします。

## 仕組み
<a name="structured-output-how-it-works"></a>

構造化出力は、特定のスキーマに従うようにモデルレスポンスを制限し、ダウンストリーム処理のために有効で解析可能な出力を確保します。構造化出力は、次の 2 つの補完的なメカニズムを通じて使用できます。

### JSON スキーマ出力形式
<a name="structured-output-json-schema"></a>

Anthropic Claude モデルを使用した InvokeModel API の場合は、 `output_config.format`リクエストフィールドを使用します。オープンウェイトモデルでは、 `response_format` リクエストフィールドを使用します。Converse APIs、 `outputConfig.textFormat`リクエストフィールドを使用します。モデルのレスポンスは、指定された JSON スキーマに準拠します。

### 厳格なツールの使用
<a name="structured-output-strict-tool-use"></a>

ツール定義に `strict: true`フラグを追加して、ツール名と入力のスキーマ検証を有効にします。その後、モデルのツール呼び出しは、定義されたツール入力スキーマに従います。

これらのメカニズムは、同じリクエストで個別に使用することも、一緒に使用することもできます。詳細については、[Bedrock API ドキュメント](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)を参照してください。

### リクエストワークフロー
<a name="structured-output-request-workflow"></a>

以下に、Amazon Bedrock が構造化された出力でリクエストを処理する方法について説明します。

1. **初期リクエスト** – `outputConfig.textFormat`、、または `response_format`パラメータを介して JSON スキーマを含めるか`output_config.format`、推論リクエストに `strict: true`フラグを持つツール定義を含めます。

1. **スキーマの検証** – Amazon Bedrock は、サポートされている JSON Schema Draft 2020-12 サブセットに対して JSON スキーマ形式を検証します。スキーマにサポートされていない機能が含まれている場合、Amazon Bedrock はすぐに 400 エラーを返します。

1. **初回コンパイル** – 新しいスキーマの場合、Amazon Bedrock は文法をコンパイルします。これには数分かかる場合があります。

1. **キャッシュ** — 正常にコンパイルされた文法は、最初のアクセスから 24 時間キャッシュされます。キャッシュされた文法は AWS が管理するキーで暗号化されます。

1. **後続のリクエスト** – 同じアカウントからの同じスキーマはキャッシュされた文法を使用するため、推論レイテンシーは最小限のオーバーヘッドで標準リクエストと同等になります。

1. **レスポンス** – 厳密なスキーマコンプライアンスで標準の推論レスポンスを受け取ります。

## サポートされている APIsまたは機能
<a name="structured-output-supported-apis"></a>

構造化出力は、次の Amazon Bedrock 機能で使用できます。

**Converse および ConverseStream APIs** – 会話推論用の Converse および ConverseStream APIs で構造化出力を使用します。

**InvokeModel API と InvokeModelWithResponseStream APIs** シングルターン推論にはInvokeModel API と InvokeModelWithResponseStream APIs で構造化出力を使用します。

**クロスリージョン推論** – 追加セットアップなしで、クロスリージョン推論内で構造化出力を使用します。

**バッチ推論** – 追加のセットアップなしで、バッチ推論内で構造化出力を使用します。

**注記**  
構造化出力は、Anthropic モデルの引用と互換性がありません。構造化出力の使用中に引用を有効にすると、モデルは 400 エラーを返します。

## サポートされているモデル
<a name="structured-output-supported-models"></a>

構造化出力をサポートするモデルを確認するには、[モデルを一目で](model-cards.md)確認し、関心のあるモデルを選択してください。

## リクエストの例
<a name="structured-output-examples"></a>

### JSON スキーマ出力形式
<a name="structured-output-json-schema-examples"></a>

次の例は、構造化出力で JSON スキーマ出力形式を使用する方法を示しています。

#### Converse API
<a name="json-schema-converse"></a>

##### 例を表示する
<a name="w2aac15c32c35c13b3b5b3b1"></a>

```
{
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "text": "Given the following unstructured data, extract it into the provided structure."
        },
        {
          "text": "..."
        }
      ]
    }
  ],
  "outputConfig": {
    "textFormat": {
      "type": "json_schema",
      "structure": {
        "jsonSchema": {
          "schema": "{\"type\": \"object\", \"properties\": {\"title\": {\"type\": \"string\", \"description\": \"title\"}, \"summary\": {\"type\": \"string\", \"description\": \"summary\"}, \"next_steps\": {\"type\": \"string\", \"description\": \"next steps\"}}, \"required\": [\"title\", \"summary\", \"next_steps\"], \"additionalProperties\": false}",
          "name": "data_extraction",
          "description": "Extract structured data from unstructured text"
        }
      }
    }
  }
}
```

#### InvokeModel (Anthropic Claude)
<a name="json-schema-invokemodel-claude"></a>

##### 例を表示する
<a name="w2aac15c32c35c13b3b7b3b1"></a>

```
{
  "anthropic_version": "bedrock-2023-05-31",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Given the following unstructured data, extract it into the provided structure."
        },
        {
          "type": "text",
          "text": "..."
        }
      ]
    }
  ],
  "max_tokens": 3000,
  "temperature": 1.0,
  "output_config": {
    "format": {
      "type": "json_schema",
      "schema": {
        "type": "object",
        "properties": {
          "title": {
            "type": "string",
            "description": "title"
          },
          "summary": {
            "type": "string",
            "description": "summary"
          },
          "next_steps": {
            "type": "string",
            "description": "next steps"
          }
        },
        "required": [
          "title",
          "summary",
          "next_steps"
        ],
        "additionalProperties": false
      }
    }
  }
}
```

#### InvokeModel (オープンウェイトモデル)
<a name="json-schema-invokemodel-openweight"></a>

##### 例を表示する
<a name="w2aac15c32c35c13b3b9b3b1"></a>

```
{
  "messages": [
    {
      "role": "user",
      "content": "Given the following unstructured data, extract it into the provided structure."
    },
    {
      "role": "user",
      "content": "..."
    }
  ],
  "inferenceConfig": {
    "maxTokens": 3000,
    "temperature": 1.0
  },
  "response_format": {
    "json_schema": {
      "name": "summarizer",
      "schema": {
        "type": "object",
        "properties": {
          "title": {
            "type": "string",
            "description": "title"
          },
          "summary": {
            "type": "string",
            "description": "summary"
          },
          "next_steps": {
            "type": "string",
            "description": "next steps"
          }
        },
        "required": [
          "title",
          "summary",
          "next_steps"
        ],
        "additionalProperties": false
      }
    },
    "type": "json_schema"
  }
}
```

### 厳格なツールの使用
<a name="structured-output-strict-tool-examples"></a>

次の例は、ツールで strict フィールドを使用する方法を示しています。

#### Converse API
<a name="strict-tool-converse"></a>

##### 例を表示する
<a name="w2aac15c32c35c13b5b5b3b1"></a>

```
{
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "text": "What's the weather like in New York?"
        }
      ]
    }
  ],
  "toolConfig": {
    "tools": [
      {
        "toolSpec": {
          "name": "get_weather",
          "description": "Get the current weather for a specified location",
          "strict": true,
          "inputSchema": {
            "json": {
              "type": "object",
              "properties": {
                "location": {
                  "type": "string",
                  "description": "The city and state, e.g. San Francisco, CA"
                },
                "unit": {
                  "type": "string",
                  "enum": [
                    "fahrenheit",
                    "celsius"
                  ],
                  "description": "The temperature unit to use"
                }
              },
              "required": [
                "location",
                "unit"
              ]
            }
          }
        }
      }
    ]
  }
}
```

#### InvokeModel (Anthropic Claude)
<a name="strict-tool-invokemodel-claude"></a>

##### 例を表示する
<a name="w2aac15c32c35c13b5b7b3b1"></a>

```
{
  "anthropic_version": "bedrock-2023-05-31",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "What's the weather like in San Francisco?"
        }
      ]
    }
  ],
  "max_tokens": 3000,
  "temperature": 1.0,
  "tools": [
    {
      "name": "get_weather",
      "description": "Get the current weather for a specified location",
      "strict": true,
      "input_schema": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA"
          },
          "unit": {
            "type": "string",
            "enum": [
              "fahrenheit",
              "celsius"
            ],
            "description": "The temperature unit to use"
          }
        },
        "required": [
          "location",
          "unit"
        ],
        "additionalProperties": false
      }
    }
  ]
}
```

#### InvokeModel (オープンウェイトモデル)
<a name="strict-tool-invokemodel-openweight"></a>

##### 例を表示する
<a name="w2aac15c32c35c13b5b9b3b1"></a>

```
{
  "messages": [
    {
      "role": "user",
      "content": "What's the weather like in San Francisco?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Get the current weather for a specified location",
        "strict": true,
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
              "type": "string",
              "enum": [
                "fahrenheit",
                "celsius"
              ],
              "description": "The temperature unit to use"
            }
          },
          "required": [
            "location",
            "unit"
          ]
        }
      }
    }
  ],
  "tool_choice": "auto",
  "max_tokens": 2000,
  "temperature": 1.0
}
```