2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gemini APIにAIエージェント向けの機能が登場

Last updated at Posted at 2025-12-15

はじめに

2025年12月、GoogleはGemini APIの新しいインターフェースであるInteractions APIをパブリックベータとして公開しました。

本記事では以下のGoogle公式のAPIドキュメントをもとに、どのような基本的な利用方法やどのような使い方が考えられるかを紹介します。

Interactions APIの主な特徴

  • サーバーサイド状態管理: 会話履歴をサーバー側で保持し、IDで参照可能
  • モデルとエージェントの統一インターフェース: Geminiモデルと専用エージェント(現在はDeep Researchのみ)を同じAPIで呼び出せる
  • バックグラウンド実行: 長時間タスクを非同期で実行できる
  • ストリーミング対応: レスポンスをリアルタイムで受信可能
  • リモートMCPサポート: Model Context Protocol(MCP)サーバーをツールとして直接呼び出せる

基本的な使い方

最もシンプルな例を見てみましょう。

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-2.5-flash",
    input="プログラミングに関する短いジョークを教えてください。"
)

print(interaction.outputs[-1].text)

inputには文字列を直接渡すことができ、結果はinteraction.outputsから取得します。

注意事項

Interactions APIは現在パブリックベータ版です。機能やスキーマは今後変更される可能性があります。本番環境では引き続きgenerateContentの使用が推奨されています。(https://ai.google.dev/gemini-api/docs/api-versions?hl=ja)

SDKのバージョン要件:

  • Python: google-genai パッケージ 1.55.0以降
  • JavaScript: @google/genai パッケージ 1.33.0以降

エージェントの利用

Interactions APIでは、Geminiモデルと専用エージェント(Deep Researchなど)をclient.interactions.creatという同じインターフェースで呼び出せます。

Deep Researchは、Googleが提供するリサーチ特化型のエージェントです。複雑な調査タスクを自律的に実行し、詳細なレポートを生成します。

さらにbackground=Trueとすることで、エージェントの処理がGoogleのクラウド上で実行されるようになり、ローカルのネットワークエラーや予期せぬエラーが起こっても処理は継続されます。
特にDeepResearchは処理が長いので、こういった機能はありがたいですね。

from google import genai

client = genai.Client()

# agent パラメータでエージェントを指定
interaction = client.interactions.create(
    agent="deep-research-pro-preview-12-2025",
    input="2024年から2025年にかけてのGoogle TPUの進化について調査してください。",
    background=True  # 長時間タスクのためバックグラウンド実行
)

print(f"リサーチ開始。Interaction ID: {interaction.id}")

background=Trueは現在agentを利用する場合にのみ対応しているので、注意してください。

3. サーバーサイドの状態管理でマルチターン会話を簡潔に

Interactions APIの最大の特徴は、会話の状態管理をサーバー側で行える点です。従来のgenerateContentでは、マルチターン会話を実装する際にクライアント側で会話履歴を管理する必要がありましたが、Interactions APIではサーバーサイドで会話履歴を管理してくれます。

従来の方法(ステートレス)

Interactions APIでも、従来通りクライアント側で会話履歴を管理する方法を使えます。

from google import genai

client = genai.Client()

# クライアント側で会話履歴を管理
conversation_history = [
    {"role": "user", "content": "スペインで最も大きい都市を3つ教えてください。"}
]

interaction1 = client.interactions.create(
    model="gemini-2.5-flash",
    input=conversation_history
)

print(f"Model: {interaction1.outputs[-1].text}")

# 履歴に追加
conversation_history.append({"role": "model", "content": interaction1.outputs[-1].text})
conversation_history.append({
    "role": "user",
    "content": "2番目の都市の有名な観光地は何ですか?"
})

# 履歴全体を再送信
interaction2 = client.interactions.create(
    model="gemini-2.5-flash",
    input=conversation_history
)

print(f"Model: {interaction2.outputs[-1].text}")

この方法は動作しますが、会話が長くなるにつれてリクエストサイズが増大し、履歴管理のコードも煩雑になります。

新しい方法(ステートフル)

Interactions APIでは、previous_interaction_idパラメータを使うことで、サーバー側に保存された会話履歴を参照できます。

from google import genai

client = genai.Client()

# 1. 最初のターン
interaction1 = client.interactions.create(
    model="gemini-2.5-flash",
    input="こんにちは、私の名前は田中です。"
)
print(f"Model: {interaction1.outputs[-1].text}")

# 2. 2回目のターン(previous_interaction_id で前の会話を参照)
interaction2 = client.interactions.create(
    model="gemini-2.5-flash",
    input="私の名前を覚えていますか?",
    previous_interaction_id=interaction1.id
)
print(f"Model: {interaction2.outputs[-1].text}")

クライアント側で履歴を管理する必要がなく、IDを渡すだけで会話を継続できます。

過去のインタラクションを取得する

保存されたインタラクションは、IDを指定して後から取得できます。

# インタラクションIDで過去の会話を取得
previous_interaction = client.interactions.get("<YOUR_INTERACTION_ID>")
print(previous_interaction)

データの保存と保持期間

デフォルトでは、すべてのインタラクションはサーバー側に保存されます(store=True)。保持期間は以下の通りです。

プラン 保持期間
有料プラン 55日間
無料枠 1日間

現在ベータ版であり、今後変更が見込まれるため、詳細な期間については以下をご確認ください。

保存を無効にしたい場合は、store=Falseを指定します。ただし、この場合はprevious_interaction_idによる会話継続やbackground=Trueによるバックグラウンド実行は使用できません。

# 保存を無効にする場合
interaction = client.interactions.create(
    model="gemini-2.5-flash",
    input="この会話は保存されません。",
    store=False
)

モデルとエージェントを組み合わせる

Web版のGeminiのように、同一の会話内で通常のモデル実行とエージェントを組み合わせることも可能です。たとえば、Deep Researchで情報収集を行い、その結果を通常のGeminiモデルで要約するというケースを作ってみましょう。

処理のステップ

  1. まずはDeepResearchをバックグラウンドで実行
  2. バックグラウンド実行のため、結果を待つ必要があるので、一定間隔でIDから現在のステータスを取得
  3. 結果が得られたら、通常のGeminiモデルで要約を作成(コンテキストとなるinteraction idを設定)
from google import genai
import time

client = genai.Client()

# 1. Deep Researchで調査
research = client.interactions.create(
    agent="deep-research-pro-preview-12-2025",
    input="最新のLLMベンチマーク結果について調査してください。",
    background=True
)

# 結果を待つ
while True:
    result = client.interactions.get(research.id)
    if result.status == "completed":
        break
    time.sleep(10)

# 2. 調査結果を通常のモデルで要約
summary = client.interactions.create(
    model="gemini-2.5-flash",
    input="以下の調査結果を3行で要約してください。",
    previous_interaction_id=research.id  # 調査結果を引き継ぐ
)

print(summary.outputs[-1].text)

このように、DeepResearchのエージェント単体だけではなく、組み合わせて利用することができます。
以下のように段階的なDeepResearchの実行も可能なため、深堀りの調査や分析が楽になりそうですね。
DeepResearchで情報収集(agent)→さらに必要な情報を抽出(agent)→DeepResearchで情報収集(agent)→…

組み込みツールとMCPの利用

Interactions APIでは、Googleが用意した組み込みツールや既存のMCP(Model Context Protocol)を利用することで、エージェントの幅を広げることができます。

組み込みツール

2025/12/15 現在、Geminiには以下の組み込みツールが用意されています。

ツール 説明
google_search Google検索によるグラウンディング
code_execution コード実行
url_context URLコンテキスト
from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-2.5-flash",
    input="2025年の流行語を教えてください",
    tools=[{"type": "google_search"}]
)

print(interaction.outputs[-1].text)

MCP

リモートMCPを使うことで、外部サーバーでホストされているツールをGemini APIから直接呼び出すことができます。

from google import genai

client = genai.Client()

mcp_server = {
    "type": "mcp_server",
    "name": "weather_service",
    "url": "https://your-mcp-server.example.com/mcp"
}

interaction = client.interactions.create(
    model="gemini-2.5-flash",
    input="今日のニューヨークの天気は?",
    tools=[mcp_server]
)

print(interaction.outputs[-1].text)

7. まとめ

Interactions APIは、GeminiやGeminiを使ったエージェントを同一のインタフェースで管理できる新しい機能です。
現在はパブリックベータのため、エージェントの種類もDeepResearchのみですが、なかなか面白い機能なので今後のアップデートに期待ですね。
GoogleはADK(Agent Development Kit)と呼ばれるエージェント開発キットをOSSで公開していますが、Interactions APIはより簡単にエージェントを呼び出して利用できる機能という印象です。
ちなみに、Interaction APIとADKは連携ができるので、こちらもどこかで紹介したいと思います。

いいねやストックがモチベーションに繋がるので、ぜひお願いします!

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?