Gemini 3.5 Flash 的新功能

3.6 Flash 和 3.5 Flash-Lite 此页面

Gemini 3.5 Flash 已正式发布 (GA),性能稳定,可用于大规模生产。作为我们最智能的 Flash 模型,它可在智能体执行、编码和长期任务中持续提供前沿性能,并能大规模处理这些任务。

本指南概述了 Gemini 3.5 Flash 的改进、API 变更和迁移指南。

新建模型

模型 模型 ID 说明
Gemini 3.5 Flash gemini-3.5-flash Google 迄今为止最智能的模型,可在智能体和编程任务中持续提供前沿性能。

Gemini 3.5 Flash 支持 100 万个 token 的上下文窗口、最多 65,000 个输出 token、思考功能,以及与 Gemini 3 Flash 相同的工具和平台功能集,包括电脑使用(预览版)

如需了解完整规格,请参阅型号概览。 如需了解价格,请参阅价格页面

快速入门

本指南中的所有示例均使用 Interactions API。该 API 还支持 GenerateContent API;相同的配置选项和建议也适用于该 API。

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.5-flash",
    input="Explain how parallel agentic execution works in three sentences."
)
print(interaction.output_text)

JavaScript

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

const client = new GoogleGenAI({});

async function main() {
  const interaction = await client.interactions.create({
    model: "gemini-3.5-flash",
    input: "Explain how parallel agentic execution works in three sentences.",
  });
  console.log(interaction.output_text);
}

main();

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;

Client client = new Client();

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.5-flash"))
        .input(
            InteractionsInput.of(
                "Explain how parallel agentic execution works in three sentences."))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

Go

package main

import (
    "context"
    "fmt"
    "log"

    "google.golang.org/genai"
    "google.golang.org/genai/interactions/models/interactions"
    "google.golang.org/genai/interactions/models/operations"
)

func main() {
    ctx := context.Background()
    client, err := genai.NewClient(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }

    res, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
        Body: operations.NewCreateInteractionRequestBody(interactions.CreateModelInteraction{
            Model: interactions.Model("gemini-3.5-flash"),
            Input: interactions.NewInteractionsInput("Explain how parallel agentic execution works in three sentences."),
        }),
    })
    if err != nil {
        log.Fatal(err)
    }
    if res.Interaction.OutputText != nil {
        fmt.Println(*res.Interaction.OutputText)
    }
}

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.5-flash",
    "input": "Explain how parallel agentic execution works in three sentences."
  }'

最新资讯

  • 持续提供前沿性能:Google 最智能的 Flash 模型,针对大规模智能体和编码任务进行了优化。
  • Agentic 执行:大规模部署子代理、解决问题和快速 agentic 循环。
  • 编码:迭代编码周期、快速探索和原型设计,以测试替代方案并动态探索解决方案。
  • 长周期:大规模使用多步工作流和工具。
  • 思维保留:模型会自动在多轮对话中保留中间推理过程。无需更改 API。
  • 新的默认努力程度:默认思考力度从 high 更改为 medium。如需了解详情,请参阅新的默认效果水平
  • 改进了 low 思维low 在代码和智能体任务方面有了显著改进,只需更少的步骤即可提供高质量的输出,同时延迟和成本更低。
  • GA 版本:稳定模型,可用于大规模生产。

选择合适的 Flash 型号

Gemini 3.5 Flash 是我们迄今为止最智能、最强大的 Flash 模型。不过,不同的应用场景可能对费用和延迟时间有不同的要求。

  • Gemini 3.1 Flash-Lite:对于不需要 3.5 Flash 的高级推理深度、但需要低成本、大批量完成的任务,我们建议使用 Gemini 3.1 Flash-Lite。它是一种稳定、长期的模型,经过优化,可提高效率。如需了解详情,请参阅 Flash-Lite 开发者指南
  • Gemini 3 Flash(预览版):虽然我们建议迁移到 3.5 Flash 以获得正式版的稳定性和更强的推理能力,但Gemini 3 Flash(预览版)仍可供希望继续使用预览版模型进行测试的开发者使用。

行为变更

新的默认运动强度:medium

默认思考强度现在为 medium,之前在 Gemini 3 Flash 预览版中为 highmedium 在各种任务中都能取得非常出色的效果,同时速度更快、成本更低。对于复杂问题,high可促使模型进行更深入的思考。

工作量级别 适用情形
minimal 针对响应速度进行了优化。聊天式使用情形、快速的事实性回答、更简单的工具调用。
low 需要更低延迟和更少步骤的代码和智能体任务。还非常适合需要一定思考的分析和写作任务。
medium(默认) 适用于大多数任务的最佳质量。建议用于复杂的代码和智能体用例。
high 最大限度地提高模型的思考能力和工具使用能力。最擅长处理复杂的推理、高难度的数学问题以及最困难的代码或代理任务。允许进行扩展思考和函数调用。

如需替换默认值,请在配置中设置 thinking_level

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.5-flash",
    input="Prove that the square root of 2 is irrational.",
    generation_config={"thinking_level": "high"},
)
print(interaction.output_text)

JavaScript

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

const client = new GoogleGenAI({});

async function main() {
  const interaction = await client.interactions.create({
    model: "gemini-3.5-flash",
    input: "Prove that the square root of 2 is irrational.",
    generationConfig: { thinkingLevel: "high" },
  });
  console.log(interaction.output_text);
}

main();

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.GenerationConfig;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.ThinkingLevel;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;

Client client = new Client();

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.5-flash"))
        .input(InteractionsInput.of("Prove that the square root of 2 is irrational."))
        .generationConfig(GenerationConfig.builder().thinkingLevel(ThinkingLevel.HIGH).build())
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

Go

package main

import (
    "context"
    "fmt"
    "log"

    "google.golang.org/genai"
    "google.golang.org/genai/interactions/models/interactions"
    "google.golang.org/genai/interactions/models/operations"
)

func main() {
    ctx := context.Background()
    client, err := genai.NewClient(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }

    res, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
        Body: operations.NewCreateInteractionRequestBody(interactions.CreateModelInteraction{
            Model: interactions.Model("gemini-3.5-flash"),
            Input: interactions.NewInteractionsInput("Prove that the square root of 2 is irrational."),
            GenerationConfig: &interactions.GenerationConfig{
                ThinkingLevel: interactions.ThinkingLevelHigh.ToPointer(),
            },
        }),
    })
    if err != nil {
        log.Fatal(err)
    }
    if res.Interaction.OutputText != nil {
        fmt.Println(*res.Interaction.OutputText)
    }
}

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.5-flash",
    "input": "Prove that the square root of 2 is irrational.",
    "generation_config": {"thinking_level": "high"}
  }'

下表显示了每个模型支持的思考级别:

思考等级 Gemini 3.5 Flash Gemini 3.1 Pro Gemini 3.1 Flash-Lite Gemini 3 Flash 说明
minimal 支持 不受支持 支持(默认) 支持 与大多数查询的“不思考”设置相匹配。请注意,minimal 并不保证思考功能已关闭,模型可能会针对复杂任务进行非常少的推理。
low 支持 支持 支持 支持 最大限度地缩短延迟时间并降低费用。
medium 支持(默认) 支持 支持 支持 平衡的思考能力,可处理大多数任务。
high 支持(动态) 支持(默认,动态) 支持(动态) 支持(默认,动态) 最大限度地提高推理深度。

思维保留

模型会自动在多轮对话中保持中间推理。当对话历史记录中存在推理上下文时,推理上下文会延续下去,从而提高在复杂的多步任务(例如迭代调试和代码重构)方面的性能。无需进行 API 变更:

  • Interactions API:系统会自动保留想法。行为没有变化。
  • GenerateContent API:从 Gemini 3.5 Flash 开始,如果对话历史记录中存在思维签名,模型会使用之前所有轮次的推理上下文。如需启用此功能,请在 contents 中传递完整、未修改的对话历史记录(包括思考签名)。SDK 会自动处理此问题。

Gemini 3.x 中的参数更新和最佳实践

以下内容适用于所有 Gemini 3.x 模型,包括 Gemini 3.5 Flash。

  • temperaturetop_ptop_k:我们强烈建议不要更改默认值。Gemini 3 的推理能力已针对默认设置进行优化。
  • 不过,应使用 thinking_level 代替 thinking_budget
  • 函数调用响应匹配idname 和响应计数必须与前面的调用匹配。
  • 多模态函数响应:在函数响应内(而非外部)包含多模态内容。
  • 函数响应中的内嵌指令:附加到函数响应文本,而不是作为单独的部分。
  • 减少不必要的工具调用:使用较低的思考水平或通过实验调整系统指令,以减少智能体工作流中的工具调用。

如需了解如何更新代码,请参阅下文。

抽样参数(不再推荐)

temperaturetop_ptop_k 不再建议用于所有 Gemini 3.x 模型。Gemini 3 的推理能力已针对默认设置进行优化。从所有请求中移除这些参数。

# ⚠️ Remove these parameters (not recommended)
generation_config = {
    "temperature": 0.7,
    "top_p": 0.9,
    "top_k": 40,
}

为确保确定性,我们建议您根据具体使用情形定义包含明确规则的系统指令。

thinking_budget(不再推荐)

现在不建议在所有 Gemini 3.x 模型中使用原始数值 thinking_budget 参数。请改用 thinking_level 字符串枚举。

# ⚠️ Before (not recommended)
generation_config = {
    "thinking": {"thinking_budget": 7500},
}

# ✅ After
generation_config = {
    "thinking": {"thinking_level": "medium"},
}

可用值:minimallowmedium(默认值)和 high

函数调用:严格的响应匹配

Interactions API 已经会在函数响应不匹配时出错。GenerateContent API 目前不会出错,但如果响应不匹配,模型在大多数情况下会返回带有 finish_reason: STOP 的空响应。请务必遵循以下惯例:

要求 详细信息
包含id 每个 FunctionResponse 都必须包含相应 FunctionCall 中的 id
name 响应中的 name 必须与调用中的 name 一致
匹配数 针对收到的每个 FunctionCall 准确返回一个 FunctionResponse

Python

# ✅ Include matching call_id and name in the function_result
final_interaction = client.interactions.create(
    model="gemini-3.5-flash",
    previous_interaction_id=interaction.id,
    tools=[my_tool],
    input=[{
        "type": "function_result",
        "name": fc_step.name,
        "call_id": fc_step.id,
        "result": [{"type": "text", "text": json.dumps(result)}],
    }],
)

JavaScript

// ✅ Include matching call_id and name in the function_result
const finalInteraction = await client.interactions.create({
  model: "gemini-3.5-flash",
  previousInteractionId: interaction.id,
  tools: [myTool],
  input: [{
    type: "function_result",
    name: fcStep.name,
    call_id: fcStep.id,
    result: [{ type: "text", text: JSON.stringify(result) }],
  }],
});

Java

import java.util.Arrays;
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Function;
import com.google.genai.gaos.models.interactions.FunctionResultStep;
import com.google.genai.gaos.models.interactions.FunctionResultStepResultUnion;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.List;

Client client = new Client();

// Assumes interactionId, callId, functionName, myTool, and resultJson from previous step
String interactionId = "interaction-id-123";
String callId = "call-id-123";
String functionName = "get_weather";
Function myTool = Function.builder().name(functionName).build();
String resultJson = "{\"temperature\": \"72F\"}";

// ✅ Include matching callId and name in the FunctionResultStep
FunctionResultStep functionResult =
    FunctionResultStep.builder()
        .name(functionName)
        .callId(callId)
        .result(
            FunctionResultStepResultUnion.of(
                Arrays.asList(TextContent.builder().text(resultJson).build())))
        .build();

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.5-flash"))
        .previousInteractionId(interactionId)
        .tools(Arrays.asList(myTool))
        .input(InteractionsInput.ofStep(Arrays.asList(functionResult)))
        .build();

Interaction finalInteraction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

Go

package main

import (
    "context"
    "log"

    "google.golang.org/genai"
    "google.golang.org/genai/interactions/models/interactions"
    "google.golang.org/genai/interactions/models/operations"
)

func main() {
    ctx := context.Background()
    client, err := genai.NewClient(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }

    // Assumes interactionID, callID, functionName, myTool, and resultJSON from previous step
    interactionID := "interaction-id-123"
    callID := "call-id-123"
    functionName := "get_weather"
    myTool := interactions.NewTool(interactions.Function{Name: genai.Ptr(functionName)})
    resultJSON := `{"temperature": "72F"}`

    // ✅ Include matching CallID and Name in the FunctionResultStep
    functionResult := interactions.NewStep(interactions.FunctionResultStep{
        Name:   genai.Ptr(functionName),
        CallID: callID,
        Result: interactions.NewFunctionResultStepResultUnion([]interactions.FunctionResultSubcontent{
            interactions.NewFunctionResultSubcontent(interactions.TextContent{Text: resultJSON}),
        }),
    })

    finalRes, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
        Body: operations.NewCreateInteractionRequestBody(interactions.CreateModelInteraction{
            Model:                 interactions.Model("gemini-3.5-flash"),
            PreviousInteractionID: genai.Ptr(interactionID),
            Tools:                 []interactions.Tool{myTool},
            Input:                 interactions.NewInteractionsInput([]interactions.Step{functionResult}),
        }),
    })
    if err != nil {
        log.Fatal(err)
    }
    _ = finalRes
}

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.5-flash",
    "previous_interaction_id": "<INTERACTION_ID>",
    "tools": [...],
    "input": [{
      "type": "function_result",
      "name": "my_function",
      "call_id": "<CALL_ID>",
      "result": [{"type": "text", "text": "..."}]
    }]
  }'

多模态函数响应

我们经常看到客户在函数响应之外提供图片。这可能会导致模型出现意外行为(例如思维泄露),并导致输出质量下降。请改为遵循多模态函数响应 API 文档中的建议,并在发送给模型的函数响应部分中添加多模态内容。模型可以在下一轮对话中处理此多模态内容,从而生成更明智的回答。

Python

# ✅ Include multimodal content in the function response
final_interaction = client.interactions.create(
    model="gemini-3.5-flash",
    previous_interaction_id=interaction.id,
    input=[
        {
            "type": "function_result",
            "name": tool_call.name,
            "call_id": tool_call.id,
            "result": [
                {"type": "text", "text": "instrument.jpg"},
                {
                    "type": "image",
                    "mime_type": "image/jpeg",
                    "data": base64_image_data,
                },
            ],
        }
    ],
)

JavaScript

// ✅ Include multimodal content in the function response
const finalInteraction = await client.interactions.create({
  model: "gemini-3.5-flash",
  previousInteractionId: interaction.id,
  input: [{
    type: "function_result",
    name: toolCall.name,
    call_id: toolCall.id,
    result: [
      { type: "text", text: "instrument.jpg" },
      {
        type: "image",
        mime_type: "image/jpeg",
        data: base64ImageData,
      },
    ],
  }],
});

Java

import java.util.Arrays;
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.FunctionResultStep;
import com.google.genai.gaos.models.interactions.FunctionResultStepResultUnion;
import com.google.genai.gaos.models.interactions.ImageContent;
import com.google.genai.gaos.models.interactions.ImageContentMimeType;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.List;

Client client = new Client();

// Assumes interactionId, callId, functionName, and base64ImageData from previous step
String interactionId = "interaction-id-123";
String callId = "call-id-123";
String functionName = "get_instrument_image";
String base64ImageData = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";

// ✅ Include multimodal content in the function response
FunctionResultStep functionResult =
    FunctionResultStep.builder()
        .name(functionName)
        .callId(callId)
        .result(
            FunctionResultStepResultUnion.of(
                Arrays.asList(
                    TextContent.builder().text("instrument.jpg").build(),
                    ImageContent.builder()
                        .mimeType(ImageContentMimeType.IMAGE_JPEG)
                        .data(base64ImageData)
                        .build())))
        .build();

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.5-flash"))
        .previousInteractionId(interactionId)
        .input(InteractionsInput.ofStep(Arrays.asList(functionResult)))
        .build();

Interaction finalInteraction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

Go

package main

import (
    "context"
    "log"

    "google.golang.org/genai"
    "google.golang.org/genai/interactions/models/interactions"
    "google.golang.org/genai/interactions/models/operations"
)

func main() {
    ctx := context.Background()
    client, err := genai.NewClient(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }

    // Assumes interactionID, callID, functionName, and base64ImageData from previous step
    interactionID := "interaction-id-123"
    callID := "call-id-123"
    functionName := "get_instrument_image"
    base64ImageData := "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="

    // ✅ Include multimodal content in the function response
    functionResult := interactions.NewStep(interactions.FunctionResultStep{
        Name:   genai.Ptr(functionName),
        CallID: callID,
        Result: interactions.NewFunctionResultStepResultUnion([]interactions.FunctionResultSubcontent{
            interactions.NewFunctionResultSubcontent(interactions.TextContent{
                Text: "instrument.jpg",
            }),
            interactions.NewFunctionResultSubcontent(interactions.ImageContent{
                MimeType: interactions.ImageContentMimeType("image/jpeg").ToPointer(),
                Data:     genai.Ptr(base64ImageData),
            }),
        }),
    })

    finalRes, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
        Body: operations.NewCreateInteractionRequestBody(interactions.CreateModelInteraction{
            Model:                 interactions.Model("gemini-3.5-flash"),
            PreviousInteractionID: genai.Ptr(interactionID),
            Input:                 interactions.NewInteractionsInput([]interactions.Step{functionResult}),
        }),
    })
    if err != nil {
        log.Fatal(err)
    }
    _ = finalRes
}

函数响应中的内嵌指令

我们经常看到,客户端会提供额外的指令以及函数响应作为后续的 Parts。这可能会导致模型出现意外行为(例如思维泄露),并导致输出质量下降。而是将所有额外的指令附加到函数响应文本的末尾,并用两个换行符分隔。

Python

# ✅ Append inline instructions to the end of the function response separated by two newlines
result_text = f"{json.dumps(result)}\n\n<your inline instructions>"

final_interaction = client.interactions.create(
    model="gemini-3.5-flash",
    previous_interaction_id=interaction.id,
    tools=[my_tool],
    input=[{
        "type": "function_result",
        "name": fc_step.name,
        "call_id": fc_step.id,
        "result": [{"type": "text", "text": result_text}],
    }],
)

JavaScript

// ✅ Append inline instructions to the end of the function response separated by two newlines
const resultText = `${JSON.stringify(result)}\n\n<your inline instructions>`;

const finalInteraction = await client.interactions.create({
  model: "gemini-3.5-flash",
  previousInteractionId: interaction.id,
  tools: [myTool],
  input: [{
    type: "function_result",
    name: fcStep.name,
    call_id: fcStep.id,
    result: [{ type: "text", text: resultText }],
  }],
});

Java

import java.util.Arrays;
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Function;
import com.google.genai.gaos.models.interactions.FunctionResultStep;
import com.google.genai.gaos.models.interactions.FunctionResultStepResultUnion;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.List;

Client client = new Client();

// Assumes interactionId, callId, functionName, myTool, and resultJson from previous step
String interactionId = "interaction-id-123";
String callId = "call-id-123";
String functionName = "get_weather";
Function myTool = Function.builder().name(functionName).build();
String resultJson = "{\"temperature\": \"72F\"}";

// ✅ Append inline instructions to the end of the function response separated by two newlines
String resultText = resultJson + "\n\n<your inline instructions>";

FunctionResultStep functionResult =
    FunctionResultStep.builder()
        .name(functionName)
        .callId(callId)
        .result(
            FunctionResultStepResultUnion.of(
                Arrays.asList(TextContent.builder().text(resultText).build())))
        .build();

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.5-flash"))
        .previousInteractionId(interactionId)
        .tools(Arrays.asList(myTool))
        .input(InteractionsInput.ofStep(Arrays.asList(functionResult)))
        .build();

Interaction finalInteraction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

Go

package main

import (
    "context"
    "log"

    "google.golang.org/genai"
    "google.golang.org/genai/interactions/models/interactions"
    "google.golang.org/genai/interactions/models/operations"
)

func main() {
    ctx := context.Background()
    client, err := genai.NewClient(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }

    // Assumes interactionID, callID, functionName, myTool, and resultJSON from previous step
    interactionID := "interaction-id-123"
    callID := "call-id-123"
    functionName := "get_weather"
    myTool := interactions.NewTool(interactions.Function{Name: genai.Ptr(functionName)})
    resultJSON := `{"temperature": "72F"}`

    // ✅ Append inline instructions to the end of the function response separated by two newlines
    resultText := resultJSON + "\n\n<your inline instructions>"

    functionResult := interactions.NewStep(interactions.FunctionResultStep{
        Name:   genai.Ptr(functionName),
        CallID: callID,
        Result: interactions.NewFunctionResultStepResultUnion([]interactions.FunctionResultSubcontent{
            interactions.NewFunctionResultSubcontent(interactions.TextContent{Text: resultText}),
        }),
    })

    finalRes, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
        Body: operations.NewCreateInteractionRequestBody(interactions.CreateModelInteraction{
            Model:                 interactions.Model("gemini-3.5-flash"),
            PreviousInteractionID: genai.Ptr(interactionID),
            Tools:                 []interactions.Tool{myTool},
            Input:                 interactions.NewInteractionsInput([]interactions.Step{functionResult}),
        }),
    })
    if err != nil {
        log.Fatal(err)
    }
    _ = finalRes
}

减少不必要的工具调用

如果您发现工具调用的使用次数过多,可以采用以下两种技巧来尽量减少这种情况:

  1. 首先降低思考水平mediumlowminimal):较高的思考水平会促使模型使用更多工具进行探索和验证,因此降低思考水平可以减少工具调用次数。

  2. 添加系统指令:如果调整思维水平后,过度使用的情况仍然存在,请考虑添加限制工具使用的提示。例如:

    You have a limited action budget of <n> tool calls. Use them efficiently.
    

迁移核对清单

我们强烈建议您更新到 google-genai SDK v2.0.0 或更高版本。此版本对 Interactions API 引入了重大变更。如需了解详情,请参阅重大变更迁移指南

从 Gemini 3 Flash 预览版迁移

  • 更新模型名称:gemini-3-flash-previewgemini-3.5-flash
  • 查看价格。Gemini 3.5 Flash 比 Gemini 3 Flash 预览版更贵。如果您的应用场景对成本非常敏感,请考虑改用 Gemini 3.1 Flash-Lite。如需了解详情,请参阅价格页面
  • 从配置中移除 temperaturetop_ptop_k(不再推荐)。
  • thinking_budget 替换为 thinking_level
  • 向所有 FunctionResponse 部分添加了 id 和匹配的 name
  • 测试提示。默认效果从 high 更改为 medium;验证质量、速度和费用。
  • “思维保留”功能现在默认处于开启状态。推理上下文会在对话轮次之间延续,这有助于提高性能,但可能会增加 token 用量。
  • 减少不必要的工具调用:首先降低思考水平(mediumlowminimal);如果过度使用工具的情况仍然存在,请添加系统指令来限制工具使用。
  • 支持 Computer Use

从 Gemini 2.5 迁移

以上全部,外加:

  • 简化提示。如果您之前使用思维链提示工程来强制推理,请尝试改用 thinking_level: "medium""high",并使用更简单的提示。
  • 测试 PDF 和媒体工作负载。如果您之前依赖特定行为进行密集文档解析,请测试 media_resolution_high 设置,以确保准确率不受影响。迁移到 Gemini 3 默认设置还可能会增加 PDF 的 token 使用量,但会减少视频的 token 使用量;如果请求超出上下文窗口,请明确降低 media_resolution。如需了解详情,请参阅媒体分辨率文档。
  • 利用组合工具使用。 Google 搜索、网址上下文、代码执行和自定义函数可以在同一请求中使用。
  • 如果使用多模态函数响应,请将多模态内容移到函数响应部分内,而不是与函数响应部分并列。
  • 如果将内嵌指令与函数响应搭配使用,请将内嵌指令附加到函数响应文本中,并用两个换行符分隔,而不是作为单独的部分。
  • Gemini 3.x 不支持图像分割。对于分割工作负载,请继续使用 Gemini 2.5 Flash 并关闭思考功能。
  • 从配置中移除 candidate_count(Gemini 3.x 中不支持)

Gemini 3 系列功能

Gemini 3.5 Flash 继承了 Gemini 3 系列的所有功能,包括 Computer Use。 Gemini 3 中引入并沿用至今的功能:

  • 思考:在 API 调用之间保留的加密推理上下文。在 Interactions API 中为自动;在 GenerateContent 中为隐式。
  • 使用工具的结构化输出:将 JSON 模式与内置工具(搜索、网址上下文、代码执行、函数调用)相结合。
  • 多模态函数响应:在函数调用结果中返回图片、音频和其他媒体。
  • 代码执行与图片:执行可处理和生成图片的代码。
  • 组合工具使用:在同一请求中使用内置工具和自定义函数调用。
  • 媒体分辨率:可精细控制图片、视频和 PDF 输入的 token 分配。Gemini 3 模型支持为混合保真度提示设置每个内容项的分辨率(lowmediumhighultra_high)。
  • 思考特征:模型内部推理的加密表示形式。在无状态模式下进行多轮函数调用时需要;由 Interactions API 和官方 SDK 自动管理。

提示撰写最佳实践

Gemini 3.x 模型是推理模型,因此您需要改变提示方式。

  • 指令要准确:应简明扼要。Gemini 3.x 最适合回答直接、清晰的指令。为旧版模型设计的冗长或复杂的提示工程技术可能会导致模型过度分析。
  • 输出详细程度:默认情况下,Gemini 3 和 3.1 的输出详细程度较低,更倾向于提供直接、高效的答案。如果您的应用场景需要对话式语气,请在提示中明确引导模型(例如,“以友好健谈的助理身份解释此内容”)。
  • 上下文管理:处理大型数据集(例如整本书、代码库或长视频)时,请将具体指令或问题放在提示末尾的数据上下文之后。以“根据上述信息…”等短语开头,锚定模型的推理。

如需详细了解提示设计策略,请参阅提示工程指南

限制

  • Gemini 3.x 不支持图像分割。对于分割工作负载,请继续使用 Gemini 2.5 Flash 并关闭思考功能。

常见问题解答

  1. Gemini 3.5 Flash 的知识截点是什么?Gemini 3.5 Flash 的知识截点为 2025 年 1 月。如需了解最新信息,请使用搜索基础工具。

  2. 上下文窗口有哪些限制?Gemini 3.5 Flash 支持 100 万个 token 输入的上下文窗口,以及最多 65,000 个输出 token。

  3. 我的旧版 thinking_budget 代码是否仍然有效?可以,为了实现向后兼容性,我们仍支持 thinking_budget,但建议您迁移到 thinking_level,以便获得更可预测的性能。请勿在同一请求中同时使用这两者。

  4. Gemini 3.5 Flash 是否支持 Batch API?可以。如需了解详情,请参阅批量 API 指南。

  5. 是否支持上下文缓存?支持上下文缓存

  6. 支持哪些工具?Gemini 3.5 Flash 支持 Google 搜索Grounding with Google Maps文件搜索代码执行网址上下文和标准函数调用,包括组合工具使用电脑使用

后续步骤