A Fastapi Based Proxy for Gemini API
Project description
Gemini Calo
Gemini Calo is a powerful, yet simple, FastAPI-based proxy server for Google's Gemini API. It provides a seamless way to add a layer of authentication, logging, and monitoring to your Gemini API requests. It's designed to be run as a standalone server or integrated into your existing FastAPI applications.
One of its key features is providing an OpenAI-compatible endpoint, allowing you to use Gemini models with tools and libraries that are built for the OpenAI API.
Key Features
- Authentication: Secure your Gemini API access with an additional layer of API key authentication.
- Request Logging: Detailed logging of all incoming requests and outgoing responses.
- OpenAI Compatibility: Use Gemini models through an OpenAI-compatible
/v1/chat/completionsendpoint. - Round-Robin API Keys: Distribute your requests across multiple API keys, both globally and per model route.
- Multi-Provider Routing: Route specific models (via glob patterns) to different upstream providers — use OpenAI, Anthropic, or any OpenAI-compatible endpoint alongside Gemini.
- Easy Integration: Use it as a standalone server or mount it into your existing FastAPI project.
- Extensible: Easily add your own custom middleware to suit your needs.
How It's Useful
- Centralized API Key Management: Instead of hardcoding your Gemini API keys in various clients, you can manage them in one place.
- Security: Protect your expensive Gemini API keys by exposing only a proxy key to your users or client applications.
- Monitoring & Observability: The logging middleware gives you insight into how your API is being used, helping you debug issues and monitor usage patterns.
- Seamless Migration: If you have existing tools that use the OpenAI API, you can switch to using Google's Gemini models without significant code changes.
Running the Built-in Server
You can quickly get the proxy server up and running with just a few steps.
1. Installation
Install the package using pip:
pip install gemini-calo
2. Configuration
The server is configured through environment variables. You can create a .env file in your working directory to store them.
GEMINI_CALO_API_KEYS: A comma-separated list of your Google Gemini API keys. The proxy will rotate through these keys for outgoing requests. Required when using the built-in server.GEMINI_CALO_PROXY_API_KEYS: (Optional) A comma-separated list of API keys that clients must provide to use the proxy. If not set, the proxy accepts all requests without authentication.GEMINI_CALO_HTTP_PORT: The port on which the server will run. Defaults to8000.GEMINI_CALO_LOG_LEVEL: Sets the logging level. Options:DEBUG,INFO,WARNING,ERROR,CRITICAL. Defaults toCRITICAL.GEMINI_CALO_LOG_FILE: Specifies the file where logs will be written. Defaults toapp.log.GEMINI_CALO_CONVERSATION_SUMMARIZATION_LRU_CACHE: Size of the LRU cache for conversation summarization. Defaults to20.GEMINI_CALO_MODEL_OVERRIDE: Forces all requests to use a specific model name, overriding whatever the client sends.
Example .env file:
# Your gemini API Keys
export GEMINI_CALO_API_KEYS=AIaYourGeminiKey1,AIaYourGeminiKey2
# API Keys for your internal user
export GEMINI_CALO_PROXY_API_KEYS=my_secret_proxy_key_1,my_secret_proxy_key_2
# Gemini Calo HTTP Port
export GEMINI_CALO_HTTP_PORT=8080
# Logging level
export GEMINI_CALO_LOG_LEVEL=DEBUG
# Log file
export GEMINI_CALO_LOG_FILE=gemini_calo.log
3. Running the Server
Once configured, you can start the server with the gemini-calo command:
gemini-calo
The server will start on the configured port (e.g., http://0.0.0.0:8080).
Integrating with an Existing FastAPI Application
If you have an existing FastAPI application, you can easily integrate Gemini Calo's proxy functionality into it.
from fastapi import FastAPI
from gemini_calo.proxy import GeminiProxyService
from gemini_calo.middlewares.auth import auth_middleware
from gemini_calo.middlewares.logging import logging_middleware
from functools import partial
import os
# Your existing FastAPI app
app = FastAPI()
# 1. Initialize the GeminiProxyService
api_keys = os.getenv("GEMINI_CALO_API_KEYS", "").split(",")
proxy_service = GeminiProxyService(api_keys=api_keys)
# 2. (Optional) Add Authentication Middleware
proxy_api_keys = os.getenv("GEMINI_CALO_PROXY_API_KEYS", "").split(",")
if proxy_api_keys:
auth_middleware_with_keys = partial(auth_middleware, user_api_key_checker=proxy_api_keys)
app.middleware("http")(auth_middleware_with_keys)
# 3. (Optional) Add Logging Middleware
app.middleware("http")(logging_middleware)
# 4. Mount the Gemini and OpenAI routers
app.include_router(proxy_service.gemini_router)
app.include_router(proxy_service.openai_router)
@app.get("/health")
def health_check():
return {"status": "ok"}
# Now you can run your app as usual with uvicorn
# uvicorn your_app_file:app --reload
Routing Models to Different Providers
GeminiProxyService supports a model_routes parameter — a dict that maps glob patterns to a RouteConfig. When a request arrives, the proxy extracts the model name (from the URL path for Gemini-format requests, or from the JSON body for OpenAI-format requests) and checks it against each pattern in order. The first match wins; unmatched models fall back to base_url + api_keys.
RouteConfig fields
| Field | Type | Default | Description |
|---|---|---|---|
url |
str |
— | Upstream base URL for this route |
api_keys |
list[str] |
— | Keys rotated round-robin for this route |
auth_type |
"bearer" | "x-goog-api-key" |
"bearer" |
Header used to send the API key |
timeout |
float |
300.0 |
Per-request timeout in seconds |
Example: mixing Gemini and OpenAI
import os
from fastapi import FastAPI
from gemini_calo.proxy import GeminiProxyService, RouteConfig
app = FastAPI()
proxy = GeminiProxyService(
base_url="https://generativelanguage.googleapis.com",
api_keys=["gemini-key-1", "gemini-key-2"], # default: round-robined for unmatched models
model_routes={
# Glob pattern → RouteConfig
"gpt-4*": RouteConfig(
url="https://api.openai.com",
api_keys=["openai-key-1", "openai-key-2"],
auth_type="bearer",
),
"claude-*": RouteConfig(
url="https://api.anthropic.com",
api_keys=["anthropic-key-1"],
auth_type="bearer",
timeout=600.0,
),
# Gemini requests not matched above use base_url + api_keys
},
)
app.include_router(proxy.gemini_router)
app.include_router(proxy.openai_router)
Pattern matching uses Python's fnmatch, so * matches any substring within a segment and ? matches a single character. Patterns are checked in insertion order — the first match wins.
How the Middleware Works
Middleware in FastAPI are functions that process every request before it reaches the specific path operation and every response before it is sent back to the client. Gemini Calo includes two useful middlewares by default.
Logging Middleware (logging_middleware)
This middleware logs the details of every incoming request and outgoing response, including headers and body content. This is invaluable for debugging and monitoring. It's designed to handle both standard and streaming responses correctly.
Authentication Middleware (auth_middleware)
This middleware protects your proxy endpoints. It checks for an API key in the Authorization header (as a Bearer token) or the x-goog-api-key header. It then validates this key against the list of keys you provided in the GEMINI_CALO_PROXY_API_KEYS environment variable. If the key is missing or invalid, it returns a 401 Unauthorized error.
Adding Your Own Middleware
Because Gemini Calo is built on FastAPI, you can easily add your own custom middleware. For example, you could add a middleware for rate limiting, CORS, or custom header injection.
Advanced Middleware: Modifying Request Body and Headers
Here is a more advanced example that intercepts a request, modifies its JSON body, adds a new header, and then forwards it to the actual endpoint. This can be useful for injecting default values, adding metadata, or transforming request payloads.
Important: Reading the request body consumes it. To allow the endpoint to read the body again, we must reconstruct the request with the modified body.
from fastapi import FastAPI, Request
from starlette.datastructures import MutableHeaders
import json
app = FastAPI()
# This middleware will add a 'user_id' to the request body
# and a 'X-Request-ID' to the headers.
async def modify_request_middleware(request: Request, call_next):
# Get the original request body
body = await request.body()
# Modify headers
request_headers = MutableHeaders(request.headers)
request_headers["X-Request-ID"] = "some-unique-id"
# Modify body (if it's JSON)
new_body = body
if body and request.headers.get("content-type") == "application/json":
try:
json_body = json.loads(body)
# Add or modify a key
json_body["user_id"] = "injected-user-123"
new_body = json.dumps(json_body).encode()
except json.JSONDecodeError:
# Body is not valid JSON, pass it through
pass
# To pass the modified body and headers, we need to create a new Request object.
# We do this by defining a new 'receive' channel.
async def receive():
return {"type": "http.request", "body": new_body, "more_body": False}
# We replace the original request's scope with the modified headers
request.scope["headers"] = request_headers.raw
# Create the new request object and pass it to the next middleware/endpoint
new_request = Request(request.scope, receive)
response = await call_next(new_request)
return response
app.middleware("http")(modify_request_middleware)
# ... then add the Gemini Calo proxy and routers as shown above
Integration with Zrb
Suppose you run Gemini Calo with the following configuration, then you will have Gemini Calo run on http://localhost:8080.
# Your gemini API Keys
export GEMINI_CALO_API_KEYS=AIaYourGeminiKey1,AIaYourGeminiKey2
# API Keys for your internal user
export GEMINI_CALO_PROXY_API_KEYS=my_secret_proxy_key_1,my_secret_proxy_key_2
# Gemini Calo HTTP Port
export GEMINI_CALO_HTTP_PORT=8080
# Start Gemini Calo
gemini-calo
Integration Using OpenAI Compatibility Layer
To use OpenAI compatibility layer with Zrb, you need to set some environment variables.
# OpenAI compatibility URL
export ZRB_LLM_BASE_URL=http://localhost:8080/v1beta/openai/
# One of your valid API Key for internal user
export ZRB_LLM_API_KEY=my_secret_proxy_key_1
# The model you want to use
export ZRB_LLM_MODEL=gemini-2.5-flash
# Run `zrb llm ask` or `zrb llm chat`
zrb llm ask "What is the current weather at my current location?"
Integration Using Gemini Endpoint
To use Gemini Endpoint, you will need to edit or create zrb_init.py
from google import genai
from google.genai.types import HttpOptions
from pydantic_ai.models.gemini import GeminiModelSettings
from pydantic_ai.providers.google import GoogleProvider
from pydantic_ai.models.google import GoogleModel
from zrb import llm_config
client = genai.Client(
api_key="my_secret_proxy_key_1", # One of your valid API Key for internal user
http_options=HttpOptions(
base_url="http://localhost:8080",
),
)
provider = GoogleProvider(client=client)
model = GoogleModel(
model_name="gemini-2.5-flash",
provider=provider,
settings=GeminiModelSettings(
temperature=0.0,
gemini_safety_settings=[
# Let's become evil 😈😈😈
# https://ai.google.dev/gemini-api/docs/safety-settings
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_CIVIC_INTEGRITY",
"threshold": "BLOCK_NONE",
},
]
)
)
llm_config.set_default_model(model)
Once you set up everything, you can start interacting with Zrb.
# Run `zrb llm ask` or `zrb llm chat`
zrb llm ask "What is the current weather at my current location?"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gemini_calo-0.2.2.tar.gz.
File metadata
- Download URL: gemini_calo-0.2.2.tar.gz
- Upload date:
- Size: 17.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.1 CPython/3.13.0 Darwin/25.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59b3e3f559c4c3b320025103dd2ba028b00a4521ef868457149586aebb0b455b
|
|
| MD5 |
5398fe6a3c7b3006c459c2661a7d49e7
|
|
| BLAKE2b-256 |
90d4ca44761c66d98de0abed25ff7dc07e7cd349d3739dfab8e1e07583dcdf57
|
File details
Details for the file gemini_calo-0.2.2-py3-none-any.whl.
File metadata
- Download URL: gemini_calo-0.2.2-py3-none-any.whl
- Upload date:
- Size: 18.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.1 CPython/3.13.0 Darwin/25.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
462d4eea4d76c3b84a3295e91e60422db33c62375b7989d1c15929985b29c904
|
|
| MD5 |
6972b2275a7e1ff36375496a09a7732f
|
|
| BLAKE2b-256 |
5044dba35f1dbf495aa8f5330f9a911477e476b08600396a184a59790f634c9f
|