> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crazyrouter.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini 工具调用

> 基于 2026-03-23 Crazyrouter 生产环境验证的 Gemini 原生工具能力

> 更新日期：2026-06-06

# Gemini 工具调用

```
POST /v1beta/models/{model}:generateContent
```

截至 2026 年 3 月 23 日，Crazyrouter 生产环境已验证 `gemini-3.1-pro` 可以稳定触发以下 Gemini 原生工具能力：

* `codeExecution`
* `googleSearch`
* `urlContext`
* `functionDeclarations`

<Note>
  本页只保留已经在生产环境实际触发成功的请求模式和响应标记。
</Note>

***

## 代码执行

下面的请求在生产环境返回了 `executableCode` 和 `codeExecutionResult` 两个关键响应块：

```bash theme={null}
curl "https://api.crazyrouter.com/v1beta/models/gemini-3.1-pro:generateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          {"text": "Use code execution. Compute the sum of the first 20 prime numbers and state the result."}
        ]
      }
    ],
    "tools": [
      {
        "codeExecution": {}
      }
    ]
  }'
```

生产环境实测到的关键响应形态：

```json theme={null}
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "executableCode": {
              "language": "PYTHON",
              "code": "..."
            }
          },
          {
            "codeExecutionResult": {
              "outcome": "OUTCOME_OK",
              "output": "639\n"
            }
          },
          {
            "text": "The sum is 639."
          }
        ]
      }
    }
  ]
}
```

***

## Google 搜索

下面的请求在生产环境返回了 `groundingMetadata`，说明模型确实触发了 Google Search：

```bash theme={null}
curl "https://api.crazyrouter.com/v1beta/models/gemini-3.1-pro:generateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          {"text": "Use Google Search. What are two AI news items from March 2026?"}
        ]
      }
    ],
    "tools": [
      {
        "googleSearch": {}
      }
    ]
  }'
```

当前可据此检查：

* `candidates[0].groundingMetadata`
* 正常的文本回答仍在 `candidates[0].content.parts[*].text`

***

## URL 上下文

下面的请求在生产环境返回了 `urlContextMetadata`，同时也带有 `groundingMetadata`：

```bash theme={null}
curl "https://api.crazyrouter.com/v1beta/models/gemini-3.1-pro:generateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          {"text": "Use URL context to summarize https://example.com/ in one sentence."}
        ]
      }
    ],
    "tools": [
      {
        "urlContext": {}
      }
    ]
  }'
```

当前可据此检查：

* `candidates[0].urlContextMetadata`
* `candidates[0].groundingMetadata`

***

## 自定义函数调用

下面的请求在生产环境返回了 `functionCall`：

```bash theme={null}
curl "https://api.crazyrouter.com/v1beta/models/gemini-3.1-pro:generateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          {"text": "What is the weather in Beijing? Use the provided function."}
        ]
      }
    ],
    "tools": [
      {
        "functionDeclarations": [
          {
            "name": "get_weather",
            "description": "Get weather info for a city",
            "parameters": {
              "type": "object",
              "properties": {
                "city": {
                  "type": "string"
                }
              },
              "required": ["city"]
            }
          }
        ]
      }
    ]
  }'
```

关键响应形态：

```json theme={null}
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "functionCall": {
              "name": "get_weather",
              "args": {
                "city": "Beijing"
              }
            }
          }
        ]
      }
    }
  ]
}
```

函数结果回传时，继续使用 Gemini 原生的 `functionResponse` 结构即可。
