Deploying Responsible AI with Vertex AI and Gemini Models

From Zero to Vertex AI Invoke Gemini using Responsible AI Principles

This article serves as a tutorial on deploying a FastAPI application to Google Cloud Run that invokes Gemini models via Vertex AI while adhering to responsible AI principles.

Introduction

The guide illustrates how to configure safety filters for four harm categories: dangerous content, harassment, hate speech, and sexually explicit content, using strict blocking thresholds. It utilizes Vellox as an adapter for running ASGI applications in Google Cloud Functions and implements Bearer token authentication for enhanced security.

Moreover, the tutorial details the entire setup process, including enabling necessary Google Cloud services, configuring IAM roles, and deploying the function with environment variables.

This tutorial emphasizes practical safety implementations by demonstrating how Vertex AI screens both inputs and outputs. It returns a “SAFETY” finish reason accompanied by detailed safety ratings when harmful content is detected, which is particularly beneficial for developers aiming to build AI applications with integrated content moderation and security.

Technology Used

Cloud Run Functions:

  • Designed for quick responses to events or HTTP triggers.
  • Minimal configuration required — all infrastructure is managed for you.
  • Suitable for concise functions rather than full-fledged services.

Velox:

Vellox is an adapter that allows the execution of ASGI applications (Asynchronous Server Gateway Interface) in Google Cloud Functions.

HTTPBearer:

HTTPBearer in FastAPI is a security utility provided by the fastapi.security module. It manages Bearer token authentication, a common method for securing API endpoints by handling the presence and extraction of the Bearer token.

Steps to Implement

Development Environment Setup:

Use devcontainer to install all necessary components. Set up Docker and DevContainer, and after pulling the code, you will be ready to go.

Enable Services:

Initially, execute the following commands:

gcloud init

Followed by:

gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com aiplatform.googleapis.com

IAM Permissions:

Assign the project role ‘roles/aiplatform.user’ to the current project in IAM.

Deploy with Environment Variables:

Use the following command to deploy:

gcloud run deploy fastapi-func --source . --function handler --base-image python313 --region asia-south1 --set-env-vars API_TOKEN="damn-long-token",GOOGLE_GENAI_USE_VERTEXAI=True,GOOGLE_CLOUD_LOCATION=global --allow-unauthenticated

This command:

  • Deploys a FastAPI function named handler from your local folder.
  • Runs on Python 3.13, in the Mumbai (asia-south1) region.
  • Sets environment variables for API tokens and Google Vertex AI usage.
  • Makes the function publicly available (no authentication required except for the Bearer token).

Walkthrough of main.py

The core of the implementation involves a simple FastAPI application integrated with Google Gemini AI, while also incorporating safety content filters.

import httpx, os, uuid
from fastapi import Depends, FastAPI, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from vellox import Vellox
from pydantic import BaseModel
from typing import Optional
from google import genai
from google.genai.types import (
GenerateContentConfig,
HarmCategory,
HarmBlockThreshold,
HttpOptions,
SafetySetting,
)

The safety_settings variable is defined as a list of SafetySetting objects, each specifying a harm category along with a block threshold. This includes:

  • Dangerous content
  • Harassment
  • Hate speech
  • Sexually explicit content

All categories are configured to block at the BLOCK_LOW_AND_ABOVE threshold, ensuring strict moderation.

Essentially, these settings allow the application to screen both inputs and outputs. If the model assesses the content as harmful, the call is blocked and no text is returned. By default, Gemini employs a severity-aware harm-block method in Vertex AI, which can be adjusted as necessary.

Conclusion

The implementation of responsible AI principles in deploying FastAPI applications using Google Cloud Run and Vertex AI is crucial in developing secure, ethical AI solutions. By leveraging these technologies, developers can effectively manage content moderation while ensuring the safety and integrity of their applications.

For further details, visit the GitHub repository.

More Insights

Revolutionizing Drone Regulations: The EU AI Act Explained

The EU AI Act represents a significant regulatory framework that aims to address the challenges posed by artificial intelligence technologies in various sectors, including the burgeoning field of...

Revolutionizing Drone Regulations: The EU AI Act Explained

The EU AI Act represents a significant regulatory framework that aims to address the challenges posed by artificial intelligence technologies in various sectors, including the burgeoning field of...

Embracing Responsible AI to Mitigate Legal Risks

Businesses must prioritize responsible AI as a frontline defense against legal, financial, and reputational risks, particularly in understanding data lineage. Ignoring these responsibilities could...

AI Governance: Addressing the Shadow IT Challenge

AI tools are rapidly transforming workplace operations, but much of their adoption is happening without proper oversight, leading to the rise of shadow AI as a security concern. Organizations need to...

EU Delays AI Act Implementation to 2027 Amid Industry Pressure

The EU plans to delay the enforcement of high-risk duties in the AI Act until late 2027, allowing companies more time to comply with the regulations. However, this move has drawn criticism from rights...

White House Challenges GAIN AI Act Amid Nvidia Export Controversy

The White House is pushing back against the bipartisan GAIN AI Act, which aims to prioritize U.S. companies in acquiring advanced AI chips. This resistance reflects a strategic decision to maintain...

Experts Warn of EU AI Act’s Impact on Medtech Innovation

Experts at the 2025 European Digital Technology and Software conference expressed concerns that the EU AI Act could hinder the launch of new medtech products in the European market. They emphasized...

Ethical AI: Transforming Compliance into Innovation

Enterprises are racing to innovate with artificial intelligence, often without the proper compliance measures in place. By embedding privacy and ethics into the development lifecycle, organizations...

AI Hiring Compliance Risks Uncovered

Artificial intelligence is reshaping recruitment, with the percentage of HR leaders using generative AI increasing from 19% to 61% between 2023 and 2025. However, this efficiency comes with legal...