Skip to main content

Fast, lightweight SDK for 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.

PyPI version Python versions License: MIT

✨ 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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📜 License

Distributed under the MIT License. See LICENSE for more information.


Developed with ❤️ for the Gemini Community.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gsdk-1.0.4.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gsdk-1.0.4-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file gsdk-1.0.4.tar.gz.

File metadata

  • Download URL: gsdk-1.0.4.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for gsdk-1.0.4.tar.gz
Algorithm Hash digest
SHA256 283053662951597eb0bccbd006bb8c5dac095b6634649b40f47cc0e862433999
MD5 7a5a6c7738c3e944690d2493959eb510
BLAKE2b-256 12857601c96ab4a957aa07cd973965654fc8f8a8ee9556286507158fcb534e31

See more details on using hashes here.

File details

Details for the file gsdk-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: gsdk-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for gsdk-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 95fc83c35ba746f115c428a389afdad290e3c3585b8516c63ea955990f3f2554
MD5 e067a98246cd3bad6b2f81dc9d2d05c5
BLAKE2b-256 914555d7d2e6f022c083499cfe7e1d9fe74b9d296a00501a5f67bcfb79c6ffc5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page