To use the OpenAI Python libraries, install the OpenAI SDK: To authenticate with the Chat Completions API, you can
either modify your client setup or change your environment
configuration to use Google authentication and a Vertex AI
endpoint. Choose whichever method that's easier, and follow the steps for
setting up depending on whether you want to call Gemini models
or self-deployed Model Garden models. Certain models in Model Garden and
supported Hugging Face models
need to be
deployed to a Vertex AI endpoint
first before they can serve requests.
When
calling these self-deployed models from the Chat Completions API, you need to
specify the endpoint ID. To list your
existing Vertex AI endpoints, use the
To programmatically get Google credentials in Python, you can use the
Before trying this sample, follow the Python setup instructions in the
Vertex AI quickstart using
client libraries.
For more information, see the
Vertex AI Python API
reference documentation.
To authenticate to Vertex AI, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
By default, access tokens last for 1 hour. You can
extend the life of your access token
or periodically refresh your token and update the Install the Google Cloud CLI. The OpenAI library can
read the To call a Gemini model, set the To call a self-deployed model from Model Garden, set the Next, initialize the client: The Gemini Chat Completions API uses OAuth to authenticate
with a
short-lived access token.
By default, access tokens last for 1 hour. You can
extend the life of your access token
or periodically refresh your token and update the The following example shows how to refresh your credentials automatically as
needed:pip install openai
gcloud ai endpoints list
command. Client setup
google-auth
Python SDK:pip install google-auth requests
Python
openai.api_key
variable. Environment variables
OPENAI_API_KEY
and OPENAI_BASE_URL
environment
variables to change the authentication and endpoint in their default client.
Set the following variables:$ export PROJECT_ID=PROJECT_ID
$ export LOCATION=LOCATION
$ export OPENAI_API_KEY="$(gcloud auth application-default print-access-token)"
MODEL_ID
variable and use the openapi
endpoint:$ export MODEL_ID=MODEL_ID
$ export OPENAI_BASE_URL="https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/openapi"
ENDPOINT
variable and use that in your URL instead:$ export ENDPOINT=ENDPOINT_ID
$ export OPENAI_BASE_URL="https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT}"
client = openai.OpenAI()
OPENAI_API_KEY
environment variable.Refresh your credentials
Python
What's next
Authenticate
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-15 UTC.