|
|
Gemini Code Execution Tool
Author: Venkata Sudhakar
Gemini code execution is a built-in tool that lets the model write Python code, run it in a secure Google-managed sandbox, observe the output, and iterate until it has the correct answer. This is transformative for analytical questions: instead of approximating a growth percentage Gemini actually computes it. Instead of estimating compound interest it writes the formula and runs it. The sandbox includes numpy, pandas, and matplotlib - making it capable of real data analysis, statistical calculations, and even chart generation. Enable code execution by passing types.Tool(code_execution=types.CodeExecution()) in the tools list. Gemini automatically decides when to write code - for factual questions it answers directly, for anything requiring calculation it writes and runs Python. The response includes executable_code blocks showing what Python was written and code_execution_result blocks showing the output. This is the right tool for any business analyst workflow where you need precise computed answers rather than LLM estimates. The below example shows a retail sales analyst asking questions about monthly revenue data - Gemini writes and runs the Python to give exact numbers for growth rates, averages, and revenue targets rather than approximations.
It gives the following output with Gemini running Python and showing exact computed answers,
Q1: Total FY revenue and monthly average?
[Gemini writes and runs Python:]
monthly = [4250000,6230000,3980000,4120000,4780000,5340000,
7820000,9650000,8430000,4250000,3890000,5120000]
total = sum(monthly)
avg = total / len(monthly)
print(f"Total: Rs {total:,.0f}")
print(f"Average monthly: Rs {avg:,.0f}")
[Output:]
Total: Rs 63,960,000
Average monthly: Rs 5,330,000
Total FY 2024-25 revenue: Rs 6.396 crore
Average monthly revenue: Rs 53.3 lakh
Q2: Peak month and how far above average?
[Code computes peak index and percentage deviation]
Peak month: November 2024 with Rs 96.5 lakh revenue
81.2% above the annual monthly average of Rs 53.3 lakh
Q3: 20pct growth target for FY 2025-26?
[Code: target_total = 63960000 * 1.20 / 12]
Monthly revenue target: Rs 63.96 lakh per month
(Total annual target: Rs 7.675 crore, a 20% increase)
# Every answer is COMPUTED not estimated
# Gemini wrote and ran Python, showed the code, and gave exact results
Gemini can also generate ASCII visualisations from data using code execution,
Monthly Revenue Chart (each * = Rs 5 lakh)
Apr: ********* (42.5L)
May: ************ (62.3L)
Jun: ******** (39.8L)
Oct: **************** (78.2L)
Nov: ******************* (96.5L) PEAK
Dec: ***************** (84.3L)
Avg: ---------- (53.3L)
# Code execution sandbox is secure - no file system or network access
# Perfect for business analysts asking ad-hoc questions without writing code themselves
Code execution is ideal for: precise financial calculations (compound interest, EMI, tax computation), sales data analysis (growth rates, averages, rankings), unit conversions and formula applications, and any question where the answer must be exact rather than approximate. The sandbox is stateless - each generate_content call starts fresh with no memory of previous code runs. For multi-step analysis where each calculation builds on the previous, include all context and data in a single generate_content call rather than chaining separate calls. Code execution works seamlessly alongside other Gemini features like function calling and grounding - combine them for maximum analytical power.
|
|