◉GEMINI LABJP
●2.5 ROUTE — From today, 12/1, calls to gemini-2.5-pro and 2.5-flash are rerouted to newer models automatically●CLI 0.71.1 — Gemini CLI stable is still v0.71.1, with a 0.73 nightly published on 11/30●INTRO END — Introductory pricing for 3.8, 3.7 and 3.6 Flash ends on 12/31. 30 days left●VERTEX — Developers are asking how to run Gemini CLI with Vertex AI credentials●NEON — Gemini 4 Neon became generally available on 11/18 (paid tier only)●IMAGE TEXT — A new test shows text inside an image can sway Gemini's answer●2.5 ROUTE — From today, 12/1, calls to gemini-2.5-pro and 2.5-flash are rerouted to newer models automatically●CLI 0.71.1 — Gemini CLI stable is still v0.71.1, with a 0.73 nightly published on 11/30●INTRO END — Introductory pricing for 3.8, 3.7 and 3.6 Flash ends on 12/31. 30 days left●VERTEX — Developers are asking how to run Gemini CLI with Vertex AI credentials●NEON — Gemini 4 Neon became generally available on 11/18 (paid tier only)●IMAGE TEXT — A new test shows text inside an image can sway Gemini's answer
Articles/API / SDK
◈ API / SDK/2026-04-11Advanced

Building a Production Content Moderation System with Gemini API: A

A complete guide to building a production-grade content moderation system with the Gemini API. Covers custom safety criteria, multimodal inspection of text and images, async batch processing, Human-in-the-Loop workflows, and cost optimization.

gemini-api291content-moderationproduction140multimodal45safetypython113

✦ Premium Article

A phrase like "knife techniques" means one thing inside a cooking recipe and something else entirely inside a threatening post. For any service handling user-generated content, that kind of judgment is exactly what keyword matching and image classifiers keep missing.

Gemini 2.5 Pro and Flash close that gap with multimodal input and genuine contextual understanding — which is why building a custom moderation layer on Gemini has become a credible alternative to dedicated services like Amazon Rekognition or Azure Content Moderator.

The built-in safety filters (safety_settings), though, stop short of requirements like these:

  • Industry-specific prohibited content: Compliance requirements unique to healthcare, finance, or education platforms
  • Custom severity scoring: Weighted word lists or tiered response flows
  • Multimodal compound decisions: Evaluating text and images together as a unit
  • Audit logs and explainability: Recording why a particular decision was made

System Architecture

Core Components

A robust production moderation system is organized into four layers.

Layer 1: Ingest Layer Receives incoming user content and writes it to an async buffer (Redis or Cloud Tasks).

Layer 2: Moderation Processing Layer Worker processes that call the Gemini API to analyze content. Designed for horizontal scaling.

Layer 3: Decision and Action Layer A rules engine that routes analysis results to automatic approval, automatic rejection, or human review.

Layer 4: Human-in-the-Loop Layer Sends borderline cases to human moderators and feeds their decisions back as a training signal.

Choosing Your Gemini Model

To balance cost and accuracy, adopt a two-stage moderation strategy. Run Gemini 2.5 Flash for the initial screen -- it is optimized for high throughput and low cost -- and escalate to Gemini 2.5 Pro only when the confidence score falls near the boundary. Pro delivers higher accuracy for complex contextual decisions but at greater cost per token.


Moderating Text Content

Defining Custom Safety Criteria

One of Gemini API's biggest advantages for moderation is the ability to define business-specific judgment criteria in plain language via System Instructions.

import google.generativeai as genai
import json
from enum import Enum
from dataclasses import dataclass
from typing import Optional
 
genai.configure(api_key="YOUR_GEMINI_API_KEY")
 
class ModerationDecision(Enum):
    APPROVED = "approved"
    REJECTED = "rejected"
    REVIEW = "review"
 
@dataclass
class ModerationResult:
    decision: ModerationDecision
    confidence: float          # 0.0 to 1.0
    categories: list[str]      # Detected categories
    reason: str                # Decision rationale (for audit logs)
    escalate_to_human: bool    # Whether human review is needed
 
MODERATION_SYSTEM_PROMPT = """
You are a content moderator for a community platform.
Review each piece of content against the criteria below and respond ONLY in the specified JSON format.
 
## Criteria
 
### REJECTED (immediate removal)
- Threats or harassment targeting real individuals
- Exposure of personal information (home address, phone numbers, credit card numbers)
- Promotion of illegal goods or services
- Content inappropriate for minors
 
### REVIEW (send to human review)
- Inflammatory content related to politics or religion
- Medical or legal advice (requires expert verification)
- Borderline cases where confidence is below 0.7
 
### APPROVED
- General user posts that do not fall into the above categories
 
## Response format (return this JSON and nothing else)
{
  "decision": "approved|rejected|review",
  "confidence": numeric value between 0.0 and 1.0,
  "categories": ["list", "of", "detected", "categories"],
  "reason": "Decision rationale in 1 to 2 sentences"
}
"""
 
def moderate_text(content: str, model_name: str = "gemini-2.5-flash") -> ModerationResult:
    """Moderate a text content item."""
    model = genai.GenerativeModel(
        model_name=model_name,
        system_instruction=MODERATION_SYSTEM_PROMPT
    )
    
    try:
        response = model.generate_content(
            f"Please review the following content:\n\n{content}",
            generation_config=genai.GenerationConfig(
                response_mime_type="application/json",
                temperature=0.1,
                max_output_tokens=512
            )
        )
        
        result_data = json.loads(response.text)
        decision = ModerationDecision(result_data["decision"])
        confidence = float(result_data.get("confidence", 0.5))
        
        return ModerationResult(
            decision=decision,
            confidence=confidence,
            categories=result_data.get("categories", []),
            reason=result_data.get("reason", ""),
            escalate_to_human=(
                decision == ModerationDecision.REVIEW or
                confidence < 0.7
            )
        )
    except (json.JSONDecodeError, KeyError, ValueError) as e:
        return ModerationResult(
            decision=ModerationDecision.REVIEW,
            confidence=0.0,
            categories=["parse_error"],
            reason=f"API response parse error: {str(e)}",
            escalate_to_human=True
        )

The critical detail here is response_mime_type="application/json". Gemini API's JSON mode guarantees structured responses every time, dramatically reducing parse errors in production. Setting temperature=0.1 ensures consistent, repeatable decisions across similar inputs.

Expected output for a benign post:

{
  "decision": "approved",
  "confidence": 0.95,
  "categories": [],
  "reason": "General user inquiry with no harmful content detected."
}

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦Developers frustrated by built-in safety filters can now implement a custom harmful-content detection system tailored to their business rules -- and have it running today
✦You will get a complete, production-ready architecture and working code for processing multimodal content (text and images) through an async batch pipeline
✦You will be able to apply a practical tuning strategy that optimizes detection accuracy, processing throughput, and cost simultaneously -- plus wire in a Human-in-the-Loop review flow
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Gemini Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

◈ API / SDK2026-07-15
Sample what you already accepted — an audit budget that catches silent quality drift
A confidence gate only ever looks at output the model hesitated on. Silent drift sinks into the batch that sailed through. Working from a fixed 30-minutes-a-day review budget, this walks through deriving detection time from the binomial, reallocating the same budget across risk strata, and catching slow decay with a cumulative monitor.
◈ API / SDK2026-07-04
When Gemini API Leaks Japanese Into Your English Output Once in a While — Field Notes on Measuring the Contamination Rate and Tightening It in Stages
You told Gemini to answer in English, and 3 out of 100 runs slip a Japanese sentence into the tail. Here is why you cannot stop that 'once in a while', and a production pattern that measures the contamination rate as an SLO and tightens it with graded recovery, with working code.
◈ API / SDK2026-05-18
Building Automatic Wallpaper Category Classification with Gemini Vision
An indie developer shares how they implemented automatic wallpaper image classification with the Gemini Vision API — including accuracy results, real pitfalls, structured-output tips, and a cost comparison with GPT-4o Vision.