Gemini Batch API Utility
gemini-batch-api は、GoogleのGemini Batch APIを簡単に利用するためのユーティリティライブラリです。
大量のリクエストを効率的に一括処理できます。
特徴
- 簡単なリクエスト定義: テキスト、ローカルファイル、アップロードファイルの指定が容易です。
- 構造化出力のサポート: Pydanticモデルを使用して、JSON形式の結果を直接取得できます。
- 一括モニタリング: バッチジョブの完了を待機し、結果をリストとして取得します。
使い方の例
from pathlib import Path
from gemini_batch_api import Request, create_batch, get_inlined_responses
import os
# 1. クライアントを初期化
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
# 2. リクエストのリストを作成
requests = [
Request(query="富士山について教えてください。"),
Request(query="画像の内容を説明してください。", file=Path("sample.png")),
]
# 3. バッチジョブを作成
batch_name = create_batch(
client=client,
model="gemini-flash-lite-latest",
requests=requests,
display_name="my-first-batch"
)
print(f"Batch job created: {batch_name}")
# 4. 結果の取得(完了するまでポーリングし、結果をリストで返します)
responses = get_inlined_responses(client, batch_name)
if responses:
for res in responses:
if res.response:
print(res.response.text)
上記をmain.pyに保存して、以下のコマンドで実行できます(結果取得まで時間がかかることがあります)。
GEMINI_API_KEY="AI..." uv run --with gemini-batch-api main.py
構造化出力 (Structured Output)
Pydanticモデルを使用して、出力を構造化できます。
from pydantic import BaseModel, Field
class ItemInfo(BaseModel):
name: str = Field(description="商品名")
price: int = Field(description="価格")
requests = [
Request(
query="このレシートの内容を抽出して",
file=Path("receipt.jpg"),
schema=ItemInfo # Pydanticモデルを指定
),
]
# あとは同様に create_batch / get_inlined_responses を実行します
ファイルアップロード
ファイルをアップロードする場合は、Request.uploadにTrueを指定してください。
アップロードするファイル名がASCIIでない場合は、Request.upload_file_nameにASCIIのファイル名を指定してください。
Metadata
Release files for gemini-batch-api 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gemini_batch_api-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for gemini_batch_api-0.1.3-py3-none-any.whl |
Release files / gemini_batch_api-0.1.3-py3-none-any.whl
| Download URL | gemini_batch_api-0.1.3-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d4b78596c34f5834fc93e52d11fb6f3cc00fd90b4ed6e53a2244d2f678f51575
|
|
BLAKE2b-256 checksum How to use checksums |
8a5430722188bb282e8eab8687f58f71f2bb8691e03d8163ad24909bbeaa524d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|
Security for gemini-batch-api 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.