Google Gemini
Using Rdakt AI with Google Gemini's API
Google's google-genai SDK uses httpx for HTTP requests. Rdakt AI integrates via the http_options parameter.
Setup
import httpx
from google import genai
from rdakt_ai import RdaktMiddleware
client = genai.Client(
http_options={
"transport": RdaktMiddleware(inner=httpx.AsyncHTTPTransport()),
},
)Gemini's SDK passes the transport via http_options dictionary, not through an httpx.AsyncClient instance like OpenAI and Anthropic.
Supported Models
All Gemini generative models: Gemini 2.0 Flash, Gemini 2.5 Flash, Gemini 2.5 Pro, and future models.
Message Format
Gemini uses a different format from OpenAI and Anthropic:
{
"contents": [
{
"parts": [{"text": "Tell me about John Smith"}],
"role": "user"
}
]
}Rdakt AI detects Gemini requests by the URL (generativelanguage.googleapis.com) and scans contents[].parts[].text for PII.
Streaming Format
Gemini SSE events contain:
{
"candidates": [
{
"content": {
"parts": [{"text": "Hello"}],
"role": "model"
}
}
]
}Rdakt AI extracts content from candidates[0].content.parts[0].text.
Streaming Example
stream = await client.aio.models.generate_content_stream(
model="gemini-2.0-flash",
contents="Write about Jane Doe (jane@acme.com)",
)
async for chunk in stream:
print(chunk.text, end="", flush=True)Non-Streaming Example
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)Key Differences from OpenAI/Anthropic
| Feature | OpenAI/Anthropic | Gemini |
|---|---|---|
| Transport injection | httpx.AsyncClient(transport=...) | http_options={"transport": ...} |
| Message field | messages[].content | contents[].parts[].text |
| Response content | choices[0].delta.content | candidates[0].content.parts[0].text |
| Gzip compression | Anthropic only | No |