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

# マルチモーダル

> YouRouter でビジョン、画像入力、動画生成 API を扱います。

YouRouter はプレーンテキストのチャットに加えて、マルチモーダルなモデル呼び出しにも対応します。画像、視覚理解、プロバイダー固有のマルチモーダル形式、動画生成タスクが必要な場合はこのページを使います。

## どの API を使うべきか

| 用途                    | 推奨 API                                                                                      | メモ                                                                                      |
| --------------------- | ------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| テキストチャット              | `POST /v1/chat/completions`                                                                 | 標準的な OpenAI 互換呼び出し                                                                      |
| 画像理解                  | `POST /v1/chat/completions`                                                                 | `image_url` ブロックを送る                                                                     |
| PDF / 文書理解            | `POST /v1/chat/completions`、`POST /v1/projects/...:generateContent`、または `POST /v1/messages` | 対象モデルと上流形式に依存します。OpenAI 互換呼び出しでは `file` コンテンツブロック、Gemini と Claude ではプロバイダー原生の文書形式を使えます。 |
| Gemini ネイティブマルチモーダル   | `POST /v1/projects/...:generateContent`                                                     | Google ネイティブの `contents` / `parts` が必要な場合                                               |
| Claude ネイティブ Messages | `POST /v1/messages`                                                                         | Anthropic ネイティブの Messages が必要な場合                                                        |
| テキストから動画生成 / 画像から動画生成 | `POST /api/v3/contents/generations/tasks`                                                   | タスク作成 → ポーリング                                                                           |

<Note>
  多くのチャットとビジョン用途では、`https://api.yourouter.ai/v1` と OpenAI 互換の Chat Completions 形式から始めるのが最短です。
</Note>

## Chat Completions で画像入力する

`messages[].content` をブロック配列にします。通常は `text` と 1 つ以上の `image_url` を並べます。

```bash theme={null}
curl https://api.yourouter.ai/v1/chat/completions \
  -H "Authorization: Bearer $YOUROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "Describe this image in one sentence."
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://example.com/image.jpg"
            }
          }
        ]
      }
    ]
  }'
```

生成された回答は次から読み取ります。

```text theme={null}
choices[0].message.content
```

## Base64 画像入力

非公開画像は data URL を使います。

```json theme={null}
{
  "type": "image_url",
  "image_url": {
    "url": "data:image/png;base64,<BASE64_IMAGE>"
  }
}
```

ペイロードが大きくなりすぎないよう注意してください。大きい画像は一時的な HTTPS URL を推奨します。

## Python 例

```python theme={null}
import base64
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["YOUROUTER_API_KEY"],
    base_url="https://api.yourouter.ai/v1",
)

with open("image.jpg", "rb") as image_file:
    encoded = base64.b64encode(image_file.read()).decode("utf-8")

completion = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What is shown in this image?"},
                {
                    "type": "image_url",
                    "image_url": {"url": f"data:image/jpeg;base64,{encoded}"},
                },
            ],
        }
    ],
)

print(completion.choices[0].message.content)
```

## PDF / 文書入力

PDF の対応可否は対象モデルと上流プロバイダーに依存します。リクエストを送る前に、選択したモデルが文書または視覚理解に対応していることを確認してください。PDF 非対応のモデルでは上流エラーが返ります。

OpenAI 互換のファイルコンテンツブロックに対応するモデルでは、`messages[].content` に `file` ブロックを追加できます。`file_data` には PDF の生バイト列を base64 エンコードした値を入れます。`data:application/pdf;base64,` プレフィックスは付けません。

```bash theme={null}
curl https://api.yourouter.ai/v1/chat/completions \
  -H "Authorization: Bearer $YOUROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "file",
            "file": {
              "filename": "document.pdf",
              "file_data": "<BASE64_PDF>"
            }
          },
          {
            "type": "text",
            "text": "Summarize the key points in this PDF."
          }
        ]
      }
    ]
  }'
```

特定の上流プロバイダーの文書機能に依存する場合は、provider-native 形式を使います。

* Gemini: `inlineData.mimeType: "application/pdf"` を使います。詳細は [Google Generate Content](/ja/api-reference/google/generate-content) を参照してください。
* Claude: `media_type: "application/pdf"` を指定した `document` コンテンツブロックを使います。詳細は [Anthropic Messages](/ja/api-reference/messages/create) を参照してください。

<Warning>
  PDF は通常、より多くのコンテキストウィンドウとリクエスト本文サイズを消費します。本番環境ではファイルサイズとページ数を制限してください。大きいファイルは分割するか、対象上流のファイル API が連携で利用できることを確認してからアップロード型フローを使ってください。
</Warning>

## Gemini ネイティブマルチモーダル

Gemini ネイティブのリクエストフィールドに依存する連携では、Google の `generateContent` 形式を使います。

```bash theme={null}
curl https://api.yourouter.ai/v1/projects/cognition/locations/us/publishers/google/models/gemini-2.5-flash:generateContent \
  -H "Authorization: Bearer $YOUROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -H "vendor: google" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          { "text": "Describe this image in one sentence." },
          {
            "inlineData": {
              "mimeType": "image/jpeg",
              "data": "<BASE64_IMAGE>"
            }
          }
        ]
      }
    ]
  }'
```

参照: [Google Generate Content](/ja/api-reference/google/generate-content)

## Claude ネイティブ Messages

Claude ネイティブの挙動が必要な場合に使います。

```bash theme={null}
curl https://api.yourouter.ai/v1/messages \
  -H "Authorization: Bearer $YOUROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -H "vendor: anthropic" \
  -d '{
    "model": "claude-sonnet-4-20250514",
    "max_tokens": 300,
    "messages": [
      {
        "role": "user",
        "content": [
          { "type": "text", "text": "Describe this image in one sentence." },
          {
            "type": "image",
            "source": {
              "type": "base64",
              "media_type": "image/jpeg",
              "data": "<BASE64_IMAGE>"
            }
          }
        ]
      }
    ]
  }'
```

参照: [Anthropic Messages](/ja/api-reference/messages/create)

## 動画生成タスク

動画生成はタスク型です。タスクを作成し、完了するまでポーリングします。

```bash theme={null}
curl -X POST https://api.yourouter.ai/api/v3/contents/generations/tasks \
  -H "Authorization: Bearer $YOUROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "doubao-seedance-1-0-pro-250528",
    "content": [
      {
        "type": "text",
        "text": "A cinematic product shot with soft studio lighting --duration 5 --resolution 1080p"
      }
    ]
  }'
```

作成リクエストはタスク `id` を返します。そのタスクが完了するまで `id` で問い合わせます。

```bash theme={null}
curl https://api.yourouter.ai/api/v3/contents/generations/tasks/{id} \
  -H "Authorization: Bearer $YOUROUTER_API_KEY"
```

詳細は [Ark テキストから動画生成](/ja/guides/ark-video) を参照してください。

## 実装のコツ

* モデル ID は設定化しておくと、コード変更なしでビジョンモデルやマルチモーダルモデルを切り替えやすくなります。
* `vendor` はプロバイダー固有フォーマットや挙動が必要なときだけ使います。
* 大きいファイルでは HTTPS URL または分割処理を優先し、非公開/ローカル検証の画像や小さな PDF では base64 を使います。
* PDF 対応はモデルと上流能力によって変わります。公開前に対象モデルで実ファイルのテストを行ってください。
* プロバイダー固有のマルチモーダル問題を調査するときは、レスポンスのリクエスト ID を残します。
