Google Gemini API
    Google Gemini API
    • Get API key
    • Release notes
    • Libraries
    • Run Gemini on Google Cloud
    • Model Capabilities
      • Overview
      • Long context
      • Structured output
      • Document understanding
      • Image understanding
      • Video understanding
      • Audio understanding
      • Text generation
        • Text input
        • Image input
        • Streaming output
        • Multi-turn conversations
        • Multi-turn conversations (Streaming)
        • Configuration parameters
      • Generate images
        • Generate images using Gemini
        • Image editing with Gemini
        • Generate images using Imagen 3
      • Gemini thinking
        • Use thinking models
        • Set budget on thinking models
      • Function calling
        • Function Calling with the Gemini API
    • models
      • All Model
      • Pricing
      • Rate limits
      • Billing info
    • Safety
      • Safety settings
      • Safety guidance

    Run Gemini on Google Cloud

    Migrate to the Cloud#

    In this guide, learn why you might want to migrate your development projects to Google Cloud and how to migrate your Python code from the Google AI Gemini API to the Gemini API on Vertex AI.

    Why Migrate to Cloud#

    If you're new to Gemini, refer to the Quickstart and samples in Google AI Studio to get started quickly.
    However, as your generative AI solutions mature, you might need a platform to build and deploy end-to-end generative AI applications and solutions. Google Cloud provides a comprehensive ecosystem of tools that empower developers to harness the power of generative AI, from the initial stages of application deployment to managing complex data at scale. Google Cloud's Vertex AI platform offers a suite of MLOps tools that streamline the consumption, deployment, and monitoring of AI models to improve efficiency and reliability.
    The following table summarizes the key differences between Google AI and Vertex AI to help you determine which option is suitable for your use case:
    FeatureGoogle AI Gemini APIGoogle Cloud Vertex AI Gemini API
    Latest Gemini ModelsGemini Pro and Gemini UltraGemini Pro and Gemini Ultra
    Sign-upGoogle AccountGoogle Cloud Account (including Terms Agreement and Billing)
    AuthenticationAPI KeyGoogle Cloud Service Account
    PlaygroundGoogle AI StudioVertex AI Studio
    API and SDKPython, Node.js, Android (Kotlin/Java), Swift, GoSDK supports Python, Node.js, Java, Go
    Free TierYes$300 Google Cloud credits for new users
    Quota (Requests Per Minute)60 (can request increase)Increase on request (default: 60)
    Enterprise SupportNoData privacy commitments, customer-managed encryption keys, Virtual Private Cloud (VPC), data residency, access transparency
    MLOpsNoFull MLOps on Vertex AI (e.g., model evaluation, model monitoring, model registry)
    Here are other considerations to keep in mind when migrating:
    You can use an existing Google Cloud project (i.e., the project you used to generate the API key) or create a new Google Cloud project.
    The regions supported by Google AI Studio and Vertex AI may vary. Refer to the list of Google Cloud regions that support generative AI.
    Any models you created in Google AI Studio will need to be retrained in Vertex AI.
    Additionally, integration with databases, DevOps tools, logging, monitoring, and IAM provides a comprehensive approach to managing the entire generative AI lifecycle.
    Here are some examples of common use cases that are well-suited for Google Cloud products.
    Put your applications and solutions into production. With products such as Cloud Functions and Cloud Run, you can deploy applications with enterprise-grade scale, security, and privacy. For more information about security and privacy, see the Security, Privacy, and Cloud Compliance on Google Cloud guide.
    Achieve end-to-end MLOps capabilities with Vertex AI, from tuning to vector similarity search and machine learning pipelines.
    Trigger LLM (Large Language Model) calls using event-driven architecture with Cloud Functions or Cloud Run.
    Monitor application usage with Cloud Logging and BigQuery.
    Store data at scale with enterprise-grade security using services such as BigQuery, Cloud Storage, and Cloud SQL.
    Perform Retrieval Augmented Generation (RAG) using data in the cloud via BigQuery or Cloud Storage.
    Create and schedule data pipelines. You can schedule jobs using Cloud Scheduler.
    Bring LLMs to your data in the cloud. If you have data stored in Cloud Storage or BigQuery, you can prompt LLMs to process that data. For example, extract information, summarize information, or ask questions.
    Leverage Google Cloud's data governance/residency policies to manage the data lifecycle.

    Migrate from Gemini on Google AI to Vertex AI#

    This section describes how to migrate from using Google AI Gemini to Google Cloud's Vertex AI Gemini.

    Get Started with Vertex AI Studio#

    The easiest way to get started with Gemini through Vertex AI is to use Vertex AI Studio.
    1.
    If you previously created an API key through Google AI Studio, a Google Cloud project has already been created for you, and you can use the same project. To find your Google Cloud project, go to API Keys in Google AI Studio.
    image.png
    1.
    Otherwise, you will need to create a Google Cloud project and enable the Vertex AI API. For instructions, see the Set up your project guide.
    2.
    Go to the Google Cloud Console.
    3.
    Enable billing for your Google Cloud project, if you haven't already. New Google Cloud users get [$300] in free credits.
    4.
    Go to Vertex AI Studio via this link. You can also search for "Vertex AI Studio" in the search bar.
    Now you can start using Vertex AI Studio. For more information, see the Vertex AI Studio documentation.

    Python: Migrate from Google AI Gemini API to Vertex AI Gemini API#

    The following sections show code snippets to help you migrate your Python code to use the Vertex AI Gemini API.

    Vertex AI Python SDK Setup#

    On Vertex AI, you do not need an API key. Gemini on Vertex AI uses IAM (Identity and Access Management) access controls that govern what users, groups, or service accounts can do when calling the Gemini API through the Vertex AI SDK.
    There are many ways to authenticate. You can follow this decision tree to choose the appropriate authentication method for your use case.
    To make inference calls to Vertex AI, you must also ensure that you have enabled Vertex AI User.

    Code Example for Installing the Client#

    Google AIVertex AI
    pythonpip install google-generativeaifrom google.generativeai import GenerativeModelfrom google.colab import userdatagenai.configure(userdata.get('API_KEY'))pythonpip install google-cloud-aiplatformimport vertexaifrom google.cloud.aiplatform.private_preview.generative_models import GenerativeModel, ImagePROJECT_ID = ""REGION = "" # e.g. us-central1vertexai.init(project=PROJECT_ID, location=REGION)

    Code Example for Generating Text from a Text Prompt#

    Google AIVertex AI
    pythonmodel = GenerativeModel('gemini-pro')response = model.generate_content('The opposite of hot is')print(response.text) # The opposite of hot is cold.pythonmodel = GenerativeModel('gemini-pro')response = model.generate_content('The opposite of hot is')print(response.text) # The opposite of hot is cold.

    Code Example for Generating Text from Text and Image#

    Google AIVertex AI
    pythonimport PIL.Imagemultimodal_model = GenerativeModel('gemini-pro-vision')image = PIL.Image.open('image.jpg')response = multimodal_model.generate_content(['What is this picture?', image])print(response.text) # A cat is shown in this picture.pythonmultimodal_model = GenerativeModel("gemini-pro-vision")image = Image.load_from_file("image.jpg")response = multimodal_model.generate_content(["What is shown in this image?", image])print(response.text) # A cat is shown in this picture.

    Code Example for Generating Multi-Turn Chat#

    Google AIVertex AI
    pythonmodel = GenerativeModel('gemini-pro')chat = model.start_chat()print(chat.send_message("How are you?").text)print(chat.send_message("What can you do?").text)pythonmodel = GenerativeModel("gemini-pro")chat = model.start_chat()print(chat.send_message("How are you?").text)print(chat.send_message("What can you do?").text)

    Delete Unused API Keys#

    If you no longer need to use the Google AI Gemini API key, follow security best practices and delete it.

    Next Steps#

    See the Vertex AI overview to learn more about generative AI solutions on Vertex AI.
    Review the Gemini API overview to dive deeper into the Google AI Gemini API
    Previous
    Libraries
    Next
    Overview
    Built with