バックグラウンド実行

Deep Research、複雑な推論、複数ステップのエージェント実行などの長時間実行タスクの場合、接続タイムアウトにより標準の HTTP リクエスト(通常は 60 秒後に終了)が中断されることがあります。Interactions API は、これらのタスクを非同期で実行するためのバックグラウンド実行を提供します。

インタラクションがサーバーでタスクを完了するまで実行されるようにするには、インタラクションの作成時に "background": true を設定します。API はインタラクション ID をすぐに返します。クライアント アプリケーションは、この ID を使用してステータスのポーリング、進行状況のストリーミング、切断されたストリームへの再接続を行うことができます。

バックグラウンド実行は、標準の Gemini モデル(gemini-3.5-flashgemini-3.1-pro-preview など)とマネージド エージェント(antigravity-preview-05-2026 など)でサポートされています。

バックグラウンド インタラクションを作成する

バックグラウンド インタラクションを開始するには、リソースの作成時に background パラメータを true に設定します。

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.5-flash",
    input="Write a guide on space exploration.",
    background=True,
)
print(f"Created background interaction ID: {interaction.id}")

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    model: "gemini-3.5-flash",
    input: "Write a guide on space exploration.",
    background: true,
});
console.log(`Created background interaction ID: ${interaction.id}`);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Api-Revision: 2026-05-20" \
  -d '{
    "model": "gemini-3.5-flash",
    "input": "Write a guide on space exploration.",
    "background": true
  }'

バックグラウンド実行の仕組み

バックグラウンド インタラクションを作成すると、タスクはサーバーで非同期的に実行されます。インタラクションは、さまざまな実行状態に移行します。

  • in_progress: サーバーがインタラクションをアクティブに実行しています(コードの実行や調査など)。
  • requires_action: インタラクションが一時停止し、クライアントからの入力(ツールの実行の確認や質問への回答など)を待機しています。
  • completed: インタラクションが正常に終了し、出力が利用可能になりました。
  • failed: 実行中にエラーが発生しました(ツールの失敗やレート制限など)。
  • cancelled: クライアント リクエストによって実行が停止しました。

ユースケース

バックグラウンド実行は、次の目的で使用します。

  • エージェントの実行: コード実行、ウェブの閲覧、サブエージェントのオーケストレーション(antigravity-preview-05-2026 など)を必要とするタスク。

  • Deep Research: deep-research-preview-04-2026 または deep-research-max-preview-04-2026 を使用して実行します。これには数分かかります。

  • 長い推論: モデルの思考ステップが標準の HTTP 接続制限を超えるタスク。

結果を取得する

バックグラウンド インタラクションの結果は、ポーリング またはストリーミング を使用して取得します。

ポーリング パターン(非ブロッキング)

ポーリングは、終了状態に達するまで、非ブロッキング GET リクエストを使用してインタラクションのステータスを定期的に確認します。

Python

import time
from google import genai

client = genai.Client()

interaction = client.interactions.get(id="YOUR_INTERACTION_ID")

while interaction.status == "in_progress":
    time.sleep(5)
    interaction = client.interactions.get(id=interaction.id)

if interaction.status == "completed":
    print(interaction.output_text)
else:
    print(f"Finished with status: {interaction.status}")

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

let interaction = await client.interactions.get("YOUR_INTERACTION_ID");

while (interaction.status === "in_progress") {
    await new Promise(resolve => setTimeout(resolve, 5000));
    interaction = await client.interactions.get(interaction.id);
}

if (interaction.status === "completed") {
    console.log(interaction.output_text);
} else {
    console.log(`Finished with status: ${interaction.status}`);
}

REST

curl -X GET "https://generativelanguage.googleapis.com/v1beta/interactions/YOUR_INTERACTION_ID" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Api-Revision: 2026-05-20"

ストリーミング パターン

ネットワークの中断によってストリームが切断された場合、ストリーミングは最後に受信したイベントから再開できます。各デルタのペイロードには一意の event_id が含まれています。この ID を last_event_id として渡すと、そのイベントからストリームが再開されます。

Python

import time
from google import genai

client = genai.Client()
interaction_id = "YOUR_INTERACTION_ID"

def stream_with_reconnect(interaction_id: str):
    last_event_id = None
    while True:
        try:
            # Retrieve the stream. If resuming, pass last_event_id
            stream = client.interactions.get(
                id=interaction_id,
                stream=True,
                last_event_id=last_event_id
            )

            for event in stream:
                # Log event updates and capture event_id if present
                if event.event_id:
                    last_event_id = event.event_id

                if event.event_type == "step.delta" and event.delta.type == "text":
                    print(event.delta.text, end="", flush=True)

                if event.event_type == "interaction.completed":
                    return

        except Exception as e:
            print(f"\n[Connection lost: {e}. Reconnecting in 3s...]")
            time.sleep(3)

stream_with_reconnect(interaction_id)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});
const interactionId = "YOUR_INTERACTION_ID";

async function streamWithReconnect(id) {
    let lastEventId = undefined;
    while (true) {
        try {
            // Retrieve the stream. If resuming, pass last_event_id in options
            const stream = await client.interactions.get(id, {
                stream: true,
                last_event_id: lastEventId
            });

            for await (const event of stream) {
                // Capture event_id if present
                const idVal = event.event_id || event.id;
                if (idVal) {
                    lastEventId = idVal;
                }

                if (event.event_type === "step.delta" && event.delta?.type === "text") {
                    process.stdout.write(event.delta.text);
                }

                if (event.event_type === "interaction.completed") {
                    return;
                }
            }
        } catch (error) {
            console.log(`\n[Connection lost: ${error.message}. Reconnecting in 3s...]`);
            await new Promise(resolve => setTimeout(resolve, 3000));
        }
    }
}

await streamWithReconnect(interactionId);

REST

curl -N -X GET "https://generativelanguage.googleapis.com/v1beta/interactions/YOUR_INTERACTION_ID?stream=true&last_event_id=YOUR_LAST_EVENT_ID" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Api-Revision: 2026-05-20"

マルチターンの会話

後続のインタラクションは、次の制約に従って previous_interaction_id を使用してバックグラウンド会話にチェーンできます。

  1. アクティブな実行はブロックされます: 後続のインタラクションを in_progress ステータスのインタラクションにチェーンすると、400 Bad Request エラーが返されます。次のインタラクションを開始する前に、インタラクションが completed 状態になるまで待ちます。
  2. マネージド エージェントの環境パラメータ: マネージド エージェント(antigravity-preview-05-2026 など)のインタラクションをチェーンする場合、リクエストには previous_interaction_idenvironment の両方を含める必要があります。

インタラクションをチェーンする方法の例を次に示します。

Python

import time
from google import genai

client = genai.Client()
agent_model = "antigravity-preview-05-2026"

# First interaction: Provision sandbox environment and execute first instruction
interaction1 = client.interactions.create(
    model=agent_model,
    input="Create a folder named project/ and write hello.py inside.",
    environment="remote",
    background=True
)

# Wait for completion
while True:
    check = client.interactions.get(id=interaction1.id)
    if check.status != "in_progress":
        break
    time.sleep(2)

# Second interaction: Chain using previous_interaction_id and environment
interaction2 = client.interactions.create(
    model=agent_model,
    input="List all files in the project/ directory.",
    previous_interaction_id=interaction1.id,
    environment="remote",
    background=True
)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});
const agentModel = "antigravity-preview-05-2026";

// First interaction: Provision sandbox environment and execute first instruction
const interaction1 = await client.interactions.create({
    model: agentModel,
    input: "Create a folder named project/ and write hello.py inside.",
    environment: "remote",
    background: true
});

// Wait for completion
while (true) {
    const check = await client.interactions.get(interaction1.id);
    if (check.status !== "in_progress") {
        break;
    }
    await new Promise(resolve => setTimeout(resolve, 2000));
}

// Second interaction: Chain using previous_interaction_id and environment
const interaction2 = await client.interactions.create({
    model: agentModel,
    input: "List all files in the project/ directory.",
    previous_interaction_id: interaction1.id,
    environment: "remote",
    background: true
});

REST

# Chain second interaction (Make sure FIRST_INTERACTION_ID has status 'completed')
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Api-Revision: 2026-05-20" \
  -d '{
    "model": "antigravity-preview-05-2026",
    "input": "List all files in the project/ directory.",
    "previous_interaction_id": "FIRST_INTERACTION_ID",
    "environment": "remote",
    "background": true
  }'

キャンセルと削除

実行中の実行を制御し、キャンセル リクエストと削除リクエストを使用してストレージを管理します。

  • キャンセル(POST /interactions/{id}/cancel): 実行中のタスクを停止します。ステータスが cancelled に移行します。サーバーでのクリーンアップ アクションにより、GET リクエストでステータスが更新されるまでに若干の遅延が生じることがあります。
  • 削除(DELETE /interactions/{id}): サーバーからインタラクション レコードを削除します。後続の GET リクエストは 404 Not Found エラーを返します。

Python

from google import genai

client = genai.Client()

# Cancel a running interaction
client.interactions.cancel(id="YOUR_INTERACTION_ID")

# Delete the interaction record entirely
client.interactions.delete(id="YOUR_INTERACTION_ID")

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

// Cancel a running interaction
await client.interactions.cancel("YOUR_INTERACTION_ID");

// Delete the interaction record entirely
await client.interactions.delete("YOUR_INTERACTION_ID");

REST

# Cancel the interaction
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions/YOUR_INTERACTION_ID/cancel" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Api-Revision: 2026-05-20"

# Delete the interaction
curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/interactions/YOUR_INTERACTION_ID" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Api-Revision: 2026-05-20"

次のステップ