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

# メッセージタイプ

> スパンに記録されるメッセージ、メッセージパート、トークン使用量、推論、メディア添付ファイル。

これらには `tracing` モジュールからアクセスします。

```python theme={"system"}
from coreweave.forge.agentlens import tracing
```

## メッセージ

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

会話内の 1 件のメッセージです。

次の 2 つの構築スタイルをサポートしています。

1. フラット形式 (後方互換性があり、プレーンテキストを扱いやすい形式) :
   `Message(role="assistant", content="Hi there")`

2. 明示的なパート形式 (より表現力が高く、ツール呼び出し、推論とテキストの混在、
   インラインメディアをサポートします) :
   `Message(role="assistant", parts=[TextPart(content="Let me check"), ToolCallPart(id="c1", name="get_weather", arguments='{...}')])`

`parts` が空でない場合は、それが正規の表現になります。空の場合は、
シリアライザーがフラットなフィールドから単一の TextPart (`role="tool"` の場合は
ToolCallResponsePart) を生成します。

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

**フィールド**

| フィールド | タイプ | デフォルト |
| - | - | - |
| `role` | `Literal['user', 'assistant', 'system', 'tool']` | 必須 |
| `content` | `str` | `''` |
| `tool_call_id` | `str` | `''` |
| `tool_name` | `str` | `''` |
| `parts` | `list[MessagePart]` | `[]` |

### Message.user

```python theme={"system"}
@classmethod
def user(text: str) -> Message: ...
```

プレーンテキストからユーザーメッセージを作成します。

### Message.system

```python theme={"system"}
@classmethod
def system(text: str) -> Message: ...
```

プレーンテキストからシステムメッセージを作成します。

### Message.assistant

```python theme={"system"}
@classmethod
def assistant(
    text: str = '',
    *,
    tool_calls: Union[list[ToolCallPart], None] = None,
) -> Message: ...
```

テキストとツール呼び出し (いずれもオプション) を含むアシスタント メッセージを作成します。

単純な応答にはプレーンテキストを使用します。アシスタントが 1 つ以上のツールを呼び出す場合は、
`tool_calls` を渡します。両方を指定した場合、テキストが先頭の `TextPart` として出力され、
その後に各 `ToolCallPart` が続きます。これにより、チャット ビューではこれらがインラインでレンダリングされます。

### Message.tool\_result

```python theme={"system"}
@classmethod
def tool_result(call_id: str, output: Any) -> Message: ...
```

以前にリクエストされたツール呼び出しに対する、ツール結果メッセージを作成します。

`output` には string、dict、list、スカラー値、または `None` を指定できます。
内部の `ToolCallResponsePart` は、string 以外の値を JSON エンコードします。

## MessagePart

```python theme={"system"}
MessagePart = Annotated[Union[TextPart, ReasoningPart, ToolCallPart, ToolCallResponsePart, BlobPart, UriPart, FilePart], Field(discriminator='type')]
```

## TextPart

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

プレーンテキストのコンテンツパートです。

`TextPart` は Pydantic モデルです。インスタンスを作成する際に、以下の任意のフィールドをキーワード引数として指定できます。

**フィールド**

| フィールド | タイプ | デフォルト |
| - | - | - |
| `type` | `Literal['text']` | `'text'` |
| `content` | `str` | `''` |

## ReasoningPart

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

推論 / 思考の連鎖を表すコンテンツパートです。

`ReasoningPart` は Pydantic モデルです。インスタンスの生成時に、以下の任意のフィールドをキーワード引数として指定できます。

**フィールド**

| フィールド | タイプ | デフォルト |
| - | - | - |
| `type` | `Literal['reasoning']` | `'reasoning'` |
| `content` | `str` | `''` |

## ToolCallPart

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

ツールの呼び出しを求めるアシスタントのリクエストです。

`arguments` は、通信上では string として扱われます (GenAI semconv に準拠) 。
`JSONString` アノテーションにより、呼び出し元は構築時または代入時に dict / list / スカラー値を
渡すことができ、SDK がそれを JSON エンコードします。
エンコード済みの string は、変更されずにそのまま渡されます。

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

**フィールド**

| フィールド | タイプ | デフォルト |
| - | - | - |
| `type` | `Literal['tool_call']` | `'tool_call'` |
| `id` | `str` | `''` |
| `name` | `str` | `''` |
| `arguments` | `JSONString` | `''` |

## ToolCallResponsePart

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

以前にリクエストされたツール呼び出しの実行結果です。

`response` は、ワイヤー上では文字列として送受信されます。`ToolCallPart.arguments` と同様に、`JSONString` として扱えます。

`ToolCallResponsePart` は Pydantic モデルです。インスタンスを作成する際に、以下の任意のフィールドをキーワード引数として指定できます。

**フィールド**

| フィールド | タイプ | デフォルト |
| - | - | - |
| `type` | `Literal['tool_call_response']` | `'tool_call_response'` |
| `id` | `str` | `''` |
| `response` | `JSONString` | `''` |

## BlobPart

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

インラインのバイナリコンテンツ (例: base64 エンコードされた画像やオーディオ) 。

`BlobPart` は Pydantic モデルです。インスタンスを作成する際に、以下の各フィールドをキーワード引数として指定できます。

**フィールド**

| フィールド | タイプ | デフォルト |
| - | - | - |
| `type` | `Literal['blob']` | `'blob'` |
| `mime_type` | `str` | `''` |
| `modality` | `str` | `''` |
| `content` | `str` | `''` |

## UriPart

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

URI で指定するメディアコンテンツへの参照です。

`UriPart` は Pydantic モデルです。インスタンスを作成する際に、以下の各フィールドをキーワード引数として指定できます。

**フィールド**

| フィールド | タイプ | デフォルト |
| - | - | - |
| `type` | `Literal['uri']` | `'uri'` |
| `mime_type` | `str` | `''` |
| `modality` | `str` | `''` |
| `uri` | `str` | `''` |

## FilePart

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

プロバイダー側のファイル ID でメディアコンテンツを参照します。

`FilePart` は Pydantic モデルです。インスタンスを作成する際に、以下の各フィールドをキーワード引数として指定できます。

**フィールド**

| フィールド | タイプ | デフォルト |
| - | - | - |
| `type` | `Literal['file']` | `'file'` |
| `mime_type` | `str` | `''` |
| `modality` | `str` | `''` |
| `file_id` | `str` | `''` |

## 使用方法

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

LLM Call のトークン使用量です。

`Usage` は Pydantic モデルです。インスタンスを生成する際に、以下の各フィールドをキーワード引数として指定できます。

**フィールド**

| フィールド | タイプ | デフォルト |
| - | - | - |
| `input_tokens` | `int` | `0` |
| `output_tokens` | `int` | `0` |
| `total_tokens` | `Union[int, None]` | `None` |
| `reasoning_tokens` | `int` | `0` |
| `cache_creation_input_tokens` | `int` | `0` |
| `cache_read_input_tokens` | `int` | `0` |

## 推論

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

LLM Call から得られる推論 / 思考の連鎖のコンテンツです。

`Reasoning` は Pydantic モデルです。インスタンスを作成する際に、以下の任意のフィールドをキーワード引数として指定できます。

**フィールド**

| フィールド | タイプ | デフォルト |
| - | - | - |
| `content` | `str` | `''` |

## MediaAttachment

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

OTLP GenAI の blob、URI、またはプロバイダーの file パートとして出力されるメディアです。

`MediaAttachment` は Pydantic モデルです。インスタンスを作成する際に、以下の各フィールドをキーワード引数として指定できます。

**フィールド**

| フィールド | タイプ | デフォルト |
| - | - | - |
| `content` | `str` | `''` |
| `uri` | `str` | `''` |
| `file_id` | `str` | `''` |
| `modality` | `str` | `''` |
| `mime_type` | `str` | `''` |
