以下の内容はhttps://nikkie-ftnext.hatenablog.com/entry/gemini-api-vertexai-controlled-generation-202501より取得しました。


Vertex AIのGemini APIで出力をJSON形式で構造化するには(google-cloud-aiplatform、google-genai)

はじめに

TAAFアニメファン賞ミリアニにお願いします! nikkieです。

OpenAIのGPTではおなじみのJSON形式出力をGeminiでどうやるのか調べています。
前回はGoogle AI篇で、今回はVertex AI篇です1

目次

ドキュメントより

google-cloud-aiplatform

cloud.google.com

JSONスキーマを表す辞書を手で書いて指定します。
同じGeminiのはずなのですが、google-generativeaiのようにTypedDictで2渡せないのが謎です(ライブラリにプルリクチャンスなのかな?)

ドキュメントには、Gemini 1.5 Flash・Gemini 1.5 Proのモデルの記載がありました。

cookbookより

google-genai

1つのライブラリで、Google AIのGemini APIもVertex AIのGemini APIも叩ける新星。
ドキュメントを参考にPydantic.BaseModelを継承したクラスを渡してみましょう。
https://googleapis.github.io/python-genai/#json-response-schema

動作確認スクリプト

Vertex AIが有効なGoogle Cloudのプロジェクトをgcloudコマンドのデフォルトprojectに設定しました。
その状態でgcloud auth application-default loginしたので、vertexai.init()projectlocationの指定無しで済んでいます。
一方、google-genaiのClientは、ADCがあってもprojectlocationの指定がいるようでした(省略したら 503 Service Unavailable になってハマりました)3

  • uv 0.5.17 (Homebrew 2025-01-10)
    • スクリプトにinline script metadataを書き、uv runで実行しています
  • Python 3.12.5
  • google-cloud-aiplatform 1.77.0
  • google-genai 0.5.0

google-cloud-aiplatformでプロンプト・Gemini 1.5

Vertex AIのドキュメントには記載がないですが、試しました。
Google AI同様にJSONのコードブロックで返ってきており、欲しい形(JSONそのもの)ではありません

※実際はバッククォートのみです(バックスラッシュはエスケープの意図です)

\`\`\`json
[
  {
    "recipe_name": "Chocolate Chip Cookies",
    "ingredients": [
      "1 cup (2 sticks) unsalted butter, softened",
      "¾ cup granulated sugar",
      "¾ cup packed brown sugar",
      "1 teaspoon pure vanilla extract",
      "2 large eggs",
      "2 ¼ cups all-purpose flour",
      "1 teaspoon baking soda",
      "1 teaspoon salt",
      "2 cups chocolate chips"
    ]
  },
  # 省略
]
\`\`\`

google-cloud-aiplatformで手書きJSONスキーマ・Gemini 1.5

JSONで得られました!

[
  {
    "recipe_name": "Chocolate Chip Cookies",
    "ingredients": [
      "1 cup (2 sticks) unsalted butter, softened",
      "1 cup granulated sugar",
      "1 cup packed brown sugar",
      "2 teaspoons pure vanilla extract",
      "2 large eggs",
      "3 cups all-purpose flour",
      "1 teaspoon baking soda",
      "1 teaspoon salt",
      "2 cups chocolate chips"
    ]
  },
  // 省略
  {
    "recipe_name": "Snickerdoodles",
    "ingredients": [
      "1 cup (2 sticks) unsalted butter, softened",
      "1 1/2 cups granulated sugar",
      "2 large eggs",
      "2 3/4 cups all-purpose flour",
      "2 teaspoons cream of tartar",
      "1 teaspoon baking soda",
      "1/4 teaspoon salt",
      "2 tablespoons granulated sugar",
      "2 teaspoons ground cinnamon"
    ]
  }
]

google-genaiでPydanticのBaseModel・Gemini 2.0

JSONで得られました!
google-genaiでlistが未サポートのようだったので、レシピの例ではなくgoogle-genaiのドキュメントの例です。

{
  "capital": "Washington D.C.",
  "continent": "North America",
  "gdp": 25000000000000,
  "name": "United States",
  "official_language": "English",
  "population": 331000000,
  "total_area_sq_mi": 3797000
}

google-genaiでGoogle AIとVertex AIのコードが統合されたのはとてもありがたいです

終わりに

Vertex AIのGemini APIJSON出力を得るには以下の方法があります。

  • google-cloud-aiplatformで手書きJSONスキーマを渡す(Gemini 1.5)
  • google-genaiでPydanticのBaseModelを継承したモデルを渡す(Gemini 2.0)

google-generativeaiはTypedDictでしたが、なんとVertex AIとは互換性がありません!(みんなちがって、みんないい...??)
google-genaiの登場でGoogle AI・Vertex AIを同じコードで叩けるようになったのは福音ですが、まだまだ発展途上のライブラリなので機能拡充が待たれます🙏(プルリクチャンス!)


  1. Gemini APIGoogle AIとVertex AIの双方から利用できます
  2. 前回記事のこの辺り Google AIのGemini APIで出力をJSON形式で構造化するには(google-generativeai、google-genai) - nikkie-ftnextの日記



以上の内容はhttps://nikkie-ftnext.hatenablog.com/entry/gemini-api-vertexai-controlled-generation-202501より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14