Gemini 3.1 ProのAPIを使えば、高度な推論・長文処理・マルチモーダル機能を自分のアプリやツールに組み込めます。この記事では、APIキーの取得からPython・JavaScriptでの実装例、Thinking Levelの活用まで、実践的に解説します。
APIキーの取得方法
- Google AI StudioにアクセスしてGoogleアカウントでログイン
- 左メニューから「Get API key」→「Create API key」をクリック
- 表示されたAPIキーをコピーして安全な場所に保管
APIキーは無料で取得でき、無料枠(1日1,500リクエスト・1分60リクエスト)内なら費用はかかりません。
Pythonでの基本的な使い方
ライブラリのインストール
pip install google-genai
※ Gen AI SDK for Python バージョン1.51.0以降が必要です。
テキスト生成(基本)
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
response = client.models.generate_content(
model="gemini-3.1-pro-preview",
contents="日本の観光地トップ5を教えてください"
)
print(response.text)
チャット(会話履歴を保持)
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
chat = client.chats.create(model="gemini-3.1-pro-preview")
# 複数ターンの会話
response1 = chat.send_message("Pythonでウェブスクレイピングするには?")
print(response1.text)
response2 = chat.send_message("BeautifulSoupのサンプルコードも書いて")
print(response2.text)
ストリーミング出力
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
for chunk in client.models.generate_content_stream(
model="gemini-3.1-pro-preview",
contents="AIの未来について長めに教えてください"
):
print(chunk.text, end="", flush=True)
画像・動画の入力(マルチモーダル)
ローカル画像の分析
from google import genai
from google.genai import types
import pathlib
client = genai.Client(api_key="YOUR_API_KEY")
# 画像ファイルを読み込み
image_path = pathlib.Path("photo.jpg")
response = client.models.generate_content(
model="gemini-3.1-pro-preview",
contents=[
types.Part.from_bytes(
data=image_path.read_bytes(),
mime_type="image/jpeg"
),
"この画像に何が写っていますか?詳しく説明してください"
]
)
print(response.text)
YouTubeの動画を分析
from google import genai
from google.genai import types
client = genai.Client(api_key="YOUR_API_KEY")
response = client.models.generate_content(
model="gemini-3.1-pro-preview",
contents=[
types.Part.from_uri(
file_uri="https://www.youtube.com/watch?v=XXXXXXXX",
mime_type="video/youtube"
),
"この動画の内容を3つの要点にまとめてください"
]
)
print(response.text)
Thinking Level(思考レベル)の設定
Gemini 3.1 Proの特徴的な機能がThinking Levelです。コストと精度のバランスを調整できます。
| レベル | 用途 | コスト |
|---|---|---|
| LOW | シンプルな質問・高速レスポンス | 低 |
| MEDIUM | バランス型(デフォルト) | 中 |
| HIGH | 複雑な推論・数学・コーディング | 高 |
from google import genai
from google.genai import types
client = genai.Client(api_key="YOUR_API_KEY")
response = client.models.generate_content(
model="gemini-3.1-pro-preview",
contents="この数学の証明問題を解いてください:...",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_level="HIGH"
)
)
)
print(response.text)
JavaScriptでの使い方
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });
async function main() {
const response = await ai.models.generateContent({
model: "gemini-3.1-pro-preview",
contents: "Reactのベストプラクティスを教えてください",
});
console.log(response.text);
}
main();
料金の計算方法
Gemini 3.1 ProのAPI料金はトークン数で計算します。
| 種類 | 料金(1Mトークンあたり) |
|---|---|
| 入力(128K以下) | $2.00 |
| 入力(128K超) | $4.00 |
| 出力(128K以下) | $12.00 |
| 出力(128K超) | $18.00 |
1,000文字の日本語テキストはおよそ800〜1,200トークンです。短い質問と回答であれば1回あたり$0.01以下に収まることがほとんどです。
よくあるエラーと対処法
429 Resource Exhausted(レート制限)
import time
from google.api_core.exceptions import ResourceExhausted
for attempt in range(3):
try:
response = client.models.generate_content(...)
break
except ResourceExhausted:
print(f"レート制限。{2**attempt}秒待機...")
time.sleep(2**attempt)
Safety Filter(安全フィルター)
回答が安全フィルターでブロックされた場合は、response.prompt_feedbackでブロック理由を確認できます。プロンプトを修正して再試行してください。
まとめ
Gemini 3.1 ProのAPIは、テキスト・画像・動画を扱えるマルチモーダルAPIです。Python・JavaScriptどちらでも数行のコードで使い始められ、Thinking LevelでコストとAIの思考深度を調整できます。
無料枠が充実しているため、個人プロジェクトや副業ツールの試作にも最適です。ぜひ活用してみてください。

