#!/usr/bin/env python3 """Code execution tool (`tools: [{"codeExecution": {}}]`): Google runs Python in a sandbox; parts come back as executableCode{language, code, id} → codeExecutionResult{outcome, output, id} → text. STATUS: LIVE_VERIFIED 2026-09-18 (gemini-3.5-flash-lite, tmp-live/gemini-tools/d_code_execution.json): parts ["executableCode,thoughtSignature", "codeExecutionResult", "text,thoughtSignature"], outcome OUTCOME_OK, output "4\\n", usageMetadata.toolUsePromptTokenCount 34. Run: .venv/bin/python examples/gemini/tools/code-execution/code_execution.py """ import json, sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[4])) from scripts.live import gemini_request MODEL = "gemini-3.5-flash-lite" st, r, _ = gemini_request("POST", f"/v1beta/models/{MODEL}:generateContent", {"contents": [{"parts": [{"text": "Run print(2+2) in Python and report the output."}]}], "tools": [{"codeExecution": {}}], "generationConfig": {"maxOutputTokens": 128}}, note="code_execution.py") print("HTTP", st) for p in r["candidates"][0]["content"]["parts"]: kind = next(k for k in p if k != "thoughtSignature") print(f"{kind:20}", json.dumps(p[kind])[:200]) print("usage:", json.dumps({k: v for k, v in r["usageMetadata"].items() if "Count" in k}))