0Pricing
AI Agents · レッスン

ツールスキーマの定義(JSON Schema)

型、説明、enum、必須フィールドを含むツールパラメータのJSON Schema定義を記述します。

「ツールスキーマの定義(JSON Schema)」はCoddyKit上の無料AI Agentsレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAI Agents学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 AI Agentsコースには全4レッスンが含まれています。

ツールスキーマはJSON Schemaです

OpenAI、Anthropicなど、ほとんどのプロバイダーはツールパラメーターにJSON Schemaを使用します。

OpenAPI / Swaggerを使ったことがあれば、この内容の90%はすでに理解しています。

3つの必須フィールド

すべてのツール定義には、次の要素があります。

  1. name — 一意の識別子(snake_case)
  2. description — ツールの機能と使用するタイミング
  3. parameters — 入力値のJSON Schema

最小限のスキーマ

必須の文字列フィールドを1つ持つオブジェクトです。

schema = {
    'name': 'search_orders',
    'description': 'Find orders by customer email',
    'parameters': {
        'type': 'object',
        'properties': {
            'email': {
                'type': 'string',
                'description': 'Customer email address'
            }
        },
        'required': ['email']
    }
}
import json
print(json.dumps(schema, indent=2))

JSON Schemaの型

  • string — テキスト
  • integer、number — 数値
  • boolean — true/false
  • array — リスト(itemsも必要です)
  • object — 辞書(propertiesも必要です)

限定された集合には列挙型を使う

許可される値がちょうどN個ある場合は、enumを使用します。

unit_param = {
    'unit': {
        'type': 'string',
        'enum': ['C', 'F'],
        'description': 'Temperature unit'
    }
}
# The model will only ever output C or F
print(unit_param)
print("Allowed values:", unit_param['unit']['enum'])

配列パラメーター

リスト形式の入力には、itemsを設定します。

tags_param = {
    'tags': {
        'type': 'array',
        'items': {'type': 'string'},
        'description': 'List of tags to filter by'
    }
}
print(tags_param)

ネストしたオブジェクト

オブジェクトはネストできます。ただし、モデルの信頼性を保つため、スキーマは浅く(最大2〜3階層に)保ってください。

filter_param = {
    'filter': {
        'type': 'object',
        'properties': {
            'min_price': {'type': 'number'},
            'in_stock': {'type': 'boolean'}
        }
    }
}
print(filter_param)

descriptionフィールドは重要です

モデルはdescriptionに基づいてツールを選び、引数を埋めます。descriptionはAPIドキュメントと同じように扱ってください。

# Bad
bad = {'description': 'gets data'}

# Good
good = {'description': 'Fetch the most recent 50 orders for the given customer email. Returns order_id, status, total. Use this when the user asks about their order history or order status.'}
print("Bad description:", bad['description'])
print("Good description:", good['description'])

required配列

必須フィールドには明示的に印を付けます。モデルはこれらを必ず埋め、任意フィールドは必要な場合だけ埋めます。

tool_params = {
    'parameters': {
        'properties': {
            'city': {'type': 'string'},
            'unit': {'type': 'string', 'enum': ['C', 'F']}
        },
        'required': ['city']
    }
}
print(tool_params)
print("Required fields:", tool_params['parameters']['required'])

PydanticからJSON Schemaへ

Pydanticモデルからスキーマを自動生成できます。

from pydantic import BaseModel, Field

class SearchArgs(BaseModel):
    email: str = Field(description='Customer email')
    limit: int = Field(50, description='Max orders to return')

schema = SearchArgs.model_json_schema()

Strictモード(OpenAI Structured Outputs)

strict: trueとadditionalProperties: falseを追加すると、モデルの出力がスキーマに正確に一致することを保証できます。

tools = [{
    'type': 'function',
    'function': {
        'name': 'get_weather',
        'strict': True,
        'parameters': {
            'type': 'object',
            'properties': {'city': {'type': 'string'}},
            'required': ['city'],
            'additionalProperties': False
        }
    }
}]
import json
print(json.dumps(tools, indent=2))

descriptionの重要性

ツールのdescriptionは、なぜそれほど重要なのでしょうか。

まとめ

スキーマがモデルの動作を決めます。適切なdescription、限定された集合に対するenum、required配列、strictモードが、信頼性を高めるための鍵です。

よくある質問

「ツールスキーマの定義(JSON Schema)」レッスンは無料ですか?

はい。「ツールスキーマの定義(JSON Schema)」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、AI Agentsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 AI Agentsコースには全4レッスンが含まれています。

「ツールスキーマの定義(JSON Schema)」で何を学びますか?

型、説明、enum、必須フィールドを含むツールパラメータのJSON Schema定義を記述します。 ブラウザで直接実行するハンズオンコードでAI Agentsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

AI Agentsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのAI Agentsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「ツールスキーマの定義(JSON Schema)」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このAI Agentsレッスンでコードを書いて実行できますか?

はい。すべてのAI Agentsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Function Callingの仕組み
  2. ツールスキーマの定義(JSON Schema)
  3. 実行時のツール選択
  4. モデルへの結果の返却
← AI Agentsに戻る