Code Execution
Code execution enables Gemini to generate, run, and iteratively refine Python code to solve problems. The model can write code, execute it in a secure sandbox, observe results, and automatically fix errors until producing a working solution.Why Code Execution?
Iterative Learning
Model observes execution results and refines code automatically
Mathematical Accuracy
Solve complex calculations with verified results
Data Processing
Generate and test data analysis code
Supported Models
Code execution is available on:- gemini-3.1-pro-preview
- gemini-3-flash-preview
- gemini-2.5-pro
- gemini-2.5-flash
Basic Example
Setup
Generate and Execute Code
View Generated Code
Access the Python code that was generated:View Execution Results
Access code execution output and status:Mathematical Problem Solving
Complex Calculations
Data Analysis
Code Execution in Chat
Maintain context across multiple code-related queries:Streaming Code Execution
Stream responses as code is generated and executed:Data Exploration
Analyze datasets iteratively:Algorithmic Problem Solving
Sorting Algorithm
Graph Algorithms
Statistical Analysis
Code Validation
The model automatically validates and fixes code:Extract Code and Results
Programmatically access generated code and results:Combine with Other Features
Code Execution + Function Calling
Code Execution + Multimodal
Best Practices
Clear Instructions
Specify exactly what the code should accomplish
Temperature 0
Use temperature=0 for deterministic code generation
Test Cases
Include test cases in your prompts
Verify Results
Always validate code execution outcomes
Effective Prompts
✅ Good Prompts:Limitations
Available Libraries
The execution environment includes Python standard library modules:math,random,statisticsdatetime,timejson,csvitertools,functoolscollections,heapq- And other standard library modules