Skip to main content

A resilient, zero-dependency Python wrapper for the official Google Gemini Node.js CLI.

Project description

gemini-cli-headless

A standalone, zero-dependency Python wrapper for executing the official Node.js Google Gemini CLI (@google/gemini-cli) in fully programmatic, headless mode.

Note: While gemini-cli-headless is a powerful standalone library, it also serves as the foundational execution engine for Cortex, an Autonomous Developer OS for multi-agent software engineering.

Why this wrapper?

While the official Python SDKs are excellent for standard API calls, the @google/gemini-cli provides powerful built-in features for developers working with local codebases (e.g., attaching entire directories via @files or resuming specific sessionId chat histories from the CLI's internal cache).

This wrapper allows you to leverage those CLI-specific features headlessly within your Python scripts, Data pipelines, or RAG systems. It is built for absolute resilience, featuring native retry loops for transient infrastructure drops.

Features

  • Zero Dependencies: Pure Python standard library (no requests, no aiohttp).
  • JSON Parsing: Automatically requests and safely parses the --output-format json from the Node CLI into a clean Python GeminiSession dataclass.
  • Token & Cost Stats: Aggregates inputTokens, outputTokens, and cachedTokens from the raw JSON response.
  • Session Resumption: Supports the -r <sessionId> flag, and even allows you to inject local .json session files directly into the Node CLI cache before execution.
  • Built-in Resilience: Automatically catches transient API drops (like 503 errors) and malformed JSON, retrying the subprocess call seamlessly without crashing your script.

Installation

# Make sure you have the Node.js CLI installed globally first:
npm install -g @google/gemini-cli

# Then install this Python wrapper:
pip install git+https://github.com/jarek108/gemini-cli-headless.git

Quick Start

from gemini_cli_headless import run_gemini_cli_headless

# Provide your API key explicitly, or let the wrapper use your environment variables
my_key = "AIzaSy..."

# Execute a command headlessly with built-in retries
session = run_gemini_cli_headless(
    prompt="Explain quantum computing in one sentence.",
    api_key=my_key,
    max_retries=3
)

print(f"Cost basis - Input: {session.stats.get('inputTokens')}, Output: {session.stats.get('outputTokens')}")
print(f"Response: {session.text}")
print(f"Session ID: {session.session_id}")

Portable Memory (Resuming from a local file)

Instead of relying on the global CLI cache, you can keep session files directly in your project and inject them on the fly.

import shutil
from gemini_cli_headless import run_gemini_cli_headless

# 1. First interaction
session = run_gemini_cli_headless("Remember the secret password is 'Rosebud'.")

# 2. Save the session to your local project
shutil.copy2(session.session_path, "my_context.json")

# ... Days later on a different machine ...

# 3. Resume the conversation later from your local file!
new_session = run_gemini_cli_headless(
    prompt="What was the secret password?",
    session_to_resume="my_context.json"
)

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

gemini_cli_headless-1.0.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

gemini_cli_headless-1.0.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file gemini_cli_headless-1.0.0.tar.gz.

File metadata

  • Download URL: gemini_cli_headless-1.0.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for gemini_cli_headless-1.0.0.tar.gz
Algorithm Hash digest
SHA256 37959afd130035931aded542353cba5a94ead2550f824a4de6b42b42f353096d
MD5 4a6fde69599dcd5b7e500378f529c216
BLAKE2b-256 c17f104f2f155b3e9abaa5b51e89d708b4dc4fa8675a9106b58cc851f91dfc0a

See more details on using hashes here.

File details

Details for the file gemini_cli_headless-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gemini_cli_headless-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4fa523e49c7224147e77fc9c9c00ffa21cfcc83064d452b7a59fcbac9fdbcf1d
MD5 5c1f0172f3586dbd8c0fb12fa89718c0
BLAKE2b-256 ee251d1269512d0e62628d5171e085c4103720b7f2646aba09329ae6ca03fef0

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