> ## 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 PDF 理解与结构化提取

> 更新日期：2026-06-06

# Gemini 文档理解

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

截至 2026 年 3 月 23 日，Crazyrouter 生产环境已验证 `gemini-3.1-pro` 支持：

* 通过 `inlineData` 读取 PDF
* 结合 `responseMimeType` + `responseSchema` 输出可解析 JSON

***

## PDF 摘要

下面的请求在生产环境成功读取 PDF，并返回摘要文本：

```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": "Summarize the attached PDF in one sentence."},
          {
            "inlineData": {
              "mimeType": "application/pdf",
              "data": "JVBERi0xLjQK..."
            }
          }
        ]
      }
    ]
  }'
```

本次生产环境测试中，模型成功返回了对 PDF 内容的单句摘要。

***

## PDF + 结构化输出

下面的请求在生产环境成功返回了可直接 `json.loads()` 的 JSON 文本：

```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": "Extract the key topic and whether the document mentions gamma."},
          {
            "inlineData": {
              "mimeType": "application/pdf",
              "data": "JVBERi0xLjQK..."
            }
          }
        ]
      }
    ],
    "generationConfig": {
      "responseMimeType": "application/json",
      "responseSchema": {
        "type": "object",
        "properties": {
          "topic": {"type": "string"},
          "mentions_gamma": {"type": "boolean"}
        },
        "required": ["topic", "mentions_gamma"]
      }
    }
  }'
```

生产环境实际返回示例：

```json theme={null}
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "{\n  \"topic\": \"Crazyrouter PDF test document\",\n  \"mentions_gamma\": false\n}"
          }
        ]
      }
    }
  ]
}
```

对应代码里直接解析即可：

```python theme={null}
import json

text = response["candidates"][0]["content"]["parts"][0]["text"]
data = json.loads(text)
```

<Note>
  当前页面只确认了 PDF 输入和 JSON 结构化输出这两条链路。更复杂的表格抽取、长文档对比等场景，建议先用你自己的真实样本再做一次生产验证。
</Note>

<Warning>
  大型 PDF 会显著增加输入 Token。正式接入前，建议先用小文档验证字段和提示词，再逐步扩大输入规模。
</Warning>
