Skip to content
ガイド

ツール呼び出しと構造化出力 ​

厳格な JSON Schema のツール定義とスキーマ準拠が保証された構造化レスポンスを用いて、LLM を社内 API、データベース、外部ワークフローと安全に接続できます。

関数/ツール呼び出しのループ ​

  1. ユーザーメッセージとあわせて tools(JSON Schema によるパラメーター定義)を送信します。
  2. message.tool_calls(Anthropic Messages の場合は tool_use ブロック)を確認します。
  3. アプリケーション側で関数をローカル実行し、tool_call_id を紐づけた role: "tool" の結果をメッセージ配列に追加します。
  4. 更新したメッセージ配列を再送信し、ツール結果に基づいた最終回答を取得します。
python
import json, os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["VANIX_API_KEY"],
    base_url="https://api.vanix.io/v1",
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_order_status",
            "description": "Retrieve live shipment status by order ID.",
            "parameters": {
                "type": "object",
                "properties": {
                    "order_id": {"type": "string", "description": "Order ID such as ORD-9042"}
                },
                "required": ["order_id"],
                "additionalProperties": False,
            },
            "strict": True,
        },
    }
]

resp = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Where is order ORD-9042?"}],
    tools=tools,
    tool_choice="auto",
)

for call in resp.choices[0].message.tool_calls or []:
    print(call.function.name, json.loads(call.function.arguments))

厳格な構造化出力(json_schema) ​

壊れやすい正規表現によるパースに頼ることなく、スキーマに厳密に準拠した JSON 出力を保証できます。

bash
curl https://api.vanix.io/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $VANIX_API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {"role": "user", "content": "Classify ticket: Billing invoice PDF returns 403 after login."}
    ],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "ticket_triage",
        "strict": true,
        "schema": {
          "type": "object",
          "properties": {
            "category": {"type": "string", "enum": ["billing", "auth", "api", "other"]},
            "severity": {"type": "string", "enum": ["p1", "p2", "p3"]},
            "summary": {"type": "string"}
          },
          "required": ["category", "severity", "summary"],
          "additionalProperties": false
        }
      }
    }
  }'

Strict モードのチェックリスト

OpenAI スキーマで "strict": true を有効にする場合、すべてのオブジェクト階層で "additionalProperties": false を宣言し、すべてのキーを "required" に列挙する必要があります。

Vanix AI 統合モデルゲートウェイ · https://api.vanix.io