> ## 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.

# Tool

> Tool クラスは、1 回のツール実行を execute_tool スパンとして記録します。

```python theme={"system"}
class Tool(BaseModel): ...
```

1 回のツール実行を表します。execute\_tool OTel スパンに対応します。

`arguments` と `result` は `JSONString` アノテーションを使用します。
呼び出し元は dict / list / スカラー値を代入でき、SDK が構築時または代入時に
JSON エンコードします。格納される値は常に
string であり、GenAI semconv で定められたワイヤー形式と一致します。

`Tool` は Pydantic モデルです。構築時には、以下の任意のフィールドをキーワード引数として指定できます。

`Tool` はコンテキストマネージャーであり、`with` ステートメントで使用できます。

<h2 id="fields">
  フィールド
</h2>

| フィールド | タイプ | デフォルト |
| - | - | - |
| `name` | `str` | `''` |
| `arguments` | `JSONString` | `''` |
| `result` | `JSONString` | `''` |
| `tool_call_id` | `str` | `''` |
| `tool_type` | `str` | `''` |
| `tool_description` | `str` | `''` |
| `tool_definitions` | `str` | `''` |
| `duration_ms` | `int` | `0` |
| `started_at` | `Union[datetime, None]` | `None` |
| `ended_at` | `Union[datetime, None]` | `None` |

<h2 id="methods">
  メソッド
</h2>

<h3 id="end">
  end
</h3>

```python theme={"system"}
def end() -> None: ...
```

<h3 id="start">
  start
</h3>

```python theme={"system"}
def start() -> Self: ...
```

このスパンを一度だけ開始します。コンテキストマネージャーへのエントリ時も同じ処理経路を使用します。

<h3 id="record_error">
  record\_error
</h3>

```python theme={"system"}
def record_error(error: BaseException) -> Self: ...
```

スパンを終了せずにエラーを記録します。スパンを終了する準備ができたら `end()` を呼び出してください。

<h3 id="set_attributes">
  set\_attributes
</h3>

```python theme={"system"}
def set_attributes(attributes: dict[str, Any]) -> Self: ...
```

このスパンに任意の OTel 属性を付与します。

キーが 1 つでも複数でも dict を渡してください。キーが 1 つの場合は
`span.set_attributes({"weave.tag": "value"})` のように呼び出します。OTel の
`Span.set_attributes` と同じ動作です。

`start_*` ファクトリ、`start()`、または `with` によってスパンが開始された後、
スパンが終了するまでの間に呼び出す必要があります。この期間外に呼び出した場合は
no-op となり、警告がログされます。バッチ取り込みの場合は、オブジェクトの宣言済みフィールドに
直接値を設定し、`log_turn` / `log_conversation` に渡してください。
