[ ABORT TO HUD ]
SEQ. 1
SEQ. 2
SEQ. 3

Built-in Code Execution

💻 Code Execution & Live API 12m 250 BASE XP⌨ HANDS-ON LAB

Sandboxed Python in Gemini

Gemini models include a built-in code execution capability that runs Python code in a secure, sandboxed environment. Unlike external code interpreters, this runs directly within the Gemini API call — the model writes code, executes it, and uses the results to formulate its final answer.

What's Available in the Sandbox

  • Python 3.x runtime with standard library
  • NumPy for numerical computing
  • Pandas for data manipulation
  • Matplotlib for chart generation

Enabling Code Execution

Enable it by passing a code_execution tool in your request:

from google.genai import GenerativeModel, Tool

model = GenerativeModel(
    "gemini-3.5-flash",
    tools=[Tool.from_code_execution()]
)

# The model will write and execute Python to solve this
response = model.generate_content(
    "Calculate the first 20 Fibonacci numbers and plot them as a line chart."
)
# Response includes the generated chart and computed values

Use Cases

TaskHow Code Execution Helps
Math VerificationModel writes Python to verify calculations instead of relying on mental math
Data AnalysisProcess uploaded CSVs with Pandas, generate statistics
Chart GenerationCreate matplotlib visualizations inline
Algorithm TestingWrite and run code to test algorithmic solutions
⚡ Key Advantage: Built-in code execution is serverless and requires no infrastructure setup. Just add the tool to your request and Gemini handles the rest.
⌨ HANDS-ON LABLet Gemini Run Python on Google's Sandbox
⭐ +150 XP

Math by prediction is a coin flip. Attach the codeExecution tool so Gemini writes and actually runs Python inside the API call, then returns verified results.

1Capture an access token in the TOKEN variable.
2POST to generateContent with "tools":[{"codeExecution":{}}] and ask for a verifiable computation.
lab-sandbox — simulated environment
INFINITY LAB SANDBOX v2.6 — simulated shell
Type the command for the current objective. Helpers: "hint", "solution", "clear".
$
OBJECTIVE 1 / 2 — type "hint" if stuck
SYNAPSE VERIFICATION
QUERY 1 // 3
What libraries are available in Gemini's built-in code execution sandbox?
Only the Python standard library
Python standard library plus NumPy, Pandas, and Matplotlib
Every pip package
Only TensorFlow
Built-in Code Execution Tutorial | Code Execution & Live API — Vertex AI Academy