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.
response = client.models.generate_content( model="gemini-3-flash-preview", contents="What is the sum of the first 50 prime numbers? Generate and run code.", config=GenerateContentConfig( tools=[code_execution_tool], temperature=0 ))print(response.text)# Output: The sum of the first 50 prime numbers is 5117.
for part in response.candidates[0].content.parts: if part.executable_code: print("Generated Code:") print(part.executable_code.code)
Output:
def is_prime(n): 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 Trueprimes = []num = 2while len(primes) < 50: if is_prime(num): primes.append(num) num += 1sum_of_primes = sum(primes)print(f'{sum_of_primes=}')
for part in response.candidates[0].content.parts: if part.code_execution_result: print(f"Output: {part.code_execution_result.output}") print(f"Outcome: {part.code_execution_result.outcome}")
prompt = """A bat and a ball cost $1.10 in total. The bat costs $1.00 more than the ball.How much does the ball cost?Solve this step-by-step with code."""response = client.models.generate_content( model="gemini-3-flash-preview", contents=prompt, config=GenerateContentConfig( tools=[code_execution_tool], temperature=0 ))print(response.text)
prompt = """Generate random sales data for 12 months and:1. Calculate total annual revenue2. Find the best performing month3. Calculate month-over-month growth rate4. Identify any trendsUse code to analyze the data."""response = client.models.generate_content( model="gemini-3-flash-preview", contents=prompt, config=GenerateContentConfig( tools=[code_execution_tool] ))print(response.text)
Maintain context across multiple code-related queries:
chat = client.chats.create( model="gemini-3-flash-preview", config=GenerateContentConfig( tools=[code_execution_tool], temperature=0 ))# First queryresponse = chat.send_message( "Write a function that checks if a year is a leap year.")print("Assistant:", response.text)# Follow-up with contextresponse = chat.send_message( "Now write unit tests for that function.")print("\nAssistant:", response.text)# Another follow-upresponse = chat.send_message( "Test it with the years 2000, 2020, 2024, and 1900.")print("\nAssistant:", response.text)
Stream responses as code is generated and executed:
for chunk in client.models.generate_content_stream( model="gemini-3-flash-preview", contents="Calculate the factorial of 20. Show your work with code.", config=GenerateContentConfig( tools=[code_execution_tool], temperature=0 )): print(chunk.text, end="")
prompt = """I have the following sales data:- January: $45,000- February: $52,000- March: $48,000- April: $61,000- May: $58,000- June: $63,000Analyze this data:1. Calculate average monthly sales2. Find percentage growth from January to June3. Identify which month had the highest growth rate4. Project sales for July based on the trendUse Python code to perform the analysis."""response = client.models.generate_content( model="gemini-3-flash-preview", contents=prompt, config=GenerateContentConfig( tools=[code_execution_tool] ))print(response.text)
prompt = """Implement the quicksort algorithm in Python.Test it with the array [64, 34, 25, 12, 22, 11, 90]Verify the result is correctly sorted."""response = client.models.generate_content( model="gemini-3-flash-preview", contents=prompt, config=GenerateContentConfig( tools=[code_execution_tool] ))print(response.text)
prompt = """Implement Dijkstra's shortest path algorithm.Create a sample graph with 5 nodes and find the shortestpath from node A to node E.Show the path and its total distance."""response = client.models.generate_content( model="gemini-3-flash-preview", contents=prompt, config=GenerateContentConfig( tools=[code_execution_tool] ))print(response.text)
prompt = """Generate 1000 random data points from a normal distributionwith mean=100 and std=15.Then:1. Calculate mean, median, and standard deviation2. Find the 95th percentile3. Count how many values are within 1 standard deviation4. Verify this matches the 68-95-99.7 ruleUse Python code with numpy."""response = client.models.generate_content( model="gemini-3-flash-preview", contents=prompt, config=GenerateContentConfig( tools=[code_execution_tool] ))print(response.text)
prompt = """Write a function to calculate fibonacci numbers recursively.Test it with n=10 and n=20.If there are any issues, fix them."""response = client.models.generate_content( model="gemini-3-flash-preview", contents=prompt, config=GenerateContentConfig( tools=[code_execution_tool] ))print(response.text)# Model will generate code, test it, and refine if needed
Programmatically access generated code and results:
response = client.models.generate_content( model="gemini-3-flash-preview", contents="Calculate pi using the Monte Carlo method with 10000 samples.", config=GenerateContentConfig( tools=[code_execution_tool] ))code = Noneresult = Nonefor part in response.candidates[0].content.parts: if part.executable_code: code = part.executable_code.code if part.code_execution_result: result = part.code_execution_result.output outcome = part.code_execution_result.outcomeprint("Generated Code:")print(code)print("\nExecution Result:")print(result)print(f"\nOutcome: {outcome}")
from google.genai.types import FunctionDeclarationget_data = FunctionDeclaration( name="get_sales_data", description="Retrieve sales data from database", parameters={"type": "object", "properties": {}})response = client.models.generate_content( model="gemini-3-flash-preview", contents="Get the sales data and analyze trends using Python.", config=GenerateContentConfig( tools=[ code_execution_tool, Tool(function_declarations=[get_data]) ] ))
from google.genai.types import Partresponse = client.models.generate_content( model="gemini-3-flash-preview", contents=[ Part.from_uri( file_uri="gs://samples/data-chart.png", mime_type="image/png" ), "Extract the data points from this chart and calculate the trend line." ], config=GenerateContentConfig( tools=[code_execution_tool] ))print(response.text)
"Calculate the sum of prime numbers less than 100. Generate and run code.""Write a function to reverse a string. Test it with 'hello world'.""Analyze this dataset and find outliers using statistical methods."
❌ Less Effective:
"Do some math" # Too vague"Write code" # No clear objective"Calculate" # Missing specifics
prompt = """This code has a bug:```pythondef fibonacci(n): if n <= 0: return 0 if n == 1: return 1 return fibonacci(n-1) + fibonacci(n-2)print(fibonacci(50)) # This takes forever!
Find the problem and provide an optimized solution.
Test both versions.
"""response = client.models.generate_content(
model=“gemini-3-flash-preview”,
contents=prompt,
config=GenerateContentConfig(
tools=[code_execution_tool]
)
)print(response.text)
## Next Steps<CardGroup cols={2}> <Card title="Function Calling" icon="function" href="/gemini/function-calling"> Combine code execution with external APIs </Card> <Card title="Multimodal" icon="images" href="/gemini/multimodal"> Generate code from images and diagrams </Card> <Card title="Context Caching" icon="database" href="/gemini/context-caching"> Cache large code contexts </Card> <Card title="Grounding" icon="anchor" href="/gemini/grounding"> Ground code generation in documentation </Card></CardGroup>## Resources- [Code Execution Documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/code-execution)- [Python Standard Library](https://docs.python.org/3/library/)- [Best Practices Guide](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/code-generation-prompts)