POST
/
v1beta
/
models
/
{model}
:generateContent
curl -X POST "https://api.example.com/v1beta/models/gemini-2.5-pro:generateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          {
            "text": "Calculate the factorial of 10"
          }
        ]
      }
    ],
    "tools": [
      {
        "codeExecution": {}
      }
    ]
  }'
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "executableCode": {
              "language": "PYTHON",
              "code": "import math\nresult = math.factorial(10)\nprint(result)"
            }
          },
          {
            "codeExecutionResult": {
              "outcome": "OUTCOME_OK",
              "output": "3628800"
            }
          },
          {
            "text": "The factorial of 10 is 3,628,800."
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP"
    }
  ]
}
Official documentation: https://ai.google.dev/gemini-api/docs/code-execution
Enable Gemini models to write and execute code to solve problems, perform calculations, and process data dynamically.

Request Parameters

key
string
required
API key.
contents
array
required
Content array.Each content object contains:
  • role (string): Role (user or model)
  • parts (array): Content parts array
tools
array
Tools configuration including code execution.
  • codeExecution (object): Enable code execution capability
generationConfig
object
Generation configuration.
  • temperature (number): Sampling temperature
  • topP (number): Nucleus sampling parameter

Response

Returns the result of code execution along with any generated output.
curl -X POST "https://api.example.com/v1beta/models/gemini-2.5-pro:generateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          {
            "text": "Calculate the factorial of 10"
          }
        ]
      }
    ],
    "tools": [
      {
        "codeExecution": {}
      }
    ]
  }'
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "executableCode": {
              "language": "PYTHON",
              "code": "import math\nresult = math.factorial(10)\nprint(result)"
            }
          },
          {
            "codeExecutionResult": {
              "outcome": "OUTCOME_OK",
              "output": "3628800"
            }
          },
          {
            "text": "The factorial of 10 is 3,628,800."
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP"
    }
  ]
}