メインコンテンツへスキップ
POST
/
v1beta
/
models
/
{model}
:
{operator}
import os
from google import genai

client = genai.Client(
    api_key=os.environ["COMETAPI_KEY"],
    http_options={"api_version": "v1beta", "base_url": "https://api.cometapi.com"},
)

response = client.models.generate_content(
    model="gemini-3-flash-preview",
    contents="Explain how AI works in a few words",
)

print(response.text)
{
  "candidates": [
    {
      "content": {
        "role": "<string>",
        "parts": [
          {
            "text": "<string>",
            "functionCall": {
              "name": "<string>",
              "args": {}
            },
            "inlineData": {
              "mimeType": "<string>",
              "data": "<string>"
            },
            "thought": true
          }
        ]
      },
      "safetyRatings": [
        {
          "category": "<string>",
          "probability": "<string>",
          "blocked": true
        }
      ],
      "citationMetadata": {
        "citationSources": [
          {
            "startIndex": 123,
            "endIndex": 123,
            "uri": "<string>",
            "license": "<string>"
          }
        ]
      },
      "tokenCount": 123,
      "avgLogprobs": 123,
      "groundingMetadata": {
        "groundingChunks": [
          {
            "web": {
              "uri": "<string>",
              "title": "<string>"
            }
          }
        ],
        "groundingSupports": [
          {
            "groundingChunkIndices": [
              123
            ],
            "confidenceScores": [
              123
            ],
            "segment": {
              "startIndex": 123,
              "endIndex": 123,
              "text": "<string>"
            }
          }
        ],
        "webSearchQueries": [
          "<string>"
        ]
      },
      "index": 123
    }
  ],
  "promptFeedback": {
    "safetyRatings": [
      {
        "category": "<string>",
        "probability": "<string>",
        "blocked": true
      }
    ]
  },
  "usageMetadata": {
    "promptTokenCount": 123,
    "candidatesTokenCount": 123,
    "totalTokenCount": 123,
    "trafficType": "<string>",
    "thoughtsTokenCount": 123,
    "promptTokensDetails": [
      {
        "modality": "<string>",
        "tokenCount": 123
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "<string>",
        "tokenCount": 123
      }
    ]
  },
  "modelVersion": "<string>",
  "createTime": "<string>",
  "responseId": "<string>"
}
CometAPI は Gemini ネイティブ API 形式をサポートしており、thinking control、Google Search grounding、ネイティブな画像生成 modality など、Gemini 固有の機能をフルに利用できます。OpenAI 互換チャットエンドポイント では利用できない機能が必要な場合は、このエンドポイントを使用してください。
認証には x-goog-api-keyAuthorization: Bearer の両方のヘッダーを利用できます。

クイックスタート

CometAPI で任意の Gemini SDK または HTTP クライアントを使うには、ベース URL と API キーを置き換えてください。
SettingGoogle DefaultCometAPI
ベース URLgenerativelanguage.googleapis.comapi.cometapi.com
API キー$GEMINI_API_KEY$COMETAPI_KEY

動画入力を送信する

Gemini generateContent は content part として動画を受け付けます。動画の保存場所に応じて入力形式を選択してください。
Video sourceRequest partUse when
ローカル動画ファイルinlineData動画が十分に小さく、JSON リクエスト内で base64 として送信できる場合。
公開動画 URLfileData.fileUri動画が、認証を必要としない公開 HTTPS URL から利用可能な場合。
REST および curl リクエストでは、inlineData.mimeTypefileData.fileUri のような Gemini の camelCase フィールド名を使用してください。URL メディアを file_data.file_uri として送信しないでください。
この例では、ローカルの MP4 ファイルを読み込み、base64 にエンコードして、リクエスト本文で送信します。
read -rsp "CometAPI API key: " COMETAPI_KEY
printf '\n'
export COMETAPI_KEY
VIDEO_PATH="./your_video.mp4"
VIDEO_B64=$(base64 < "$VIDEO_PATH" | tr -d '\n')

curl -X POST \
  "https://api.cometapi.com/v1beta/models/gemini-3.5-flash:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  --data-binary @- <<EOF
{
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "inlineData": {
            "mimeType": "video/mp4",
            "data": "${VIDEO_B64}"
          }
        },
        {
          "text": "Analyze this video and list the key scenes."
        }
      ]
    }
  ],
  "generationConfig": {
    "maxOutputTokens": 512,
    "thinkingConfig": {"thinkingLevel": "MINIMAL"}
  }
}
EOF
この例では、fileData.fileUri を使って公開 MP4 URL を送信します。
read -rsp "CometAPI API key: " COMETAPI_KEY
printf '\n'
export COMETAPI_KEY
VIDEO_URL="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4"

curl -X POST \
  "https://api.cometapi.com/v1beta/models/gemini-3.5-flash:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  --data-binary @- <<EOF
{
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "fileData": {
            "mimeType": "video/mp4",
            "fileUri": "${VIDEO_URL}"
          }
        },
        {
          "text": "Analyze this video and list the key scenes."
        }
      ]
    }
  ],
  "generationConfig": {
    "maxOutputTokens": 512,
    "thinkingConfig": {"thinkingLevel": "MINIMAL"}
  }
}
EOF
CometAPI では、このエンドポイントに対して Gemini Files API の個別アップロードフローは推奨していません。inlineData または fileData.fileUri を使って、メディアを generateContent リクエスト自体に含めて送信してください。

思考(reasoning)を設定する

Gemini モデルは、レスポンスを生成する前に内部的な推論を実行できます。制御方法はモデル世代によって異なります。
Gemini 3 モデルでは、推論の深さを制御するために thinkingLevel を使用します。使用可能なレベルは MINIMALLOWMEDIUMHIGH です。特別に別の Gemini 3 バリアントが必要でない限り、デフォルトの例示用モデルとして gemini-3-flash-preview を使用してください。
curl "https://api.cometapi.com/v1beta/models/gemini-3-flash-preview:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  -d '{
    "contents": [{"parts": [{"text": "Explain quantum physics simply."}]}],
    "generationConfig": {
      "thinkingConfig": {"thinkingLevel": "LOW"}
    }
  }'
Gemini 2.5 モデルで thinkingLevel を使用したり、Gemini 3 モデルで thinkingBudget を使用したりすると、エラーが発生する可能性があります。モデルのバージョンに合った正しいパラメータを使用してください。

レスポンスをストリーミングする

モデルがコンテンツを生成する際に Server-Sent Events を受け取るには、オペレータとして streamGenerateContent?alt=sse を使用します。各 SSE イベントには、JSON の GenerateContentResponse オブジェクトを含む data: 行が含まれます。
curl "https://api.cometapi.com/v1beta/models/gemini-3-flash-preview:streamGenerateContent?alt=sse" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  --no-buffer \
  -d '{
    "contents": [{"parts": [{"text": "Write a short poem about the stars"}]}]
  }'

システム指示を設定する

会話全体を通してモデルの挙動を誘導するには、systemInstruction を使用します。
curl "https://api.cometapi.com/v1beta/models/gemini-3-flash-preview:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  -d '{
    "contents": [{"parts": [{"text": "What is 2+2?"}]}],
    "systemInstruction": {
      "parts": [{"text": "You are a math tutor. Always show your work."}]
    }
  }'

JSON 出力をリクエストする

構造化された JSON 出力を強制するには、responseMimeType を設定します。必要に応じて、厳密なスキーマ検証のために responseSchema を指定することもできます。
curl "https://api.cometapi.com/v1beta/models/gemini-3-flash-preview:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  -d '{
    "contents": [{"parts": [{"text": "List 3 planets with their distances from the sun"}]}],
    "generationConfig": {
      "responseMimeType": "application/json"
    }
  }'

Google Search でグラウンディングする

リアルタイムの Web 検索を有効にするには、googleSearch ツールを追加します。
curl "https://api.cometapi.com/v1beta/models/gemini-3-flash-preview:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  -d '{
    "contents": [{"parts": [{"text": "Who won the euro 2024?"}]}],
    "tools": [{"google_search": {}}]
  }'
レスポンスには、ソース URL と信頼度スコアを含む groundingMetadata が含まれます。

レスポンス例

CometAPI の Gemini エンドポイントからの典型的なレスポンス:
{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [{"text": "Hello"}]
      },
      "finishReason": "STOP",
      "avgLogprobs": -0.0023
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 5,
    "candidatesTokenCount": 1,
    "totalTokenCount": 30,
    "trafficType": "ON_DEMAND",
    "thoughtsTokenCount": 24,
    "promptTokensDetails": [{"modality": "TEXT", "tokenCount": 5}],
    "candidatesTokensDetails": [{"modality": "TEXT", "tokenCount": 1}]
  },
  "modelVersion": "gemini-3-flash-preview",
  "createTime": "2026-03-25T04:21:43.756483Z",
  "responseId": "CeynaY3LDtvG4_UP0qaCuQY"
}
usageMetadata 内の thoughtsTokenCount フィールドは、思考出力がレスポンスに含まれていない場合でも、モデルが内部推論に費やしたトークン(Token)数を示します。

OpenAI互換エンドポイントとの比較

機能Gemini Native (/v1beta/models/...)OpenAI-Compatible (/v1/chat/completions)
Thinking の制御thinkingConfigthinkingLevel / thinkingBudget利用不可
Google Search グラウンディングtools: [\{"google_search": \{\}\}]利用不可
Google Maps グラウンディングtools: [\{"googleMaps": \{\}\}]利用不可
画像生成モダリティresponseModalities: ["IMAGE"]利用不可
認証ヘッダーx-goog-api-key または BearerBearer のみ
レスポンス形式Gemini ネイティブ(candidates, partsOpenAI 形式(choices, message

承認

x-goog-api-key
string
header
必須

Your CometAPI key passed via the x-goog-api-key header. Bearer token authentication (Authorization: Bearer $COMETAPI_KEY) is also supported.

パスパラメータ

model
string
必須

Gemini model ID. Example: gemini-3-flash-preview, gemini-2.5-pro. See the Models page for current options.

operator
enum<string>
必須

The operation to perform. Use generateContent for synchronous responses, or streamGenerateContent?alt=sse for Server-Sent Events streaming.

利用可能なオプション:
generateContent,
streamGenerateContent?alt=sse

ボディ

application/json
contents
object[]
systemInstruction
object

System instructions that guide the model's behavior across the entire conversation. Text only.

tools
object[]

Tools the model may use to generate responses. Supports function declarations, Google Search, Google Maps, and code execution.

toolConfig
object

Configuration for tool usage, such as function calling mode.

safetySettings
object[]

Safety filter settings. Override default thresholds for specific harm categories.

generationConfig
object

Configuration for model generation behavior including temperature, output length, and response format.

cachedContent
string

The name of cached content to use as context. Format: cachedContents/{id}. See the Gemini context caching documentation for details.

レスポンス

200 - application/json

Successful response. For streaming requests, the response is a stream of SSE events, each containing a GenerateContentResponse JSON object prefixed with data:.

candidates
object[]

The generated response candidates.

promptFeedback
object

Feedback on the prompt, including safety blocking information.

usageMetadata
object

Token usage statistics for the request.

modelVersion
string

The model version that generated this response.

createTime
string

The timestamp when this response was created (ISO 8601 format).

responseId
string

Unique identifier for this response.