> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 構造化出力を有効にする

> Serverless Inference の応答で構造化出力を設定する方法

構造化出力を使用すると、Serverless Inference の応答を特定の JSON スキーマに制約し、モデルの応答が定義したフィールドとタイプに確実に従うようにできます。データ抽出、フォーム入力、ダウンストリーム処理など、アプリケーションでモデルの応答を既知のフィールドにパースする必要がある場合は、構造化出力を使用してください。

構造化出力は [JSON モード](/ja/products/inference/serverless/response-settings/json-mode) に似ていますが、指定したスキーマへの準拠も強制される点が異なります。可能な場合は、JSON モードではなく構造化出力を使用してください。

構造化出力を有効にするには、リクエストで `response_format` のタイプとして `json_schema` を指定します。次の例では、イベントの詳細を定義済みのスキーマに沿って抽出します。

<Tabs>
  <Tab title="Python">
    ```python theme={"system"}
    import json
    import openai

    client = openai.OpenAI(
        base_url='https://api.inference.wandb.ai/v1',
        api_key="[YOUR-API-KEY]",  # https://forge.coreweave.com/settings でAPIキーを作成します
    )

    response = client.chat.completions.create(
        model="openai/gpt-oss-20b",
        messages=[
            {"role": "system", "content": "Extract the event information."},
            {"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
        ],
        response_format={
            "type": "json_schema",
            "json_schema": {
                "name": "CalendarEventResponse",
                "strict": True,
                "schema": {
                    "type": "object",
                    "properties": {
                        "name": {"type": "string"},
                        "date": {"type": "string"},
                        "participants": {"type": "array", "items": {"type": "string"}},
                    },
                    "required": ["name", "date", "participants"],
                    "additionalProperties": False,
                },
            },
        },
    )

    content = response.choices[0].message.content
    parsed = json.loads(content)
    print(parsed)
    ```
  </Tab>

  <Tab title="Bash">
    ```bash theme={"system"}
    curl https://api.inference.wandb.ai/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer [YOUR-API-KEY]" \
      -d '{
        "model": "openai/gpt-oss-20b",
        "messages": [
            {"role": "system", "content": "Extract the event information."},
            {"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
        ],
        "response_format": {
            "type": "json_schema",
            "json_schema": {
                "name": "CalendarEventResponse",
                "strict": True,
                "schema": {
                    "type": "object",
                    "properties": {
                        "name": {"type": "string"},
                        "date": {"type": "string"},
                        "participants": {"type": "array", "items": {"type": "string"}},
                    },
                    "required": ["name", "date", "participants"],
                    "additionalProperties": False,
                },
            },
        },
      }'
    ```
  </Tab>
</Tabs>
