Gemini API Key Rotator for LlamaIndex
This utility monkey-patches google.auth.api_key.Credentials to rotate multiple Google API keys, useful with LlamaIndex to manage Gemini API request limits.
How to Use
- Install the package: run this in command prompt
pip install gemini-rotator. - Import and Patch: Before initializing your Gemini LLM, call the
patchfunction with your API keys. The script modifies the Google authentication library at runtime. Whenpatchis called:- It extends the
Credentialsclass to cycle through your provided API keys.
- It extends the
patch(api_keys, api_main=None)
api_keys: A list of your Google API key strings.api_main: (Optional, but Recommended) An API key fromapi_keys. Rotation only occurs if the default key passed to LlamaIndex or generativeai.configure() matchesapi_main. This prevents interference with other Google services using different keys. IfNone, keys rotate on every use (use with caution as it might affect other Google clients).
restore()
Call restore() to revert the monkey-patching and return the google.auth.api_key.Credentials class to its original state.
Example with LlamaIndex
# Assuming the rotator script is saved as gemini_key_rotator.py
from gemini_key_rotator import patch
from llama_index.llms.gemini import Gemini
from llama_index.core.base.llms.types import ChatMessage
# --- Configuration ---
MY_GEMINI_API_KEYS = [
"YOUR_API_KEY_1", # Replace
"YOUR_API_KEY_2", # Replace
"YOUR_API_KEY_3", # Replace
]
MAIN_API_KEY = MY_GEMINI_API_KEYS[0]
# Monkey patch
patch(MY_GEMINI_API_KEYS, MAIN_API_KEY)
llm = Gemini(model="gemini-2.0-flash", api_key=MAIN_API_KEY) # Initialize LlamaIndex with the main key
messages = [ChatMessage(content="Hello Gemini", role="user")] # Example messages
response = llm.chat(messages=messages) # Send message to Gemini
print(response.message.content) # Print the response
The patch will take care of rotating the API keys for you. The Gemini class will use the main key for initialization, and the rotation will happen automatically when making requests.
Important Notes
api_main: Usingapi_mainis strongly advised to ensure rotation only affects the intended Gemini client.- Get API Keys: Obtain your API keys from the Google Cloud Console under "Credentials". You should have Gemini API enabled in your project and its limited to one api key per project. You can create multiple projects to get more keys.
- Risks: Abusing the api may lead to account suspension. Use responsibly and ensure compliance with Google's API usage policies.
- Use Cases: This is not for production use. It's a temporary solution for testing that requires slightly more thant gemini rate limits to effectively test and benchmark.
Disclaimer
Use at your own risk. Comply with Google's API terms.
Metadata
Release files for gemini-rotator 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| gemini_rotator-0.1.0.tar.gz | 3.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| gemini_rotator-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.5 kB
Release files / gemini_rotator-0.1.0.tar.gz
| Download URL | gemini_rotator-0.1.0.tar.gz |
|---|---|
| Size | 3.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d4738f36a531f6679da8e2b8c21843b7c80c07667dfdf6eeb194451ae43a2760
|
|
BLAKE2b-256 checksum How to use checksums |
e8cef3f59b36171ecb10383d168d5335806af36095acb855a1eee99ddb7f5f17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.11
|
Release files / gemini_rotator-0.1.0-py3-none-any.whl
| Download URL | gemini_rotator-0.1.0-py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
79007f7722dd21cc5bc10df91c4cbc12ec520e3b3c23005b2176b3ed75525c39
|
|
BLAKE2b-256 checksum How to use checksums |
eb5c119e9055c29c444654666a57a710a2a12c6bf083b6dc30abbc35ec69608a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.11
|