Skip to content

Google AI Setup

Configuration guide for Gemini API and Vertex AI integration.


Prerequisites

For Gemini API

For Vertex AI

  • Google Cloud Platform account
  • Project with Vertex AI API enabled
  • Service account with appropriate permissions (or API key)

Environment Variables

# Gemini API (Backend)
GEMINI_API_KEY="your-gemini-api-key"

# Vertex AI (Compute Server)
GOOGLE_API_KEY="your-google-api-key"
GOOGLE_CLOUD_PROJECT="your-gcp-project-id"
GOOGLE_CLOUD_LOCATION="global"  # or specific region
GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
GOOGLE_GENAI_USE_VERTEXAI="true"

# Model Configuration
VERTEX_IMAGE_MODEL="gemini-3-pro-image-preview"
VERTEX_TEXT_MODEL="gemini-3-pro-preview"

Backend Configuration (Gemini API)

# core/app/core/config.py
class Settings(BaseSettings):
    GEMINI_API_KEY: str | None = None

    # Model defaults
    CAPTIONING_AI_PROVIDER: str = "gemini"
    CAPTIONING_MODEL: str = "gemini-2.5-flash-preview-05-20"

    STYLING_AI_PROVIDER: str = "gemini"
    STYLING_MODEL: str = "gemini-2.0-flash-latest"

    LLM_VISIBILITY_AI_PROVIDER: str = "gemini"
    LLM_VISIBILITY_MODEL: str = "gemini-flash-latest"

Compute Server Configuration (Vertex AI)

# app/core/config.py
class Settings(BaseSettings):
    # Authentication
    GOOGLE_API_KEY: Optional[str] = None
    GOOGLE_CLOUD_PROJECT: Optional[str] = None
    GOOGLE_CLOUD_LOCATION: str = "global"
    GOOGLE_APPLICATION_CREDENTIALS: Optional[str] = None
    GOOGLE_GENAI_USE_VERTEXAI: bool = True

    # Models
    VERTEX_IMAGE_MODEL: str = "gemini-3-pro-image-preview"
    VERTEX_TEXT_MODEL: str = "gemini-3-pro-preview"

Dependencies

Backend (Gemini API)

# pyproject.toml
[project.dependencies]
google-generativeai = ">=0.8.5"
google-auth = ">=2.40.3"
litellm = "^1.75.8"

Compute Server (Vertex AI)

# pyproject.toml
[project.dependencies]
google-genai = "^1.0.0"
google-auth = ">=2.40.3"

Client Initialization

Gemini API (via GeminiService)

import google.generativeai as genai
from google.genai import Client, types

class GeminiService:
    def __init__(self, api_key: str, model: str):
        self.api_key = api_key
        self.model = model
        self.client = Client(api_key=api_key)

    def generate_content(self, contents, system_instruction=None):
        config = types.GenerateContentConfig(
            response_mime_type="application/json",
            system_instruction=[types.Part.from_text(system_instruction)] if system_instruction else None
        )
        return self.client.models.generate_content(
            model=self.model,
            contents=contents,
            config=config,
        )

Vertex AI (via VertexProcessor)

from google import genai
from google.genai import types

class VertexProcessor:
    def _create_client(self) -> genai.Client:
        # Priority 1: Service Account (IAM)
        if settings.GOOGLE_APPLICATION_CREDENTIALS:
            return genai.Client(vertexai=True)

        # Priority 2: Vertex AI with API Key + Project
        if settings.GOOGLE_GENAI_USE_VERTEXAI:
            return genai.Client(
                api_key=settings.GOOGLE_API_KEY,
                vertexai=True,
                project=settings.GOOGLE_CLOUD_PROJECT,
                location=settings.GOOGLE_CLOUD_LOCATION,
            )

        # Fallback: Standard Gemini API
        return genai.Client(api_key=settings.GOOGLE_API_KEY)

Authentication Priority

Vertex AI Authentication Order

  1. Service Account (IAM) - GOOGLE_APPLICATION_CREDENTIALS set
  2. Most secure for production
  3. Uses GCP IAM roles

  4. API Key + Project - GOOGLE_GENAI_USE_VERTEXAI=true

  5. Requires GOOGLE_API_KEY and GOOGLE_CLOUD_PROJECT
  6. Good for development

  7. Standard Gemini API - Fallback

  8. Uses GOOGLE_API_KEY only
  9. No image generation support

Model Aliases (Gemini API)

model_aliases = {
    "gemini": "gemini-2.0-flash",
    "gemini-2.5-flash": "gemini-2.5-flash-preview-05-20",
    "gemini-2.0-flash": "gemini-2.0-flash-latest",
    "gemini-1.5-pro": "gemini-1.5-pro-latest",
    "gemini-1.5-flash": "gemini-1.5-flash-latest",
    "gemini-flash-latest": "gemini-flash-latest",
}

Default Parameters

Gemini API Defaults

Parameter Value Description
max_tokens 8192 Maximum output
temperature 0.4 Balanced creativity
top_p 0.95 Nucleus sampling
top_k 40 Gemini-specific

Vertex AI Defaults

Parameter Value Description
temperature 0.7 Generation creativity
top_p 0.95 Nucleus sampling
top_k 40 Token selection

Verification

Test Gemini API

from google.genai import Client

client = Client(api_key=settings.GEMINI_API_KEY)
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="Hello, Gemini!"
)
print(response.text)

Test Vertex AI

from google import genai

client = genai.Client(
    api_key=settings.GOOGLE_API_KEY,
    vertexai=True,
    project=settings.GOOGLE_CLOUD_PROJECT,
    location=settings.GOOGLE_CLOUD_LOCATION,
)

response = client.models.generate_content(
    model="gemini-3-pro-preview",
    contents="Hello from Vertex AI!"
)
print(response.text)

Troubleshooting

Invalid API Key

Error: API key not valid
  • Verify key is correct and active
  • Check key has access to required models

Quota Exceeded

Error: Quota exceeded for quota metric
  • Check usage in Google Cloud Console
  • Upgrade quota or implement rate limiting

Safety Filter Blocked

Error: Response blocked by safety filters
  • For Vertex AI: Safety settings are disabled by default
  • For Gemini API: Rephrase prompt or adjust thresholds

Model Not Found

Error: Model not found
  • Verify model name is correct
  • Check model availability in your region
  • Ensure Vertex AI API is enabled in project