Lightweight, high-performance Python wrapper for the Google Gemini API
Project description
gsdk 🚀
gsdk (Gemini SDK) is a lightweight, high-performance Python wrapper for the Google Gemini API (built on the modern google-genai). It is designed for production use, offering automatic key rotation, session persistence, and full flexibility for model configuration.
✨ Key Features
- 🔑 Smart Key Rotation: Automatically switch between multiple API keys when you hit rate limits (429/403).
- 🔄 Configurable Retries: Set custom retry counts and delays to ensure stability.
- 💾 Session Persistence: Built-in support for File and Redis storage to maintain conversation history.
- ⚙️ Full Flexibility: Pass any generation parameter (
temperature,top_p,max_tokens) globally or per request. - 🔍 Google Search Grounding: Easily enable real-time web search for the model.
- 🎙️ Live Multimodal: Support for the Gemini Real-time (WebSockets) API.
- 📁 Media Support: Simplified async file uploads for images, video, and audio.
📦 Installation
pip install gsdk
Make sure you have the requirements installed:
pip install google-genai redis
🚀 Quick Start
import asyncio
import logging
from gsdk import GeminiSDK
# Enable logging to see key rotation and API status
logging.basicConfig(level=logging.INFO)
async def main():
# Initialize the SDK
sdk = GeminiSDK(
api_keys=["YOUR_API_KEY_1", "YOUR_API_KEY_2"],
model_name="gemini-flash-latest",
temperature=0.7 # Global setting
)
# Simple text request
response = await sdk.ask("session_id_1", "Hello! Who are you?")
print(f"AI: {response.text}")
# Request with parameter override
response = await sdk.ask(
"session_id_1",
"Tell me a very short story.",
temperature=0.1, # Overrides global 0.7
max_output_tokens=100
)
print(f"AI: {response.text}")
asyncio.run(main())
🛠 Advanced Usage
1. Key Rotation & Retry Logic
Perfect for staying within free-tier limits. If one key hits a 429 error, gsdk waits and switches to the next one.
sdk = GeminiSDK(
api_keys=["KEY_1", "KEY_2", "KEY_3"],
max_retries=10,
retry_delay=5.0 # Seconds to wait before retry
)
2. Redis Storage (for Production)
Use Redis to share chat history across multiple server instances.
from gsdk.storage import RedisStorage
redis_store = RedisStorage(host='localhost', port=6379, db=0)
sdk = GeminiSDK(api_keys=["..."], storage=redis_store)
3. Media Uploads (Multimodal)
# Upload a file (Image/Video/PDF)
uploaded_file = await sdk.media.upload_file("path/to/image.jpg")
# Ask a question about the file
response = await sdk.ask("session_2", [uploaded_file, "What is in this image?"])
print(response.text)
4. Google Search Grounding
Enable the model to search the web for up-to-date information.
sdk = GeminiSDK(api_keys=["..."], use_search=True)
response = await sdk.ask("news_chat", "What is the price of Bitcoin today?")
print(f"Source info: {response.sources}")
5. Real-time Live API
from gsdk import GeminiLive
live = GeminiLive(api_key="YOUR_KEY")
async def run_live():
async with live.start_session() as session:
await session.send("Hello!", end_of_turn=True)
async for message in session.receive():
print(message)
📖 API Reference
GeminiSDK Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
api_keys |
List[str] |
Required | List of Google AI Studio API keys. |
model_name |
str |
gemini-3-flash-preview |
The Gemini model to use. |
system_instruction |
str |
None |
The system prompt for the model. |
storage |
BaseStorage |
FileStorage |
Storage engine for sessions. |
use_search |
bool |
True |
Enable Google Search grounding. |
max_retries |
int |
keys * 3 |
Total retries before raising an error. |
retry_delay |
float |
5.0 |
Seconds to wait between retries. |
**generation_config |
kwargs |
None |
Any parameter supported by Gemini (temperature, top_p, etc.). |
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📜 License
Distributed under the MIT License. See LICENSE for more information.
Developed with ❤️ for the Gemini Community.
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 gsdk-1.0.5.tar.gz.
File metadata
- Download URL: gsdk-1.0.5.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac9435591bacc210cd2d1a39a74b05c6e2d5a87023fe89463d9fff5da86c9432
|
|
| MD5 |
84b81eea4983e3010846ef7a8fc51ca5
|
|
| BLAKE2b-256 |
1325274c1a2c55670933b10fea71e7c933ed384d0ba4a681fda367d9ab83c1ba
|
File details
Details for the file gsdk-1.0.5-py3-none-any.whl.
File metadata
- Download URL: gsdk-1.0.5-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a405b32cf5e1477944301c563ab8a92c4a1e3e76ff757a04bbb2f88b8375f71
|
|
| MD5 |
ecf0bb241c827c7805eb2bd0f7a484de
|
|
| BLAKE2b-256 |
f1835d66959025bc56a5a06f4d22dd9de13670c2489a81cd95d0dd140f6f3ce5
|