Skip to main content

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 real-time streaming.

PyPI version Python versions License: MIT

✨ Key Features

  • 🔑 Smart Key Rotation: Automatically switch between multiple API keys when you hit rate limits (429/403).
  • 🌊 Streaming Support: Real-time response generation with automatic history updates.
  • 🔄 Configurable Retries: Set custom retry counts and delays for maximum stability.
  • 💾 Session Persistence: Built-in support for File and Redis storage.
  • ⚙️ Full Flexibility: Pass any generation parameter (temperature, top_p, max_tokens) globally or per request.
  • 🔍 Google Search Grounding: Integrated real-time web search capabilities.
  • 📁 Media Support: Simplified async file uploads for multimodal tasks.

📦 Installation

pip install gsdk

🚀 Quick Start

Basic Chat

import asyncio
from gsdk import GeminiSDK

async def main():
    sdk = GeminiSDK(api_keys=["YOUR_API_KEY"], model_name="gemini-flash-latest")

    response = await sdk.ask("session_1", "Hello! Who are you?")
    print(f"AI: {response.text}")

asyncio.run(main())

🌊 Real-time Streaming

Perfect for chat interfaces where you want to show text as it is generated.

async def stream_example():
    sdk = GeminiSDK(api_keys=["KEY_1", "KEY_2"])
    
    print("AI: ", end="", flush=True)
    async for chunk in sdk.ask_stream("session_1", "Write a long poem about coding."):
        print(chunk, end="", flush=True)

asyncio.run(stream_example())

🛠 Advanced Usage

1. Production Storage (Redis)

Share session history across multiple workers or servers.

from gsdk.storage import RedisStorage

storage = RedisStorage(host='localhost', port=6379)
sdk = GeminiSDK(api_keys=["..."], storage=storage)

2. Handling Images and Files

# Upload image or document
media = await sdk.media.upload_file("chart.png")

# Multimodal request
response = await sdk.ask("session_2", [media, "Analyze this chart."])
print(response.text)

3. Google Search Grounding

sdk = GeminiSDK(api_keys=["..."], use_search=True)

response = await sdk.ask("news", "What's happening in AI today?")
print(f"Sources used: {response.sources}")

📖 API Reference

GeminiSDK Methods

Method Description
ask(session_id, content, **kwargs) Sends a message and returns a full GeminiResponse.
ask_stream(session_id, content, **kwargs) Async Generator. Yields text chunks and saves history after completion.
media.upload_file(path) Uploads a file to Google servers for multimodal input.

Configuration Parameters

Parameter Type Default Description
api_keys List[str] Required List of keys for rotation.
model_name str gemini-3-flash-preview The Gemini model version.
storage BaseStorage FileStorage How to store conversation history.
max_retries int keys * 3 Total attempts for failed requests.
retry_delay float 5.0 Seconds to wait after a rate limit.
**gen_config kwargs None Global defaults for temperature, top_p, etc.

🤝 Contributing

  1. Fork the Project.
  2. Create your Feature Branch.
  3. Commit your Changes.
  4. Push to the Branch.
  5. Open a Pull Request.

📜 License

Distributed under the MIT License.


gsdk — Powering the next generation of Gemini applications. 🚀

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.9.tar.gz (7.5 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.9-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gsdk-1.0.9.tar.gz
  • Upload date:
  • Size: 7.5 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.9.tar.gz
Algorithm Hash digest
SHA256 c9ad1f63bd3b31cf94f4dc3963054d3bf45f56a8fce45e7e04d8b6de0483d55e
MD5 2449190abad74bf4cc13b6c9a63cc2cc
BLAKE2b-256 8df999e3377ae01151a03d7e62551b816a680dd4b4f127ec9a37cd8be314f68b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gsdk-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 8.3 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.9-py3-none-any.whl
Algorithm Hash digest
SHA256 65d3075f5e1b89d4a4e51db87c01b4ee7de0dd342a725f87a2883b315856ed3d
MD5 702687029178a5d0210904fa07c45b03
BLAKE2b-256 541b1cfa7fcacc2661dfa852ae2fddce3f552d7ea90738aa8bfa0009a361e96a

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