Skip to content

Google AI — Gemini API & Vertex AI Guide

DodaTech Updated 2026-06-21 10 min read

In this tutorial, you'll learn about Google AI. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Google AI platform provides access to Gemini models and Vertex AI services for building multimodal applications with text, images, audio, and Code Generation capabilities.

What You'll Learn

  • Setting up Google AI Studio and API keys for Gemini models
  • Building text and multimodal chat applications with the Gemini API
  • Using Vertex AI for enterprise-grade model deployment and MLOps
  • Managing safety filters, grounding, and content moderation
  • Understanding pricing, quotas, and production best practices

Why Google AI Matters

Google's Gemini models compete directly with GPT-4 and Claude, offering native multimodal understanding — processing text, images, audio, and video in a single model. Vertex AI adds enterprise features like model tuning, deployment, and monitoring. DodaTech's Doda Browser uses Gemini for real-time page summarization across multiple languages, and Durga Antivirus Pro leverages Vertex AI's embedding models for zero-day malware signature detection. This guide will teach you to build production-ready AI applications on Google Cloud.

flowchart LR
    A["Google AI Studio\nAPI Key Setup"] --> B["Gemini API\nText & Chat"]
    A --> C["Gemini Vision\nMultimodal"]
    A --> D["Vertex AI\nEnterprise"]
    B --> E["Safety Filters\n& Grounding"]
    B --> F["Token Counting\n& Pricing"]
    D --> G["Model Tuning\n& Deployment"]
    style B fill:#dbeafe,stroke:#2563eb

Getting Started with Google AI Studio

Every Gemini API call needs authentication. Google offers two paths: the free-tier Gemini API via AI Studio and the enterprise Vertex AI endpoint on Google Cloud.

Obtaining an API Key

  1. Go to aistudio.google.com and sign in with your Google account.
  2. Click Get API Key in the left sidebar.
  3. Create a new key in Google Cloud Console under APIs & Services.
  4. Enable the Generative Language API.

Never hardcode keys in source code. Use environment variables.

import os
import google.generativeai as genai

genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
model = genai.GenerativeModel("gemini-1.5-pro")

Expected output: No output — the model initializes silently. If GOOGLE_API_KEY is missing, you'll see google.auth.exceptions.DefaultCredentialsError.

Choosing the Right Model

Model Best For Input Modalities Context Window
Gemini 1.5 Pro Complex reasoning, code, long documents Text, Image, Audio, Video 1M tokens
Gemini 1.5 Flash Fast, cost-effective tasks Text, Image, Audio, Video 1M tokens
Gemini 1.5 Flash-8B Lightweight, high-throughput Text, Image, Audio 1M tokens
Gemini 2.0 Flash Next-gen speed and efficiency Text, Image, Audio, Video 1M tokens

Gemini 1.5 Pro's 1-million-token context window can Process entire codebases, hour-long videos, or thousands of pages of documentation in a single request.

Text Generation with Gemini

The simplest use case is generating text from a prompt. Let's build a code explanation tool.

import google.generativeai as genai

genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
model = genai.GenerativeModel("gemini-1.5-pro")

response = model.generate_content(
    "Explain how Python context managers work with a code example."
)

print(response.text)

Expected output:

Python context managers handle resource setup and cleanup using the `with` statement. They implement `__enter__` and `__exit__` methods...

Example:
```python
with open("file.txt", "r") as f:
    content = f.read()

The with statement guarantees that __exit__ is called even if an exception occurs, making context managers ideal for file handling, database connections, and locks.


The response object contains the generated text, safety ratings, and usage metadata. You can access `response.prompt_feedback` to check if the prompt was blocked by safety filters.

## Chat Conversations with History

For multi-turn conversations, use `chat_session` to maintain context.

```python
model = genai.GenerativeModel("gemini-1.5-pro")
chat = model.start_chat(history=[])

chat.send_message("Hi, I'm learning Google AI APIs.")
chat.send_message("What can I build with Gemini?")

response = chat.send_message("Give me 3 project ideas with code examples.")
print(response.text)

Expected output:

Here are 3 project ideas:

1. **Document Summarizer** — Use Gemini's 1M context to summarize entire research papers.
2. **Multimodal Search Engine** — Combine text and image understanding for visual search.
3. **Code Review Assistant** — Analyze pull requests and suggest improvements...

[Each project would include a brief code snippet demonstrating the approach.]

The chat history is stored in chat.history as a list of messages with role and parts. You can inspect it, save it, or restore it across sessions. DodaZIP uses this pattern to maintain conversation state in its AI-powered compression recommendation feature.

Multimodal: Processing Images and Audio

Gemini's native multimodal capability means you can send images and audio directly to the model without separate preprocessing.

import PIL.Image

image = PIL.Image.open("screenshot.png")
model = genai.GenerativeModel("gemini-1.5-pro")

response = model.generate_content(
    ["Describe the UI elements in this screenshot:", image]
)

print(response.text)

Expected output:

The screenshot shows a web browser interface with:
- A navigation bar at the top with tabs for 'File', 'Edit', 'View'
- A URL address bar with the current page loaded
- The main content area displaying a search results page with 10 blue link results
- A sidebar on the left showing bookmarks and history...

You can also send audio files directly. Gemini transcribes and understands audio content without needing a separate speech-to-text model like Whisper.

audio_file = genai.upload_file("meeting_recording.mp3")
response = model.generate_content(
    ["Summarize the key decisions from this meeting:", audio_file]
)
print(response.text)

Expected output: A meeting summary identifying speakers, decisions made, action items, and deadlines mentioned in the recording.

Embeddings with Vertex AI

For semantic search and similarity matching, use Vertex AI's embedding models. Durga Antivirus Pro uses these embeddings to compare malware signatures semantically across millions of samples.

from vertexai.language_models import TextEmbeddingModel

model = TextEmbeddingModel.from_pretrained("textembedding-gecko@003")
embeddings = model.get_embeddings(
    ["DodaZIP compresses files efficiently",
     "Durga Antivirus scans for malware"]
)

for i, emb in enumerate(embeddings):
    print(f"Embedding {i}: dimension={len(emb.values)}, values={emb.values[:5]}")

Expected output:

Embedding 0: dimension=768, values=[0.0234, -0.0156, 0.0456, -0.0078, 0.0345]
Embedding 1: dimension=768, values=[-0.0123, 0.0567, -0.0234, 0.0456, -0.0098]

The embeddings are 768-dimensional vectors. You can store them in a vector database like MongoDB or Pinecone for similarity search at scale.

Safety Filters and Content Moderation

Gemini has built-in safety filters across four categories: harassment, hate speech, sexually explicit content, and dangerous content. You can configure thresholds per category.

from google.generativeai.types import HarmCategory, HarmBlockThreshold

safety_settings = {
    HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
    HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
    HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
    HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
}

model = genai.GenerativeModel(
    "gemini-1.5-pro",
    safety_settings=safety_settings
)

Expected output: No direct output — the safety settings are applied at the API level. If a prompt violates the threshold, response.prompt_feedback will show block_reason.

Each category has four threshold levels:

Threshold Behavior
BLOCK_NONE Always show (use with caution)
BLOCK_ONLY_HIGH Block only high-confidence violations
BLOCK_MEDIUM_AND_ABOVE Block medium and high confidence
BLOCK_LOW_AND_ABOVE Most restrictive — blocks all suspected violations

For production applications in Doda Browser and Durga Antivirus Pro, we recommend BLOCK_MEDIUM_AND_ABOVE as default and BLOCK_LOW_AND_ABOVE for user-facing content.

Grounding and Citation

Gemini 1.5 Pro can ground responses against Google Search results or your own documents, providing citations for factual claims.

model = genai.GenerativeModel("gemini-1.5-pro")
response = model.generate_content(
    "What is the capital of France and what is its population?",
    generation_config={"enable_grounding": True}
)

print(response.text)
if response.candidates[0].grounding_metadata:
    for source in response.candidates[0].grounding_metadata.grounding_sources:
        print(f"Source: {source.url}")

Expected output:

The capital of France is Paris. As of 2024, the population of Paris is approximately 2.1 million within the city limits and over 12 million in the metropolitan area.
Source: https://en.wikipedia.org/wiki/Paris

Grounding transforms Gemini from a pure language model into a factual answer engine, critical for applications where accuracy matters — like Doda Browser's search answer feature.

Common Errors

1. PermissionDenied: 403 API Key Not Found

The API key is missing, invalid, or doesn't have the Generative Language API enabled. Go to Google Cloud Console, ensure the API is enabled, and verify the key is active.

2. InvalidArgument: Safety Blocker

Your prompt was blocked by safety filters. Check response.prompt_feedback for the specific category. Adjust your prompt or lower the safety threshold if appropriate.

3. ResourceExhausted: Quota Exceeded

You've hit your rate limit or daily quota. Free tier allows 60 requests per minute. Upgrade to a paid tier or implement exponential backoff.

4. DeadlineExceeded: Request Timeout

The request exceeded the default 60-second timeout. Use request_timeout parameter for long-running requests or stream responses with stream=True.

5. InvalidArgument: Context Length Exceeded

Your prompt plus the largest possible response exceeds 1M tokens. Truncate input or split into multiple requests. Use model.count_tokens() to check token counts before sending.

6. InternalServerError: Service Error

Temporary Google Cloud infrastructure issue. Implement retry logic with exponential backoff and jitter using tenacity.

7. ModelError: Unsupported Input Type

Gemini's available input types vary by model version. Flash-8B doesn't support video input. Check the model's modality support matrix before designing your application.

Practice Questions

  1. What authentication method does the Gemini API use?
  2. How many tokens can Gemini 1.5 Pro Process in its context window?
  3. What four safety categories does Gemini's content filtering cover?
  4. How does grounding improve response quality?
  5. What is the difference between Gemini 1.5 Pro and Gemini 1.5 Flash?

Answers:

  1. API keys generated via Google AI Studio or service accounts for Vertex AI.
  2. 1 million tokens — enough to Process full codebases, hour-long videos, or thousands of pages.
  3. Harassment, hate speech, sexually explicit content, and dangerous content.
  4. Grounding provides citations from Google Search or your documents, making responses verifiable and reducing hallucination.
  5. Gemini 1.5 Pro is optimized for complex reasoning with slower response; 1.5 Flash is faster and cheaper for simpler tasks.

Challenge: DodaZIP needs a smart compression advisor that reads a file (up to 100K tokens), analyzes its content type, and recommends the optimal compression algorithm with reasoning. Build a Gemini-powered tool that processes the file content and returns compression advice with token usage stats.

Mini Project: Multimodal Document Analyzer

Build a CLI tool that analyzes documents containing text, images, and tables:

import os
import google.generativeai as genai
import PIL.Image
from pathlib import Path

genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
model = genai.GenerativeModel("gemini-1.5-pro")

def analyze_document(file_path: str) -> str:
    path = Path(file_path)
    if path.suffix.lower() in [".jpg", ".png", ".webp"]:
        content = PIL.Image.open(path)
    else:
        with open(path) as f:
            content = f.read()

    response = model.generate_content(
        ["Analyze this document and provide:\n]
         "1. Document type and purpose\n"
         "2. Key information extracted\n"
         "3. Any action items or decisions\n"
         "4. Security considerations (if any)", content]
    )
    return response.text

if __name__ == "__main__":
    import sys
    result = analyze_document(sys.argv[1])
    print(result)

Try it: Run python analyze.py screenshot.png or python analyze.py report.txt. The analyzer processes both images and text files using Gemini's multimodal understanding. Extend it to handle PDFs by converting pages to images first.

FAQ

What is the difference between Gemini API and Vertex AI?

The Gemini API is a self-service developer API with a free tier, ideal for prototyping. Vertex AI is Google Cloud's enterprise MLOps platform with model tuning, deployment, monitoring, and IAM controls. Use Gemini API for quick experiments; Vertex AI for production.

How does Gemini compare to GPT-4?

Gemini offers native multimodal input (text, image, audio, video) in a single model, while GPT-4 requires separate models for each modality. Gemini's 1M token context window is 8x larger than GPT-4's 128K. GPT-4 often performs better on pure reasoning benchmarks.

Can I fine-tune Gemini models?

Yes, through Vertex AI. You can tune Gemini 1.5 Pro and Flash models using supervised fine-tuning with your own datasets. The process requires a Google Cloud project with Vertex AI enabled and your training data in JSONL format.

What is the cost of using the Gemini API?

Gemini 1.5 Flash costs $0.000075 per 1K input tokens and $0.0003 per 1K output tokens. Gemini 1.5 Pro costs $0.00125 per 1K input and $0.005 per 1K output. There is a free tier with 60 requests per minute.

How do I handle rate limits effectively?

Implement exponential backoff with tenacity, batch requests where possible, and upgrade to a paid tier for higher quotas. Vertex AI supports provisioned throughput for guaranteed capacity

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro