gemini-interactions-api
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGemini Interactions API Skill
Gemini Interactions API 技能指南
This skill provides instructions for authenticating, connecting to, and utilizing the stateful, server-managed Gemini Interactions API on Gemini Enterprise Agent Platform.
The Interactions API is the modern, recommended way to execute Generative AI agent conversations, background research tasks, multi-turn chats, and structured, multi-step workflows.
[!IMPORTANT] CRITICAL: Unified SDK & Latest Models
- Unified SDK: Use the Google Gen AI SDK (
for Python,google-genai >= 2.0.0for JS/TS). Legacy SDKs like@google/genai >= 2.0.0,google-cloud-aiplatform, and@google-cloud/vertexaiare strictly unsupported for Interactions.google-generativeai- Latest Models Only: Use
,gemini-3.1-pro-preview,gemini-3.1-flash-lite,gemini-3-flash-preview, orgemini-2.5-pro. Refer to the latest model versions to check for new updates. Legacy models (gemini-2.5-flash,gemini-2.0-*) are deprecated and do not support interactions.gemini-1.5-*- Turn-Scoped Parameters: Parameters like
,tools, andsystem_instructionare turn-scoped. They MUST be passed with each interaction request.generation_config
本技能文档提供了在Gemini Enterprise Agent Platform上进行认证、连接以及使用有状态、服务器托管的Gemini Interactions API的操作说明。
Interactions API是执行生成式AI代理对话、后台研究任务、多轮聊天以及结构化多步骤工作流的现代化推荐方案。
[!IMPORTANT] 重要提示:统一SDK与最新模型
- 统一SDK:请使用Google Gen AI SDK(Python版本需**
,JS/TS版本需google-genai >= 2.0.0**)。旧版SDK如@google/genai >= 2.0.0、google-cloud-aiplatform和@google-cloud/vertexai完全不支持Interactions功能。google-generativeai- 仅使用最新模型:请使用
、gemini-3.1-pro-preview、gemini-3.1-flash-lite、gemini-3-flash-preview或gemini-2.5-pro。可参考最新模型版本查看更新信息。旧版模型(gemini-2.5-flash、gemini-2.0-*)已被弃用,不支持Interactions功能。gemini-1.5-*- 轮次作用域参数:
、tools和system_instruction等参数属于轮次作用域,必须随每次交互请求一并传递。generation_config
1. Authentication
1. 身份认证
Before running any code, ensure you are authenticated with Application Default Credentials (ADC) and have the necessary API enabled.
- Login:
bash
gcloud auth application-default login - Enable API (if not already enabled):
bash
gcloud services enable aiplatform.googleapis.com
在运行任何代码之前,请确保已通过Application Default Credentials(ADC)完成身份认证,并启用了所需的API。
- 登录:
bash
gcloud auth application-default login - 启用API(若尚未启用):
bash
gcloud services enable aiplatform.googleapis.com
2. Client Initialization
2. 客户端初始化
You can initialize the client using environment variables (recommended) or by passing explicit configuration parameters.
你可以使用环境变量(推荐方式)或传入显式配置参数来初始化客户端。
Option A: Environment Variables (Recommended)
选项A:环境变量(推荐)
Configure environment variables to let the SDK automatically resolve settings:
bash
export GOOGLE_GENAI_USE_ENTERPRISE=true
export GOOGLE_CLOUD_PROJECT="your-project-id"
export GOOGLE_CLOUD_LOCATION="global"配置环境变量,让SDK自动解析设置:
bash
export GOOGLE_GENAI_USE_ENTERPRISE=true
export GOOGLE_CLOUD_PROJECT="your-project-id"
export GOOGLE_CLOUD_LOCATION="global"Python
Python
python
from google import genaipython
from google import genaiThe SDK automatically picks up the environment variables
SDK会自动读取环境变量
client = genai.Client()
undefinedclient = genai.Client()
undefinedTypeScript/JavaScript
TypeScript/JavaScript
typescript
import { GoogleGenAI } from "@google/genai";
// The SDK automatically picks up the environment variables
const ai = new GoogleGenAI();typescript
import { GoogleGenAI } from "@google/genai";
// SDK会自动读取环境变量
const ai = new GoogleGenAI();Option B: Explicit Inline Parameters
选项B:显式内联参数
Alternatively, pass configuration values directly inside your code:
或者,直接在代码中传入配置值:
Python
Python
python
from google import genai
import google.auth
_, project_id = google.auth.default()
client = genai.Client(enterprise=True, project=project_id, location="global")python
from google import genai
import google.auth
_, project_id = google.auth.default()
client = genai.Client(enterprise=True, project=project_id, location="global")TypeScript/JavaScript
TypeScript/JavaScript
typescript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({
enterprise: {
project: "your-project-id",
location: "global"
}
});typescript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({
enterprise: {
project: "your-project-id",
location: "global"
}
});3. Core Interactions API Usage
3. Interactions API核心用法
Quick Start (Single-Turn)
快速入门(单轮对话)
Submit a single prompt and read the final text response. Under the modern schema, output content is retrieved from the list.
steps提交单个提示并读取最终文本响应。在现代架构下,输出内容从列表中获取。
stepsPython
Python
python
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="Explain serverless computing in one sentence."
)python
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="Explain serverless computing in one sentence."
)Output text is located under steps
输出文本位于steps下
print(interaction.steps[-1].content[0].text)
undefinedprint(interaction.steps[-1].content[0].text)
undefinedTypeScript/JavaScript
TypeScript/JavaScript
typescript
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Explain serverless computing in one sentence."
});
console.log(interaction.steps[interaction.steps.length - 1].content[0].text);typescript
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Explain serverless computing in one sentence."
});
console.log(interaction.steps[interaction.steps.length - 1].content[0].text);Stateful Conversation (Multi-Turn)
有状态对话(多轮对话)
Interactions are stateful by default. Store the conversation state in the cloud and reference it in the subsequent turn using .
previous_interaction_idInteractions默认是有状态的。将对话状态存储在云端,并在后续轮次中使用引用该状态。
previous_interaction_idPython
Python
python
undefinedpython
undefinedTurn 1: Introduce ourselves
第一轮:自我介绍
turn1 = client.interactions.create(
model="gemini-3-flash-preview",
input="Hi! My name is John. I am working on AI agents.",
store=True
)
print(f"Turn 1: {turn1.steps[-1].content[0].text}")
turn1 = client.interactions.create(
model="gemini-3-flash-preview",
input="Hi! My name is John. I am working on AI agents.",
store=True
)
print(f"Turn 1: {turn1.steps[-1].content[0].text}")
Turn 2: Refer back to the stored turn state
第二轮:引用已存储的对话状态
turn2 = client.interactions.create(
model="gemini-3-flash-preview",
input="What is my name?",
previous_interaction_id=turn1.id
)
print(f"Turn 2: {turn2.steps[-1].content[0].text}")
undefinedturn2 = client.interactions.create(
model="gemini-3-flash-preview",
input="What is my name?",
previous_interaction_id=turn1.id
)
print(f"Turn 2: {turn2.steps[-1].content[0].text}")
undefinedTypeScript/JavaScript
TypeScript/JavaScript
typescript
// Turn 1
const turn1 = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Hi! My name is John. I am working on AI agents.",
store: true
});
// Turn 2
const turn2 = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "What is my name?",
previousInteractionId: turn1.id
});
console.log(turn2.steps[turn2.steps.length - 1].content[0].text);typescript
// 第一轮
const turn1 = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Hi! My name is John. I am working on AI agents.",
store: true
});
// 第二轮
const turn2 = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "What is my name?",
previousInteractionId: turn1.id
});
console.log(turn2.steps[turn2.steps.length - 1].content[0].text);Real-Time Streaming
实时流式传输
Stream responses in real-time. Passing returns an iterable chunk generator.
stream=True实时流式获取响应。传入会返回一个可迭代的分片生成器。
stream=TruePython
Python
python
response = client.interactions.create(
model="gemini-3-flash-preview",
input="Write a short poem about debugging.",
stream=True
)
for chunk in response:
if chunk.steps:
step = chunk.steps[-1]
if step.content and step.content[0].text:
print(step.content[0].text, end="", flush=True)
print()python
response = client.interactions.create(
model="gemini-3-flash-preview",
input="Write a short poem about debugging.",
stream=True
)
for chunk in response:
if chunk.steps:
step = chunk.steps[-1]
if step.content and step.content[0].text:
print(step.content[0].text, end="", flush=True)
print()TypeScript/JavaScript
TypeScript/JavaScript
typescript
const responseStream = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Write a short poem about debugging.",
stream: true
});
for await (const chunk of responseStream) {
if (chunk.steps) {
const step = chunk.steps[chunk.steps.length - 1];
if (step.content && step.content[0].text) {
process.stdout.write(step.content[0].text);
}
}
}
console.log();typescript
const responseStream = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Write a short poem about debugging.",
stream: true
});
for await (const chunk of responseStream) {
if (chunk.steps) {
const step = chunk.steps[chunk.steps.length - 1];
if (step.content && step.content[0].text) {
process.stdout.write(step.content[0].text);
}
}
}
console.log();Structured Output (Pydantic / Polymorphic response_format
)
response_format结构化输出(Pydantic / 多态response_format
)
response_formatRetrieve structured, type-safe JSON matching a schema. Under the modern Interactions API, a polymorphic argument directly takes the target schema structure.
response_format获取与指定 schema 匹配的结构化、类型安全的JSON。在现代Interactions API中,多态参数可直接传入目标schema结构。
response_formatPython
Python
python
from pydantic import BaseModel, Field
class Book(BaseModel):
title: str = Field(description="The title of the book")
author: str = Field(description="The book's author")
year_published: int
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="Recommend one famous sci-fi book.",
response_format=Book
)python
from pydantic import BaseModel, Field
class Book(BaseModel):
title: str = Field(description="The title of the book")
author: str = Field(description="The book's author")
year_published: int
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="Recommend one famous sci-fi book.",
response_format=Book
)The text will be a valid JSON matching the Book schema
返回的文本是符合Book schema的有效JSON
print(interaction.steps[-1].content[0].text)
undefinedprint(interaction.steps[-1].content[0].text)
undefinedTypeScript/JavaScript
TypeScript/JavaScript
typescript
import { Type } from "@google/genai";
const BookSchema = {
type: Type.OBJECT,
properties: {
title: { type: Type.STRING, description: "The title of the book" },
author: { type: Type.STRING, description: "The book's author" },
yearPublished: { type: Type.INTEGER }
},
required: ["title", "author", "yearPublished"]
};
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Recommend one famous sci-fi book.",
responseFormat: BookSchema
});
console.log(interaction.steps[interaction.steps.length - 1].content[0].text);typescript
import { Type } from "@google/genai";
const BookSchema = {
type: Type.OBJECT,
properties: {
title: { type: Type.STRING, description: "The title of the book" },
author: { type: Type.STRING, description: "The book's author" },
yearPublished: { type: Type.INTEGER }
},
required: ["title", "author", "yearPublished"]
};
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Recommend one famous sci-fi book.",
responseFormat: BookSchema
});
console.log(interaction.steps[interaction.steps.length - 1].content[0].text);Function Calling (Agent Tool Use)
函数调用(代理工具使用)
Define local tools (functions) and submit execution results to the stateful interaction history.
定义本地工具(函数)并将执行结果提交到有状态的交互历史中。
Python
Python
python
def get_stock_price(ticker: str) -> float:
"""Gets the stock price for a given ticker symbol."""
if ticker.upper() == "GOOG":
return 175.50
return 100.0python
def get_stock_price(ticker: str) -> float:
"""Gets the stock price for a given ticker symbol."""
if ticker.upper() == "GOOG":
return 175.50
return 100.0Turn 1: Pass tools to the model
第一轮:向模型传递工具
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="What is the stock price of GOOG?",
tools=[get_stock_price]
)
last_step = interaction.steps[-1]
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="What is the stock price of GOOG?",
tools=[get_stock_price]
)
last_step = interaction.steps[-1]
Check if the model requested a function call
检查模型是否请求了函数调用
if last_step.tool_calls:
for call in last_step.tool_calls:
if call.name == "get_stock_price":
ticker_arg = call.args.get("ticker")
price = get_stock_price(ticker_arg)
# Turn 2: Submit function execution result statefully
final_turn = client.interactions.create(
model="gemini-3-flash-preview",
input=f"The stock price for {ticker_arg} is ${price}.",
previous_interaction_id=interaction.id
)
print(final_turn.steps[-1].content[0].text)undefinedif last_step.tool_calls:
for call in last_step.tool_calls:
if call.name == "get_stock_price":
ticker_arg = call.args.get("ticker")
price = get_stock_price(ticker_arg)
# 第二轮:有状态地提交函数执行结果
final_turn = client.interactions.create(
model="gemini-3-flash-preview",
input=f"The stock price for {ticker_arg} is ${price}.",
previous_interaction_id=interaction.id
)
print(final_turn.steps[-1].content[0].text)undefinedTypeScript/JavaScript
TypeScript/JavaScript
typescript
import { Type } from "@google/genai";
// Define local tool
function getStockPrice({ ticker }: { ticker: string }): number {
if (ticker.toUpperCase() === "GOOG") {
return 175.50;
}
return 100.00;
}
// Turn 1: Pass tools to the model
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "What is the stock price of GOOG?",
tools: [{
functionDeclarations: [{
name: "getStockPrice",
description: "Gets the stock price for a given ticker symbol.",
parameters: {
type: Type.OBJECT,
properties: {
ticker: { type: Type.STRING, description: "The stock ticker symbol" }
},
required: ["ticker"]
}
}]
}]
});
const lastStep = interaction.steps[interaction.steps.length - 1];
// Check if the model requested a function call
if (lastStep.toolCalls) {
for (const call of lastStep.toolCalls) {
if (call.name === "getStockPrice") {
const tickerArg = call.args.ticker as string;
const price = getStockPrice({ ticker: tickerArg });
// Turn 2: Submit function execution result statefully
const finalTurn = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: `The stock price for ${tickerArg} is $${price}.`,
previousInteractionId: interaction.id
});
console.log(finalTurn.steps[finalTurn.steps.length - 1].content[0].text);
}
}
}typescript
import { Type } from "@google/genai";
// 定义本地工具
function getStockPrice({ ticker }: { ticker: string }): number {
if (ticker.toUpperCase() === "GOOG") {
return 175.50;
}
return 100.00;
}
// 第一轮:向模型传递工具
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "What is the stock price of GOOG?",
tools: [{
functionDeclarations: [{
name: "getStockPrice",
description: "Gets the stock price for a given ticker symbol.",
parameters: {
type: Type.OBJECT,
properties: {
ticker: { type: Type.STRING, description: "The stock ticker symbol" }
},
required: ["ticker"]
}
}]
}]
});
const lastStep = interaction.steps[interaction.steps.length - 1];
// 检查模型是否请求了函数调用
if (lastStep.toolCalls) {
for (const call of lastStep.toolCalls) {
if (call.name === "getStockPrice") {
const tickerArg = call.args.ticker as string;
const price = getStockPrice({ ticker: tickerArg });
// 第二轮:有状态地提交函数执行结果
const finalTurn = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: `The stock price for ${tickerArg} is $${price}.`,
previousInteractionId: interaction.id
});
console.log(finalTurn.steps[finalTurn.steps.length - 1].content[0].text);
}
}
}4. Accessing the Interactions API via REST
4. 通过REST访问Interactions API
For shell-based scripts, debugging, or non-Python/JS environments, you can communicate with the stateful Interactions API directly using raw HTTP/REST requests via .
curl对于基于Shell的脚本、调试或非Python/JS环境,你可以通过直接使用原始HTTP/REST请求与有状态的Interactions API通信。
curl1. REST Endpoint
1. REST端点
The REST API endpoint for interactions is:
http
POST https://aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{LOCATION}/interactions- LOCATION: Use (or custom region if required).
global - PROJECT_ID: Your Google Cloud Project ID.
Interactions的REST API端点为:
http
POST https://aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{LOCATION}/interactions- LOCATION:使用(如需自定义区域也可使用对应区域)。
global - PROJECT_ID:你的Google Cloud项目ID。
2. Set up Variables & Authentication Header
2. 设置变量与认证头
Set your target agent ID (e.g., model or custom agent path) and access token generated from Application Default Credentials:
bash
AGENT_ID="your-agent-id"
ACCESS_TOKEN=$(gcloud auth print-access-token)设置目标代理ID(例如模型或自定义代理路径)以及从Application Default Credentials生成的访问令牌:
bash
AGENT_ID="your-agent-id"
ACCESS_TOKEN=$(gcloud auth print-access-token)3. Single-Turn Interaction Payload
3. 单轮交互请求体
Send a request to start an interaction using the agent variable:
bash
curl -X POST "https://aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/global/interactions" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"agent": "'"${AGENT_ID}"'",
"input": [{
"role": "user",
"content": [{
"type": "text",
"text": "Explain serverless computing in one sentence."
}]
}]
}'使用代理变量发送请求以启动交互:
bash
curl -X POST "https://aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/global/interactions" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"agent": "'"${AGENT_ID}"'",
"input": [{
"role": "user",
"content": [{
"type": "text",
"text": "Explain serverless computing in one sentence."
}]
}]
}'Response Example
响应示例
A synchronous POST request returns a JSON object containing the conversation step details and unique identifiers:
json
{
"id": "your-interaction-id",
"status": "completed",
"steps": [
{
"role": "model",
"content": [
{
"type": "text",
"text": "Serverless computing is a cloud execution model where the cloud provider dynamically manages the allocation and provisioning of servers, charging customers based on actual usage rather than pre-purchased capacity."
}
]
}
],
"usage": {
"total_tokens": 24751,
"total_input_tokens": 23894,
"total_output_tokens": 857
},
"created": "2026-05-08T10:44:43Z",
"updated": "2026-05-08T10:44:43Z",
"environment_id": "your-environment-id",
"object": "interaction"
}同步POST请求会返回包含对话步骤详情和唯一标识符的JSON对象:
json
{
"id": "your-interaction-id",
"status": "completed",
"steps": [
{
"role": "model",
"content": [
{
"type": "text",
"text": "Serverless computing is a cloud execution model where the cloud provider dynamically manages the allocation and provisioning of servers, charging customers based on actual usage rather than pre-purchased capacity."
}
]
}
],
"usage": {
"total_tokens": 24751,
"total_input_tokens": 23894,
"total_output_tokens": 857
},
"created": "2026-05-08T10:44:43Z",
"updated": "2026-05-08T10:44:43Z",
"environment_id": "your-environment-id",
"object": "interaction"
}4. Multi-Turn Stateful Interaction Payload
4. 多轮有状态交互请求体
To continue an existing conversation statefully, specify the in the JSON payload:
previous_interaction_idbash
curl -X POST "https://aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/global/interactions" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"agent": "'"${AGENT_ID}"'",
"store": true,
"previous_interaction_id": "YOUR_PREVIOUS_INTERACTION_ID",
"input": [{
"role": "user",
"content": [{
"type": "text",
"text": "Can you elaborate on that?"
}]
}]
}'要继续现有对话的有状态会话,请在JSON请求体中指定:
previous_interaction_idbash
curl -X POST "https://aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/global/interactions" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"agent": "'"${AGENT_ID}"'",
"store": true,
"previous_interaction_id": "YOUR_PREVIOUS_INTERACTION_ID",
"input": [{
"role": "user",
"content": [{
"type": "text",
"text": "Can you elaborate on that?"
}]
}]
}'5. Streaming Output Payload
5. 流式输出请求体
To stream updates in real time (Server-Sent Events format), pass in the payload:
"stream": truebash
curl -X POST "https://aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/global/interactions" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"agent": "'"${AGENT_ID}"'",
"stream": true,
"input": [{
"role": "user",
"content": [{
"type": "text",
"text": "Write a long story about space travel."
}]
}]
}'The endpoint will return a chunked stream where each event begins with containing JSON updates with the and step contents.
data: event_typeHowhandles streaming: By default, whencurlis passed, the server responds with"stream": trueandTransfer-Encoding: chunked(Server-Sent Events).Content-Type: text/event-streamwill automatically keep the connection open and print the incoming data chunks tocurlin real time as they are pushed by the server. The user does not need to poll or pull further; the complete sequence of events streams continuously until completion.stdout
要实时流式获取更新(Server-Sent Events格式),请在请求体中传入:
"stream": truebash
curl -X POST "https://aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/global/interactions" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"agent": "'"${AGENT_ID}"'",
"stream": true,
"input": [{
"role": "user",
"content": [{
"type": "text",
"text": "Write a long story about space travel."
}]
}]
}'端点会返回分片流,每个事件以开头,包含带有和步骤内容的JSON更新。
data: event_typecurl处理流式传输的方式: 默认情况下,当传入时,服务器会返回"stream": true和Transfer-Encoding: chunked(Server-Sent Events)。curl会自动保持连接打开,并在服务器推送数据时实时将传入的数据分片打印到Content-Type: text/event-stream。用户无需轮询或进一步拉取,完整的事件序列会持续流式传输直至完成。stdout