RedaktAIDocs

Quickstart: Google Gemini

Anonymize Google Gemini requests in under 10 lines

Prerequisites

  • rdakt-ai and google-genai installed
  • GOOGLE_API_KEY environment variable set

Async Usage

import httpx
from google import genai
from rdakt_ai import RdaktMiddleware

client = genai.Client(
    http_options={
        "transport": RdaktMiddleware(inner=httpx.AsyncHTTPTransport()),
    },
)

response = await client.aio.models.generate_content(
    model="gemini-2.0-flash",
    contents="Summarize the case for John Smith (SSN 123-45-6789)",
)
print(response.text)

Streaming

stream = await client.aio.models.generate_content_stream(
    model="gemini-2.0-flash",
    contents="Write a report about Jane Doe, email jane@acme.com",
)

async for chunk in stream:
    print(chunk.text, end="", flush=True)

Gemini-Specific Notes

Google Gemini uses a different message format (contents[].parts[].text) compared to OpenAI/Anthropic (messages[].content). Rdakt AI detects the provider from the request URL and handles format differences automatically.

What Just Happened?

  1. Detection — PII detected in the contents field
  2. Anonymization — Real values replaced with tokens before reaching Gemini
  3. Deanonymization — Gemini's SSE stream deanonymized in real-time

On this page