Python SDK for Gemini CLI - Experimental
Project description
Gemini CLI SDK for Python
⚠️ EXPERIMENTAL: This SDK is in early development and uses LLM-based parsing as a temporary solution. The API may change as Gemini CLI evolves.
Python SDK for Gemini CLI, providing programmatic access to Gemini with an API compatible with Claude Code SDK.
Installation
pip install gemini-cli-sdk
Prerequisites:
- Python 3.10+
- Gemini CLI installed:
npm install -g @google/gemini-cli - OpenAI API key (for LLM-based parsing)
Quick Start
import anyio
from gemini_cli_sdk import query
async def main():
async for message in query(prompt="What is 2 + 2?"):
print(message)
anyio.run(main)
Migration from Claude Code SDK
This SDK is designed to be API-compatible with Claude Code SDK. In most cases, you can migrate by simply changing imports:
Option 1: Update imports (recommended)
# Before
from claude_code_sdk import query, ClaudeCodeOptions
# After
from gemini_cli_sdk import query, GeminiOptions
Option 2: Use compatibility aliases (zero code changes!)
# This works with Gemini SDK!
from gemini_cli_sdk import query, ClaudeCodeOptions
Complete example
# This Claude SDK code works unchanged with Gemini SDK
from gemini_cli_sdk import (
query,
ClaudeCodeOptions, # Compatibility alias
AssistantMessage,
TextBlock
)
options = ClaudeCodeOptions(
system_prompt="You are helpful",
max_turns=1
)
async for message in query(prompt="Hello", options=options):
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(block.text)
See MIGRATION.md for detailed migration guide.
Current Limitations
As Gemini CLI doesn't yet support structured JSON output, this SDK uses LLM-based parsing:
- ✅ Structured message types (compatible with Claude SDK)
- ✅ Async iteration pattern
- ✅ Basic error handling
- ⚠️ Additional latency from parsing (~100-500ms)
- ⚠️ Requires OpenAI API key for parsing
- ❌ Tool use blocks (not yet supported)
- ❌ Session management (not exposed by Gemini CLI)
- ❌ Cost tracking (no data available)
Environment Variables
GEMINI_API_KEYorGOOGLE_API_KEY: Your Gemini API keyOPENAI_API_KEY: Required for LLM-based parsingGEMINI_PARSER_MODEL: LLM model for parsing (default:gpt-4o-mini)
Examples
Basic Query
from gemini_cli_sdk import query, GeminiOptions, AssistantMessage, TextBlock
async for message in query(prompt="Hello Gemini"):
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(block.text)
With Options
options = GeminiOptions(
model="gemini-2.0-flash",
system_prompt="You are a helpful assistant"
)
async for message in query(prompt="Explain Python", options=options):
print(message)
How It Works
- Subprocess Execution: Runs Gemini CLI as a subprocess
- LLM Parsing: Uses Instructor + OpenAI to parse plain text output into structured messages
- Type Safety: Provides Pydantic models compatible with Claude SDK
When Gemini CLI adds native JSON output support, the SDK will automatically switch to use it without requiring code changes.
Contributing
This is an experimental project. Contributions, issues, and feedback are welcome!
License
MIT
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_cli_sdk-0.1.0.tar.gz.
File metadata
- Download URL: gemini_cli_sdk-0.1.0.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ae9ee02ade8ed6fe1b7da723dff207c20d0c0d3d6de777bc29c099a3592126b
|
|
| MD5 |
256f53aa7a3c734f85a38f404a10572d
|
|
| BLAKE2b-256 |
72e52b914e6b8e5de76dd930785f45b2a4e2a8e656f1ff0afe6092fae39c8557
|
File details
Details for the file gemini_cli_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gemini_cli_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e0ab441dd433b4f3a12b98cdc7d0ab2a693e3509d1e404217feec8a9ce86bb0
|
|
| MD5 |
bd08237b9f70b7c9d9faf4cfe4db3984
|
|
| BLAKE2b-256 |
d54a90afd1c2c51f13460a980bd939ed72ebd43c526eeb0ebd440ad08293f45c
|