Gemini API 提供了一个代码执行工具,使模型能够生成和运行 Python 代码。然后,模型可以从代码执行结果中迭代学习,直到获得最终输出。您可以使用代码执行功能来构建可受益于基于代码的推理的应用。例如,您可以使用代码执行功能来求解方程式或处理文本。您还可以使用代码执行环境中包含的库来执行更专业的任务。
Gemini 只能执行 Python 代码。您仍然可以要求 Gemini 生成其他语言的代码,但模型无法使用代码执行工具来运行这些代码。
启用代码执行功能
如需启用代码执行功能,请在模型上配置代码执行工具。这样,模型便可以生成和运行代码。
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.5-flash",
input="What is the sum of the first 50 prime numbers? "
"Generate and run code for the calculation, and make sure you get all 50.",
tools=[{"type": "code_execution"}]
)
for step in interaction.steps:
if step.type == "model_output":
for content_block in step.content:
if content_block.type == "text":
print(content_block.text)
elif step.type == "code_execution_call":
print(step.arguments.code)
elif step.type == "code_execution_result":
print(step.result)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
model: "gemini-3.5-flash",
input: "What is the sum of the first 50 prime numbers? " +
"Generate and run code for the calculation, and make sure you get all 50.",
tools: [{ type: "code_execution" }]
});
for (const step of interaction.steps) {
if (step.type === "model_output") {
for (const contentBlock of step.content) {
if (contentBlock.type === "text") {
console.log(contentBlock.text);
}
}
} else if (step.type === "code_execution_call") {
console.log(step.arguments.code);
} else if (step.type === "code_execution_result") {
console.log(step.result);
}
}
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": "What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.",
"tools": [{"type": "code_execution"}]
}'
输出可能如下所示,为了便于阅读,我们对其进行了格式化:
Okay, I need to calculate the sum of the first 50 prime numbers. Here's how I'll
approach this:
1. **Generate Prime Numbers:** I'll use an iterative method to find prime
numbers. I'll start with 2 and check if each subsequent number is divisible
by any number between 2 and its square root. If not, it's a prime.
2. **Store Primes:** I'll store the prime numbers in a list until I have 50 of
them.
3. **Calculate the Sum:** Finally, I'll sum the prime numbers in the list.
Here's the Python code to do this:
def is_prime(n):
"""Efficiently checks if a number is prime."""
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
primes = []
num = 2
while len(primes) < 50:
if is_prime(num):
primes.append(num)
num += 1
sum_of_primes = sum(primes)
print(f'{primes=}')
print(f'{sum_of_primes=}')
primes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229]
sum_of_primes=5117
The sum of the first 50 prime numbers is 5117.
此输出结合了模型在使用代码执行功能时返回的多个内容部分:
text:模型生成的内嵌文本code_execution_call:模型生成的旨在执行的代码code_execution_result:可执行代码的结果
使用图片执行代码 (Gemini 3)
Gemini 3 Flash 模型现在可以编写和执行 Python 代码,以主动操纵和检查图片。
用例
- 缩放和检查:当细节过小时,模型会隐式检测到 (例如,读取远处仪表),并编写代码来裁剪和重新检查 更高分辨率的区域。
- 视觉数学:模型可以使用代码运行多步计算(例如, 对收据上的行项求和)。
- 图片注释:模型可以注释图片来回答问题,例如 绘制箭头来显示关系。
启用使用图片执行代码的功能
Gemini 3 Flash 正式支持使用图片执行代码的功能。您可以通过同时启用“代码执行”作为工具和“思考”来激活此行为。
Python
from google import genai
import requests
import base64
from PIL import Image
import io
image_path = "https://goo.gle/instrument-img"
image_bytes = requests.get(image_path).content
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.5-flash",
input=[
{"type": "image", "data": base64.b64encode(image_bytes).decode('\utf-8'), "mime_type": "image/jpeg"},
{"type": "text", "text": "Zoom into the expression pedals and tell me how many pedals are there?"}
],
tools=[{"type": "code_execution"}]
)
for step in interaction.steps:
if step.type == "model_output":
for content_block in step.content:
if content_block.type == "text":
print(content_block.text)
elif content_block.type == "image":
display(Image.open(io.BytesIO(base64.b64decode(content_block.data))))
elif step.type == "code_execution_call":
print(step.arguments.code)
elif step.type == "code_execution_result":
print(step.result)
JavaScript
import { GoogleGenAI } from "@google/genai";
async function main() {
const client = new GoogleGenAI({});
const imageUrl = "https://goo.gle/instrument-img";
const response = await fetch(imageUrl);
const imageArrayBuffer = await response.arrayBuffer();
const base64ImageData = Buffer.from(imageArrayBuffer).toString('base64');
const interaction = await client.interactions.create({
model: "gemini-3.5-flash",
input: [
{
type: "image",
data: base64ImageData,
mime_type: "image/jpeg"
},
{ type: "text", text: "Zoom into the expression pedals and tell me how many pedals are there?" }
],
tools: [{ type: "code_execution" }]
});
for (const step of interaction.steps) {
if (step.type === "model_output") {
for (const contentBlock of step.content) {
if (contentBlock.type === "text") {
console.log("Text:", contentBlock.text);
}
}
} else if (step.type === "code_execution_call") {
console.log(`\nGenerated Code:\n`, step.arguments.code);
} else if (step.type === "code_execution_result") {
console.log(`\nExecution Output:\n`, step.result);
}
}
}
main();
REST
IMG_URL="https://goo.gle/instrument-img"
MODEL="gemini-3.5-flash"
MIME_TYPE=$(curl -sIL "$IMG_URL" | grep -i '^content-type:' | awk -F ': ' '{print $2}' | sed 's/\r$//' | head -n 1)
if [[ -z "$MIME_TYPE" || ! "$MIME_TYPE" == image/* ]]; then
MIME_TYPE="image/jpeg"
fi
if [[ "$(uname)" == "Darwin" ]]; then
IMAGE_B64=$(curl -sL "$IMG_URL" | base64 -b 0)
elif [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
IMAGE_B64=$(curl -sL "$IMG_URL" | base64)
else
IMAGE_B64=$(curl -sL "$IMG_URL" | base64 -w0)
fi
# Use jq to create the JSON payload to avoid "Argument list too long" error with large base64 strings
echo -n "$IMAGE_B64" > image_b64.txt
jq -n \
--rawfile b64 image_b64.txt \
--arg mime "$MIME_TYPE" \
'{
model: "gemini-3.5-flash",
input: [
{type: "image", data: $b64, mime_type: $mime},
{type: "text", text: "Zoom into the expression pedals and tell me how many pedals are there?"}
],
tools: [{type: "code_execution"}]
}' > payload.json
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d @payload.json
在多轮互动中使用代码执行功能
您还可以使用 previous_interaction_id 在多轮对话中使用代码执行功能。
Python
from google import genai
client = genai.Client()
interaction1 = client.interactions.create(
model="gemini-3.5-flash",
input="I have a math question for you.",
tools=[{"type": "code_execution"}]
)
print(interaction1.output_text)
interaction2 = client.interactions.create(
model="gemini-3.5-flash",
previous_interaction_id=interaction1.id,
input="What is the sum of the first 50 prime numbers? "
"Generate and run code for the calculation, and make sure you get all 50.",
tools=[{"type": "code_execution"}]
)
for step in interaction2.steps:
if step.type == "model_output":
for content_block in step.content:
if content_block.type == "text":
print(content_block.text)
elif step.type == "code_execution_call":
print(step.arguments.code)
elif step.type == "code_execution_result":
print(step.result)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction1 = await client.interactions.create({
model: "gemini-3.5-flash",
input: "I have a math question for you.",
tools: [{ type: "code_execution" }]
});
console.log(interaction1.output_text);
const interaction2 = await client.interactions.create({
model: "gemini-3.5-flash",
previous_interaction_id: interaction1.id,
input: "What is the sum of the first 50 prime numbers? " +
"Generate and run code for the calculation, and make sure you get all 50.",
tools: [{ type: "code_execution" }]
});
for (const step of interaction2.steps) {
if (step.type === "model_output") {
for (const contentBlock of step.content) {
if (contentBlock.type === "text") {
console.log(contentBlock.text);
}
}
} else if (step.type === "code_execution_call") {
console.log(step.arguments.code);
} else if (step.type === "code_execution_result") {
console.log(step.result);
}
}
REST
# First turn
RESPONSE1=$(curl -s -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": "I have a math question for you.",
"tools": [{"type": "code_execution"}]
}')
INTERACTION_ID=$(echo $RESPONSE1 | jq -r '.id')
# Second turn with previous_interaction_id
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"'",
"input": "What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.",
"tools": [{"type": "code_execution"}]
}'
输入/输出 (I/O)
从 Gemini 2.0 Flash开始,代码 执行功能支持文件输入和图表输出。借助这些输入和输出 功能,您可以上传 CSV 和文本文件,提出有关 文件的问题,并让 Matplotlib 图表作为回答的 一部分生成。输出文件会作为内嵌图片在回答中返回。
I/O 定价
使用代码执行 I/O 时,您需要为输入 token 和输出 token 支付费用:
输入 token:
- 用户提示
输出 token:
- 模型生成的代码
- 代码环境中的代码执行输出
- 思考 token
- 模型生成的摘要
I/O 详细信息
在使用代码执行 I/O 时,请注意以下技术细节:
- 代码环境的最长运行时长为 30 秒。
- 如果代码环境生成错误,模型可能会决定重新生成代码输出。这种情况最多可能会发生 5 次。
- 文件输入大小上限受模型 token 窗口的限制。如果您上传的文件超过模型的上下文窗口上限,API 将返回错误。
- 代码执行功能最适合处理文本和 CSV 文件。
- 输入文件可以作为内嵌数据传递,也可以使用 Files API 上传,而输出文件始终作为内嵌数据返回。
结算
通过 Gemini API 启用代码执行功能不会产生额外的费用。 系统会根据您使用的 Gemini 模型,按当前的输入和输出 token 费率向您收费。
以下是关于代码执行结算的一些其他事项:
- 您只需为传递给模型的输入 token 支付一次费用,并且需要为模型返回给您的最终输出 token 支付费用。
- 表示生成的代码的 token 会计为输出 token。生成的代码可以包含文本和多模态输出(例如图片)。
- 代码执行结果也会计为输出 token。
结算模型如下图所示:

- 系统会根据您使用的 Gemini 模型,按当前的输入和输出 token 费率向您收费。
- 如果 Gemini 在生成回答时使用了代码执行功能,则原始 提示、生成的代码以及已执行代码的相应结果会被标记为 中间 token,并会按 输入 token 计费。
- 然后,Gemini 会生成摘要,并返回生成的代码、已执行代码的结果以及最终摘要。这些内容会按输出 token 计费。
- Gemini API 在 API 响应中包含中间 token 数,因此您可以了解为什么会获得超出初始提示的额外输入 token。
限制
- 该模型只能生成和执行代码。它无法返回其他制品,例如媒体文件。
- 在某些情况下,启用代码执行功能可能会导致模型输出的其他方面(例如,编写故事)出现回归问题。
- 不同模型成功使用代码执行功能的能力存在一些差异。
支持的工具组合
代码执行工具可以与 “依托 Google 搜索进行接地”功能结合使用,以 支持更复杂的用例。
Gemini 3 模型支持将内置工具(例如代码执行)与自定义工具(函数调用)结合使用。
受支持的库
代码执行环境包含以下库:
- attrs
- 国际象棋
- contourpy
- fpdf
- geopandas
- imageio
- jinja2
- joblib
- jsonschema
- jsonschema-specifications
- lxml
- matplotlib
- mpmath
- numpy
- opencv-python
- openpyxl
- 打包
- pandas
- pillow
- protobuf
- pylatex
- pyparsing
- PyPDF2
- python-dateutil
- python-docx
- python-pptx
- reportlab
- scikit-learn
- scipy
- seaborn
- six
- striprtf
- sympy
- tabulate
- tensorflow
- toolz
- xlrd
您无法安装自己的库。
后续步骤
- 试用
- 了解其他 Gemini API 工具: