結論先說
- 用
response_schema替代口頭約束:在LocalAgentConfig(response_schema=YourModel)傳入 PydanticBaseModel,即可在底層收斂輸出結構。 - 用
await response.structured_output()直接取值:無需對response.text()去噪和json.loads,即可獲取強類型結構化數據。 - 支持工具調用與多模態附件協同:Agent 可先檢索倉庫,再結合
from_file("spec.pdf")、Image.from_file("chart.png")按 Schema 輸出。 - 純條件分流可優先用
Decisions API:僅需是/否、多選或評分路由時,Decisions API(gemini-decision-1)產生 0 文本輸出 Token。 - 批量抽取長文檔推薦高配額度:搭配 Gemini 4 Neon/Carbon 處理大上下文時,可關注 Gemini Google AI Ultra 5x/20x 充值指南 與 Gemini Google AI Ultra 5x 人工 iOS。

圖:本站根據 Google Antigravity SDK Structured Output 文檔 整理的示意圖
本文根據 Google Antigravity SDK 與 Gemini API 官方文檔整理,資料核對於 2026-12-03。配圖為本站整理的示意圖,不是介面截圖。本站(bigolab.com)由益陽市赫山區必高科技有限公司獨立運營,是獨立第三方服務商,不是 Google 官方網站。
適用於誰
- 需把 Agent 分析結果直接寫入數據庫或工單系統的自動化開發者。
- 希望告別正則截取 JSON、消除
JSONDecodeError的 Python 工程師。 - 需傳入 PDF 規格與介面截圖並按固定字段返回核對結果的多模態開發者。
一、三種結構化結果獲取方式對比
| 維度 | Prompt + 正則提取 | SDK response_schema | Decisions API (gemini-decision-1) |
|---|---|---|---|
| 接口 | await response.text() | response_schema=Model | noul / choice / score |
| 穩定性 | 易受寒暄或截斷影響 | Schema 約束,結構穩定 | 校準概率與離散值 |
| 工具鏈 | 支持 Agent 工具 | 先調工具再按 Schema 收斂 | 單次前向並行評估 |
| 輸出量 | 含多餘解釋文本 | 按字段輸出緊湊 JSON | 始終 0 文本輸出 Token |
需要“多步讀取文件後再產出結構化報告”的場景,優先用 response_schema。

圖:本站根據 Gemini API 官方文檔 整理的示意圖
二、Pydantic 嵌套模型與多模態抽取示例
以下聯合讀取 PDF 與截圖,並用 VisualAuditReport.model_validate 校驗:
import asyncio
from typing import Literal
import pydantic
from google.antigravity import Agent, LocalAgentConfig
from google.antigravity.types import Image, from_file
class FindingItem(pydantic.BaseModel):
component: str = pydantic.Field(description="受影響的頁面組件名")
severity: Literal["high", "medium", "low"]
discrepancy: str = pydantic.Field(description="截圖與規格不一致的描述")
class VisualAuditReport(pydantic.BaseModel):
passed: bool
summary: str
findings: list[FindingItem]
async def main():
config = LocalAgentConfig(
response_schema=VisualAuditReport,
system_instructions="你是嚴謹的 UI 驗收工程師,對照附件客觀列出差異。",
)
async with Agent(config) as agent:
spec_pdf = from_file("spec.pdf")
ui_shot = Image.from_file("chart.png")
res = await agent.chat([
"對照規格說明書核查介面截圖與要求是否一致:",
ui_shot,
spec_pdf,
])
report = VisualAuditReport.model_validate(await res.structured_output())
print(f"驗收通過: {report.passed}, 問題數: {len(report.findings)}")
if __name__ == "__main__":
asyncio.run(main())
人工核對要點:
- 為字段補充
Field(description=...),降低枚舉錯填概率。 - 用
Model.model_validate(await res.structured_output())轉成強類型實例。 - 用本地
LiteRTAgentConfig分析圖片須滿足google-antigravity >= 0.1.24與litert-lm >= 0.19.1。
訂閱與開通提示:構建長文檔抽取與 UI 驗收流水線,可選 Gemini Google AI Ultra 5x 人工 iOS 或 Gemini Google AI Ultra 20x 人工 iOS,帳號見 Google 帳號與訂閱指南;獨立測試電郵可看 谷歌電郵成品號(質保 3 天首登、封號不在質保範圍,須遵守 Google 服務條款);跨模型比對可搭 ChatGPT Plus 年費訂閱 與 Claude Pro iOS 自助充值。全站以商品頁實時標價為準,標價不含稅,開具國內增值稅發票另付 6% 稅費;單獨申請自定義明細電子收據免費,企業批量採購支持簽訂正式中文服務合同並對公結算。
常見問題
問:設定 response_schema 後,還能同時讀取 response.thoughts 嗎?
答:可以。response_schema 只約束最終輸出格式,在支持推理的模型上仍可先完成工具調用和思考,再輸出 JSON。
問:為什麼定義了深層嵌套的 Pydantic 模型後容易出現字段空缺或超時?
答:過深嵌套與過多聯合類型會增加受限解碼開銷。建議扁平化在 2 至 3 層內,並用 Literal 限定枚舉。
問:開啟 response_schema 後適合直接把未閉合 JSON 流式展示給前端用戶嗎?
答:不建議直接輸出半截 JSON。可後台等 structured_output() 完成後渲染卡片,或用 response.thoughts 作進度提示。
問:國內企業開通高配 Gemini 訂閱用於結構化抽取流水線,如何開具 6% 增值稅發票與核對售後?
答:請先瀏覽 AI 訂閱與帳號服務總覽 與 Gemini Google AI Ultra 5x/20x 充值指南 選擇對應方案。全站以商品頁實時標價為準,標價不含稅,開具國內增值稅發票另付 6% 稅費;單獨申請自定義明細電子收據免費。本站明示質保訂閱、不質保封號,請嚴格遵守服務商條款合規使用。
相關教學
- Antigravity 報錯 400 怎麼排查:projects 資源名為空、thinking 簽名損壞與參數不兼容修復
- Antigravity 2.0 混切 Gemini 4 與 Claude 模型省額度指南:長倉檢索與複雜重構分工策略
- Google AI Plus、Pro 與 Ultra 怎麼選:Gemini Notebook 筆記本來源上限與 5 小時配額對照
- Antigravity 2.0 桌面端額度消耗對比與團隊採購:Pro 與 Ultra 差異、刷新機制及 6% 增值稅發票說明
0 條評論
還沒有評論,來搶沙發~