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

This wrapper requires the official Node.js CLI to be available on your system.

# 1. Install the official Node.js CLI globally (requires Node.js):
npm install -g @google/gemini-cli

# 2. Install the Python wrapper via PyPI:
pip install gemini-cli-headless

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.1.tar.gz (6.0 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.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gemini_cli_headless-1.0.1.tar.gz
  • Upload date:
  • Size: 6.0 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.1.tar.gz
Algorithm Hash digest
SHA256 36bff3e18f298bda20e321123dbecb7129f9b6114f41833c83bdcbc2c2f69fac
MD5 0c1911b11c75cfdd22ac55ab9a6fba8a
BLAKE2b-256 07e58b1fb57e1a747ed3ed2d2c2ef192a4cc506efeb2ec86400b776b318e446d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gemini_cli_headless-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 00329d9f12a6aa56ba414e3dd5d24e78c3de1b91111ab25f2ce77d91eba03324
MD5 2e335d177a8140be084c349de51a10d3
BLAKE2b-256 7baa0f56c5a673e9ba6b3ad8ad669338decd95d7076a910889e9e5117dbd878e

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