Gemini モデルには、OpenAI ライブラリ(Python、TypeScript、JavaScript)と REST API を使用してアクセスできます。Gemini Enterprise Agent Platform の OpenAI ライブラリを使用してサポートされるのは、 Cloud de Confiance by S3NS Auth のみです。OpenAI ライブラリを使用していない場合は、Gemini API を直接呼び出します。OpenAI ライブラリを使用していて、Agent Platform SDK に移行する場合は、OpenAI SDK から Google Gen AI SDK に移行するをご覧ください。
Python
import openai
from google.auth import default
import google.auth.transport.requests
# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "global"
# Programmatically get an access token
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
credentials.refresh(google.auth.transport.requests.Request())
# OpenAI Client
client = openai.OpenAI(
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
api_key=credentials.token
)
response = client.chat.completions.create(
model="google/gemini-3.5-flash",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain to me how AI works"}
]
)
print(response.choices[0].message)
標準の OpenAI クライアント構成との違いは次のとおりです。
api_key=credentials.token:Cloud de Confiance by S3NS アプリケーションのデフォルト認証情報から取得した OAuth アクセス トークンを使用します。base_url: OpenAI ライブラリに、デフォルトの URL ではなく Agent Platform の OpenAI 互換エンドポイントにリクエストを送信するように指示します。model="google/gemini-3.5-flash": Agent Platform でホストされている互換性のある Gemini モデルを選択します。
処理中
Gemini 3 モデルは、回答を返す前に内部推論プロセスを生成し、複雑な複数ステップのタスクのパフォーマンスを向上させます。Gemini API では、thinking_level パラメータを使用して、個別の階層(MINIMAL、LOW、MEDIUM、HIGH)の推論の深さを制御します。
OpenAI 互換の Chat Completions API では、reasoning_effort パラメータ("minimal"、"low"、"medium"、または "high")を使用して推論の深さを制御します。このパラメータは、Gemini 3 モデルの対応する thinking_level 設定にマッピングされます。
reasoning_effort を省略すると、モデルのデフォルトの思考構成が使用されます。
OpenAI 互換 API から思考構成パラメータを直接制御するには、extra_body.google.thinking_config を使用します。
Python
import openai
from google.auth import default
import google.auth.transport.requests
# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "global"
# Programmatically get an access token
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
credentials.refresh(google.auth.transport.requests.Request())
# OpenAI Client
client = openai.OpenAI(
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
api_key=credentials.token
)
response = client.chat.completions.create(
model="google/gemini-3.5-flash",
reasoning_effort="low",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": "Explain to me how AI works"
}
]
)
print(response.choices[0].message)
ストリーミング
Gemini API はストリーミング レスポンスをサポートしています。
Python
import openai
from google.auth import default
import google.auth.transport.requests
# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "global"
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
credentials.refresh(google.auth.transport.requests.Request())
client = openai.OpenAI(
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
api_key=credentials.token
)
response = client.chat.completions.create(
model="google/gemini-3.5-flash",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
stream=True
)
for chunk in response:
print(chunk.choices[0].delta)
関数呼び出し
関数呼び出しを使用すると、生成モデルから構造化データ出力を簡単に取得できます。これは Gemini API でサポートされています。
Python
import openai
from google.auth import default
import google.auth.transport.requests
# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "global"
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
credentials.refresh(google.auth.transport.requests.Request())
client = openai.OpenAI(
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
api_key=credentials.token
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. Chicago, IL",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
}
]
messages = [{"role": "user", "content": "What's the weather like in Chicago today?"}]
response = client.chat.completions.create(
model="google/gemini-3.5-flash",
messages=messages,
tools=tools,
tool_choice="auto"
)
print(response)
画像理解
Gemini モデルはネイティブにマルチモーダルであり、多くの一般的なビジョン タスクをサポートしています。
Python
from google.auth import default
import google.auth.transport.requests
import base64
from openai import OpenAI
# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "global"
# Programmatically get an access token
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
credentials.refresh(google.auth.transport.requests.Request())
# OpenAI Client
client = openai.OpenAI(
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
api_key=credentials.token,
)
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Getting the base64 string
# base64_image = encode_image("Path/to/image.jpeg")
response = client.chat.completions.create(
model="google/gemini-3.5-flash",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?",
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
},
},
],
}
],
)
print(response.choices[0])
画像を生成
REST
リクエストのデータを使用する前に、次のように置き換えます。
- PROJECT_ID: 実際のプロジェクト ID。.
リクエストを送信するには、次のいずれかのオプションを展開します。
次のような JSON レスポンスが返されます。
{
"choices": [{
"finish_reason": "stop",
"index": 0,
"image": {
"data":"IMAGE_DATA",
"extra_content": {
"google": {
"mime_type":"image/png"
}
}
},
"content":"Here is an image of a banana: ",
"role":"assistant"
}],
"created":1757099999,
"id":"sample_response_id",
"model":"google/gemini-3.1-flash-image",
"object":"chat.completion",
"system_fingerprint":"",
"usage": {
"completion_tokens":1299,
"prompt_tokens":7,
"total_tokens":1306
}
}
音声の理解
次のサンプルは、音声入力を分析する方法を示しています。
Python
from google.auth import default
import google.auth.transport.requests
import base64
from openai import OpenAI
# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "global"
# Programmatically get an access token
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
credentials.refresh(google.auth.transport.requests.Request())
# OpenAI Client
client = openai.OpenAI(
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
api_key=credentials.token,
)
with open("/path/to/your/audio/file.wav", "rb") as audio_file:
base64_audio = base64.b64encode(audio_file.read()).decode('utf-8')
response = client.chat.completions.create(
model="google/gemini-3.5-flash",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Transcribe this audio",
},
{
"type": "input_audio",
"input_audio": {
"data": base64_audio,
"format": "wav"
}
}
],
}
],
)
print(response.choices[0].message.content)
構造化出力
Gemini モデルは、定義した構造で JSON オブジェクトを出力できます。
Python
from google.auth import default
import google.auth.transport.requests
from pydantic import BaseModel
from openai import OpenAI
# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "global"
# Programmatically get an access token
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
credentials.refresh(google.auth.transport.requests.Request())
# OpenAI Client
client = openai.OpenAI(
base_url=f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
api_key=credentials.token,
)
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
completion = client.beta.chat.completions.parse(
model="google/gemini-3.5-flash",
messages=[
{"role": "system", "content": "Extract the event information."},
{"role": "user", "content": "John and Susan are going to an AI conference on Friday."},
],
response_format=CalendarEvent,
)
print(completion.choices[0].message.parsed)
現在の制限
OpenAI 互換 API を使用する場合は、次の制限事項を考慮してください。
- アクセス トークンの有効期限はデフォルトで 1 時間です。有効期限が切れた場合は、更新する必要があります。詳細については、認証情報を更新するをご覧ください。
次のステップ
Google 生成 AI ライブラリを使用して、Gemini モデルを直接呼び出します。
OpenAI 互換の構文による Chat Completions API の使用例を確認する。
サポートされている Gemini モデルとパラメータについては、Agent Platform で OpenAI ライブラリを使用するをご覧ください。