SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
1.3 KB · 22 lines python
Raw Blame History
1#!/usr/bin/env python32"""Code execution tool (`tools: [{"codeExecution": {}}]`): Google runs Python in a sandbox; parts come back as3executableCode{language, code, id} → codeExecutionResult{outcome, output, id} → text.4STATUS: LIVE_VERIFIED 2026-09-18 (gemini-3.5-flash-lite, tmp-live/gemini-tools/d_code_execution.json): parts5["executableCode,thoughtSignature", "codeExecutionResult", "text,thoughtSignature"], outcome OUTCOME_OK, output "4\\n",6usageMetadata.toolUsePromptTokenCount 34.7Run: .venv/bin/python examples/gemini/tools/code-execution/code_execution.py8"""9import json, sys10from pathlib import Path11sys.path.insert(0, str(Path(__file__).resolve().parents[4]))12from scripts.live import gemini_request13MODEL = "gemini-3.5-flash-lite"14st, r, _ = gemini_request("POST", f"/v1beta/models/{MODEL}:generateContent",15                          {"contents": [{"parts": [{"text": "Run print(2+2) in Python and report the output."}]}], "tools": [{"codeExecution": {}}],16                           "generationConfig": {"maxOutputTokens": 128}}, note="code_execution.py")17print("HTTP", st)18for p in r["candidates"][0]["content"]["parts"]:19    kind = next(k for k in p if k != "thoughtSignature")20    print(f"{kind:20}", json.dumps(p[kind])[:200])21print("usage:", json.dumps({k: v for k, v in r["usageMetadata"].items() if "Count" in k}))22