Starting April 29, 2025, Gemini 1.5 Pro and Gemini 1.5 Flash models are not available in projects that have no prior usage of these models, including new projects. For details, seeModel versions and lifecycle.
Stay organized with collectionsSave and categorize content based on your preferences.
The Gemini API code execution feature enables the model to generate and run
Python code and learn iteratively from the results until it arrives at a final
output. You can use this code execution capability to build applications that
benefit from code-based reasoning and that produce text output. For example,
you could use code execution in an application that solves equations or
processes text.
The Gemini API provides code execution as a tool, similar tofunction calling. After
you add code execution as a tool, the model decides when to use it.
To enable code execution, specify a code executiontoolin your request.
CodeExecution
Tool that executes code generated by the model, and automatically returns the result to the model. See alsoExecutableCodeandCodeExecutionResultwhich are input and output to this tool.
Part
executable_code
Optional:ExecutableCode
Code generated by the model that is meant to be executed.See Code Execution [API].
code_execution_result
Optional:CodeExecutionResult
Result of executing the [ExecutableCode].See Code Execution [API].
ExecutableCode
language
Required:string (enum)
Supported programming languages for the generatedcode.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-03 UTC."],[],[],null,["# Execute code with the Gemini API\n\nThe Gemini API code execution feature enables the model to generate and run\nPython code and learn iteratively from the results until it arrives at a final\noutput. You can use this code execution capability to build applications that\nbenefit from code-based reasoning and that produce text output. For example,\nyou could use code execution in an application that solves equations or\nprocesses text.\n\nThe Gemini API provides code execution as a tool, similar to\n[function calling](/vertex-ai/generative-ai/docs/model-reference/function-calling). After\nyou add code execution as a tool, the model decides when to use it.\n\nSupported models\n----------------\n\n- [Gemini 2.5 Flash-Lite](/vertex-ai/generative-ai/docs/models/gemini/2-5-flash-lite)\n- [Gemini 2.0 Flash with Live API](/vertex-ai/generative-ai/docs/models/gemini/2-0-flash#live-api) (Preview)\n- [Vertex AI Model Optimizer](/vertex-ai/generative-ai/docs/model-reference/vertex-ai-model-optimizer) (Experimental)\n- [Gemini 2.5 Pro](/vertex-ai/generative-ai/docs/models/gemini/2-5-pro)\n- [Gemini 2.5 Flash](/vertex-ai/generative-ai/docs/models/gemini/2-5-flash)\n- [Gemini 2.0 Flash](/vertex-ai/generative-ai/docs/models/gemini/2-0-flash)\n\n### Limitations\n\n- The feature doesn't support file I/O.\n- Code execution can run for a maximum of 30 seconds before timing out.\n\nExample syntax\n--------------\n\n### curl\n\n```bash\nPROJECT_ID = myproject\nREGION = us-central1\nMODEL_ID = gemini-2.0-flash-001\n\nhttps://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/publishers/google/models/${MODEL_ID}:generateContent \\\n -d '{\n \"contents\": [{\n ...\n }],\n \"tools\": [{\n \"code_execution\": {}\n }]\n }'\n```\n\nParameter list\n--------------\n\nSee [examples](#examples) for implementation details.\n\n### Python\n\nTo enable code execution, specify a code execution `tool` in your request.\n\n#### `CodeExecution`\n\nTool that executes code generated by the model, and automatically returns the result to the model. See also [ExecutableCode](#executablecode) and [CodeExecutionResult](#codeexecutionresult) which are input and output to this tool.\n\n#### `Part`\n\n#### `ExecutableCode`\n\n#### `CodeExecutionResult`\n\nExamples\n--------\n\nHere are illustrations of how you can submit a query and function declarations to the model.\n\n### Basic use case\n\n### curl\n\n```bash\nPROJECT_ID = myproject\nREGION = us-central1\nMODEL_ID = gemini-2.0-flash-001\n\ncurl -X POST \\\n -H \"Authorization: Bearer $(gcloud auth print-access-token)\" \\\n -H \"Content-Type: application/json\" \\\n https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/publishers/google/models/${MODEL_ID}:generateContent \\\n -d '{\n \"contents\": [{\n \"role\": \"user\",\n \"parts\": [{\n \"text\": \"Calculate 20th fibonacci number. Then find the nearest palindrome to it.\"\n }]\n }],\n \"tools\": [{'codeExecution': {}}],\n }'\n```\n\n### Python\n\n```python\nfrom google import genai\nfrom google.genai.types import Tool, ToolCodeExecution, GenerateContentConfig\n\nclient = genai.Client()\nmodel_id = \"gemini-2.0-flash-001\"\n\ncode_execution_tool = Tool(\n code_execution=ToolCodeExecution()\n)\nresponse = client.models.generate_content(\n model=model_id,\n contents=\"Calculate 20th fibonacci number. Then find the nearest palindrome to it.\",\n config=GenerateContentConfig(\n tools=[code_execution_tool],\n temperature=0,\n ),\n)\nfor part in response.candidates[0].content.parts:\n if part.executable_code:\n print(part.executable_code)\n if part.code_execution_result:\n print(part.code_execution_result)\n# Example response:\n# code='...' language='PYTHON'\n# outcome='OUTCOME_OK' output='The 20th Fibonacci number is: 6765\\n'\n# code='...' language='PYTHON'\n# outcome='OUTCOME_OK' output='Lower Palindrome: 6666\\nHigher Palindrome: 6776\\nNearest Palindrome to 6765: 6776\\n'\n```\n\n### Enable code execution on the model\n\nTo enable basic code execution, see [Code execution](/vertex-ai/generative-ai/docs/multimodal/code-execution#enable_code_execution_on_the_model).\n\nWhat's next\n-----------\n\n- Learn more about the [Gemini\n API](/vertex-ai/generative-ai/docs/model-reference/gemini).\n- Learn more about [Function\n calling](/vertex-ai/generative-ai/docs/multimodal/function-calling).\n- Learn more about [Generating content with Gemini](/vertex-ai/generative-ai/docs/model-reference/inference)."]]