Skip to main content

Gemini Banner

PyPI Downloads Dependencies License Code style

GitHub stars GitHub issues CI

Gemini Icon Gemini-API

A reverse-engineered asynchronous Python wrapper for the Google Gemini web app (formerly Bard).

Features

  • Persistent Cookies - Automatically refreshes cookies in background. Optimized for always-on services.
  • Image Generation - Natively supports generating and editing images with natural language.
  • Video & Audio Generation - Supports generating videos and audio/music content natively.
  • Deep Research - Full deep research workflow with plan creation, status polling, and result retrieval.
  • System Prompt - Supports customizing the model's system prompt with Gemini Gems.
  • Extension Support - Supports generating content with Gemini extensions, such as YouTube and Gmail.
  • Classified Outputs - Categorizes text, thoughts, images, videos, and audio in the response.
  • Streaming Mode - Supports stream generation, yielding partial outputs as they are generated.
  • CLI Tool - Standalone command-line interface for quick interactions.
  • Official Flavor - Provides a simple and elegant interface inspired by Google Generative AI's official API.
  • Asynchronous - Utilizes asyncio to run generation tasks and return outputs efficiently.

Table of Contents

Installation

Install or update the package with pip.

pip install -U gemini_webapi

Optionally, the package offers a way to automatically import cookies from your local browser via optional dependency browser-cookie3. To enable this feature, install gemini_webapi[browser] instead. Supported platforms and browsers can be found here.

pip install -U gemini_webapi[browser]

Authentication

  • Go to https://gemini.google.com and log in with your Google account
  • Press F12 to open the web inspector, go to the Network tab, and refresh the page
  • Click any request and copy the cookie values of __Secure-1PSID and __Secure-1PSIDTS
services:
  main:
    environment:
      GEMINI_COOKIE_PATH: /tmp/gemini_webapi
    volumes:
      - ./gemini_cookies:/tmp/gemini_webapi

Usage

Initialization

Import the required packages and initialize a client with your cookies from the previous step. After successful initialization, the API will automatically refresh __Secure-1PSIDTS in the background as long as the process is alive.

import asyncio
from gemini_webapi import GeminiClient

# Replace "COOKIE VALUE HERE" with your actual cookie values.
# Leave Secure_1PSIDTS empty if it's not available for your account.
Secure_1PSID = "COOKIE VALUE HERE"
Secure_1PSIDTS = "COOKIE VALUE HERE"


async def main():
    # If browser-cookie3 is installed, simply use `client = GeminiClient()`
    client = GeminiClient(Secure_1PSID, Secure_1PSIDTS, proxy=None)
    await client.init(timeout=30, auto_close=False, close_delay=300, auto_refresh=True)


asyncio.run(main())

Generate Content

Ask a single-turn question by calling GeminiClient.generate_content, which returns a gemini_webapi.ModelOutput object containing the generated text, images, thoughts, and conversation metadata.

async def main():
    response = await client.generate_content("Hello World!")
    print(response.text)


asyncio.run(main())

Generate Content with Files

Gemini supports file input, including images and documents. Optionally, you can pass files as a list of paths in str or pathlib.Path to GeminiClient.generate_content together with a text prompt.

async def main():
    response = await client.generate_content(
        "Introduce the contents of these two files. Is there any connection between them?",
        files=["assets/sample.pdf", Path("assets/banner.png")],
    )
    print(response.text)


asyncio.run(main())

Conversations Across Multiple Turns

If you want to keep the conversation continuous, use GeminiClient.start_chat to create a gemini_webapi.ChatSession object and send messages through it. The conversation history will be handled automatically and updated after each turn.

async def main():
    chat = client.start_chat()
    response1 = await chat.send_message(
        "Introduce the contents of these two files. Is there any connection between them?",
        files=["assets/sample.pdf", Path("assets/banner.png")],
    )
    print(response1.text)
    response2 = await chat.send_message(
        "Use image generation tool to modify the banner with another font and design."
    )
    print(response2.text, response2.images, sep="\n\n----------------------------------\n\n")


asyncio.run(main())

Continue Previous Conversations

To manually retrieve previous conversations, you can pass a previous ChatSession's metadata to GeminiClient.start_chat when creating a new ChatSession. Alternatively, you can persist previous metadata to a file or database if you need to access it after the current Python process has exited.

async def main():
    # Start a new chat session
    chat = client.start_chat()
    response = await chat.send_message("Fine weather today")

    # Save chat's metadata
    previous_session = chat.metadata

    # Load the previous conversation
    previous_chat = client.start_chat(metadata=previous_session)
    response = await previous_chat.send_message("What was my previous message?")
    print(response)


asyncio.run(main())

Read Conversation History

You can read the conversation history of a specific chat by calling GeminiClient.read_chat with the chat ID. It returns a ChatHistory object containing a list of ChatTurn objects ordered from newest to oldest.

async def main():
    chat = client.start_chat()
    await chat.send_message("What is the capital of France?")

    # Read the chat history
    history = await client.read_chat(chat.cid)
    if history:
        for turn in history.turns:
            print(f"[{turn.role.upper()}] {turn.text}")
            print("\n----------------------------------\n")


asyncio.run(main())

To list all recent chats, use GeminiClient.list_chats:

async def main():
    chats = client.list_chats()
    if chats:
        for chat_info in chats:
            print(f"{chat_info.cid}: {chat_info.title}")


asyncio.run(main())

Delete Previous Conversations from Gemini History

You can delete a specific chat from Gemini history on the server by calling GeminiClient.delete_chat with the chat ID.

async def main():
    # Start a new chat session
    chat = client.start_chat()
    await chat.send_message("This is a temporary conversation.")

    # Delete the chat
    await client.delete_chat(chat.cid)
    print(f"Chat deleted: {chat.cid}")


asyncio.run(main())

Temporary Mode

You can start a temporary chat by passing temporary=True to GeminiClient.generate_content or ChatSession.send_message. Temporary chats won't be saved in Gemini history.

async def main():
    response = await client.generate_content("Hello World!", temporary=True)
    print(response.text, "\n\n----------------------------------\n\n")

    chat = client.start_chat()
    await chat.send_message("Fine weather today", temporary=False)
    response2 = await chat.send_message("What's my last message?", temporary=True)
    print(response2.text)


asyncio.run(main())

Streaming Mode

For longer responses, you can use streaming mode to receive partial outputs as they are generated. This provides a more responsive user experience, especially for real-time applications like chatbots.

The generate_content_stream method yields ModelOutput objects where the text_delta attribute contains only the new characters received since the last yield, making it easy to display incremental updates.

async def main():
    async for chunk in client.generate_content_stream(
        "What's the difference between 'await' and 'async for'?"
    ):
        print(chunk.text_delta, end="", flush=True)

    print()


asyncio.run(main())

Select Language Model

You can specify which language model to use by passing the model argument to GeminiClient.generate_content or GeminiClient.start_chat. Omit it, or pass None, to let Google use your account's default model.

Models are discovered dynamically at init time from your account, along with the request headers and tier capacity each one needs. Pass a name, alias, display name or id string - or an AvailableModel straight from list_models(). Names follow whatever Google publishes to your account (currently gemini-pro, gemini-flash and gemini-flash-lite), so run the snippet below to see your own list rather than hardcoding one.

async def main():
    for model in client.list_models() or []:
        print(model.model_name, "|", model.display_name, "| available:", model.is_available)

    response1 = await client.generate_content(
        "What's your language model version? Reply with the version number only.",
        model="gemini-flash",  # a name, alias, display name or id
    )
    print(f"Model version (gemini-flash): {response1.text}")

    chat = client.start_chat(model=client.resolve_model("pro"))
    response2 = await chat.send_message(
        "What's your language model version? Reply with the version number only."
    )
    print(f"Model version ({chat.model}): {response2.text}")


asyncio.run(main())

You can also pass custom model header strings directly to access models that are not listed above.

# "model_name" and "model_header" keys must be present
custom_model = {
    "model_name": "xxx",
    "model_header": {
        "x-goog-ext-525001261-jspb": '[1,null,null,null,"fbb127bbb056c959",null,null,0,[4,5,6,8],null,null,1,null,null,1,1,"EA3C5672-E422-4A5F-BE26-B5B57D3B9AC3"]',
        "x-goog-ext-73010989-jspb": "[0]",
        "x-goog-ext-73010990-jspb": "[0,0,0]",
    },
}

response = await client.generate_content("What's your model version?", model=custom_model)

List Available Models

The client dynamically discovers which models are available for your account at initialization. Use GeminiClient.list_models to see all available models and their details.

async def main():
    await client.init()  # Make sure the client is initialized first
    models = client.list_models()
    if models:
        for model in models:
            print(f"{model.display_name}: {model.model_name}")


asyncio.run(main())

Apply System Prompt with Gemini Gems

System prompts can be applied to conversations via Gemini Gems. To use a gem, you can pass the gem argument to GeminiClient.generate_content or GeminiClient.start_chat. gem can be either a gem ID string or a gemini_webapi.Gem object. Only one gem can be applied to a single conversation.

async def main():
    # Fetch all gems for the current account, including both predefined and user-created ones
    await client.fetch_gems(include_hidden=False)

    # Once fetched, gems will be cached in `GeminiClient.gems`
    gems = client.gems

    # Get the gem you want to use
    system_gems = gems.filter(predefined=True)
    coding_partner = system_gems.get(id="coding-partner")

    response1 = await client.generate_content(
        "What's your system prompt?",
        gem=coding_partner,
    )
    print(response1.text)

    # Another example with a user-created custom gem
    # Gem ids are consistent strings. Store them somewhere to avoid fetching gems every time
    your_gem = gems.get(name="Your Gem Name")
    your_gem_id = your_gem.id
    chat = client.start_chat(gem=your_gem_id)
    response2 = await chat.send_message("What's your system prompt?")
    print(response2)

Manage Custom Gems

You can create, update, and delete your custom gems programmatically with the API. Note that predefined system gems cannot be modified or deleted.

Create a Custom Gem

Create a new custom gem with a name, system prompt (instructions), and optional description:

async def main():
    # Create a new custom gem
    new_gem = await client.create_gem(
        name="Python Tutor",
        prompt="You are a helpful Python programming tutor.",
        description="A specialized gem for Python programming",
    )

    print(f"Custom gem created: {new_gem}")

    # Use the newly created gem in a conversation
    response = await client.generate_content(
        "Explain how list comprehensions work in Python", gem=new_gem
    )
    print(response.text)


asyncio.run(main())

Update an Existing Gem

async def main():
    # Get a custom gem (assuming you have one named "Python Tutor")
    await client.fetch_gems()
    python_tutor = client.gems.get(name="Python Tutor")

    # Update the gem with new instructions
    updated_gem = await client.update_gem(
        gem=python_tutor,  # Can also pass gem ID string
        name="Advanced Python Tutor",
        prompt="You are an expert Python programming tutor.",
        description="An advanced Python programming assistant",
    )

    print(f"Custom gem updated: {updated_gem}")


asyncio.run(main())

Delete a Custom Gem

async def main():
    # Get the gem to delete
    await client.fetch_gems()
    gem_to_delete = client.gems.get(name="Advanced Python Tutor")

    # Delete the gem
    await client.delete_gem(gem_to_delete)  # Can also pass gem ID string
    print(f"Custom gem deleted: {gem_to_delete.name}")


asyncio.run(main())

Retrieve Model's Thought Process

When using models with thinking capabilities, the model's thought process will be populated in ModelOutput.thoughts.

async def main():
    response = await client.generate_content("What's 1+1?", model="gemini-pro")
    print(response.thoughts)
    print(response.text)


asyncio.run(main())

Retrieve Images in Response

Images in the API's output are stored as a list of gemini_webapi.Image objects. You can access the image title, URL, and description by calling Image.title, Image.url and Image.alt respectively.

async def main():
    response = await client.generate_content("Send me some pictures of cats")
    for image in response.images:
        print(image, "\n\n----------------------------------\n\n")


asyncio.run(main())

Generate and Edit Images

You can ask Gemini to generate and edit images with Nano Banana, Google's latest image model, using natural language.

You can save images returned from Gemini locally by calling Image.save(). Optionally, you can specify the file path and file name by passing path and filename arguments to the function. This works for both WebImage and GeneratedImage.

async def main():
    response = await client.generate_content("Generate some pictures of cats")
    for i, image in enumerate(response.images):
        await image.save(path="temp/", filename=f"cat_{i}.png", verbose=True)
        print(image, "\n\n----------------------------------\n\n")


asyncio.run(main())

Retrieve Videos and Audio

Gemini can generate videos and audio/music content. These are returned as GeneratedVideo and GeneratedMedia objects in ModelOutput.videos and ModelOutput.media respectively. You can save them to disk just like images.

async def main():
    response = await client.generate_content("Generate a short video of a cat playing")

    # Save generated videos
    for video in response.videos:
        result = await video.save(path="temp/", verbose=True)
        print(f"Video saved: {result}")

    # Save generated media (audio/music)
    for media in response.media:
        result = await media.save(path="temp/", verbose=True)
        print(f"Media saved: {result}")


asyncio.run(main())

Generate Content with Gemini Extensions

After activating extensions for your account, you can access them in your prompts either in natural language or by starting your prompt with "@" followed by the extension keyword.

async def main():
    response1 = await client.generate_content("@Gmail What's the latest message in my mailbox?")
    print(response1, "\n\n----------------------------------\n\n")

    response2 = await client.generate_content(
        "@Youtube What's the latest activity of Taylor Swift?"
    )
    print(response2, "\n\n----------------------------------\n\n")


asyncio.run(main())

Check and Switch to Other Reply Candidates

A response from Gemini sometimes contains multiple reply candidates with different generated content. You can check all candidates and choose one to continue the conversation. By default, the first candidate is chosen.

async def main():
    # Start a conversation and list all reply candidates
    chat = client.start_chat()
    response = await chat.send_message("Recommend a science fiction book for me.")
    for candidate in response.candidates:
        print(candidate, "\n\n----------------------------------\n\n")

    if len(response.candidates) > 1:
        # Control the ongoing conversation flow by choosing candidate manually
        new_candidate = chat.choose_candidate(index=1)  # Choose the second candidate here
        followup_response = await chat.send_message(
            "Tell me more about it."
        )  # Will generate content based on the chosen candidate
        print(new_candidate, followup_response, sep="\n\n----------------------------------\n\n")
    else:
        print("Only one candidate available.")


asyncio.run(main())

Deep Research

Gemini's deep research feature is an autonomous research agent that browses the web, analyzes sources, and produces a comprehensive report. You can access it programmatically through the API.

Under the hood this is an ordinary chat conversation: Gemini answers the first turn with a plan, and a second turn confirms it and starts the research. The task then runs server-side, detached from the session, so polling for the report does not require holding the conversation open.

The finished report is delivered as an inline document, not as reply text — the reply itself is only a short notice such as "I've completed your research". result.text returns the report, and result.document exposes it with its id and title:

result = await client.deep_research("...")
doc = result.document
print(doc.title)  # 'SQLite vs DuckDB for Analytics'
print(doc.content)  # markdown report: headings, tables, links, [cite: N] markers
print(doc.sources)  # the web sources those [cite: N] markers resolve to

Path("report.md").write_text(doc.markdown, encoding="utf-8")  # body + numbered source list

The same document is available on any deep research turn as output.deep_research_document, so a report can be recovered later from the chat alone:

output = await client.fetch_latest_chat_response("c_abc123")
if (doc := output.deep_research_document) and doc.ready:
    print(doc.content)

Quick one-call method:

async def main():
    result = await client.deep_research(
        "Compare the top 3 cloud providers and their AI offerings",
        poll_interval=10.0,
        timeout=600.0,
    )
    print(f"Done: {result.done}")
    print(result.text)


asyncio.run(main())

Both turns run in a single chat session. Pass model to choose one, or chat to supply your own session and keep a reference to the conversation after the call returns.

Step-by-step workflow for more control, including reviewing the plan before committing to it:

async def main():
    # Plan and confirmation are two turns of one conversation, so share a chat session
    chat = client.start_chat()

    # Step 1: Create a research plan
    plan = await client.create_deep_research_plan(
        "What are the latest advancements in quantum computing?",
        chat=chat,
    )
    print(f"Title: {plan.title}")
    print(f"ETA: {plan.eta_text}")
    for step in plan.steps:
        print(f"  - {step}")

    # Step 2 (optional): Ask for changes on the same session to get a revised plan back
    # plan = await client.create_deep_research_plan("Focus on error correction", chat=chat)

    # Step 3: Confirm the plan to start the research
    await client.start_deep_research(plan, chat=chat)

    # Step 4: Poll for completion
    result = await client.wait_for_deep_research(
        plan,
        poll_interval=10.0,
        timeout=600.0,
    )

    print(result.text)


asyncio.run(main())

Logging Configuration

This package uses loguru for logging and exposes a function set_log_level to control the log level. You can set the log level to one of the following values: DEBUG, INFO, WARNING, ERROR, and CRITICAL. The default value is INFO.

from gemini_webapi import set_log_level

set_log_level("DEBUG")

CLI Tool

A standalone CLI (cli.py) is included for interacting with Gemini from the terminal. It supports single-turn questions, multi-turn chat, deep research, image download, and account diagnostics.

Export your cookies from gemini.google.com and save them as a JSON file. The CLI supports multiple formats:

{ "__Secure-1PSID": "value...", "__Secure-1PSIDTS": "value..." }

You can also use a browser cookie extension export (array-of-objects format is supported).

CLI Commands

Global options (placed before the subcommand):

--cookies-json PATH    Path to cookies JSON file (required)
--proxy URL            Proxy URL (or uses HTTPS_PROXY env)
--model NAME           Model name, alias or id (default: your account's default model)
--verbose              Enable debug logging
--no-persist           Don't update cookies file after run
--request-timeout SEC  HTTP timeout in seconds (default: 300)

Available commands:

# Ask a single question (streams by default)
python cli.py --cookies-json cookies.json ask "What is quantum computing?"

# Ask with image input
python cli.py --cookies-json cookies.json ask --image photo.jpg "Describe this"

# Non-streaming mode
python cli.py --cookies-json cookies.json ask --no-stream "Hello"

# Continue a conversation (chat ID from previous output)
python cli.py --cookies-json cookies.json reply c_abc123 "Tell me more"

# List your chat history
python cli.py --cookies-json cookies.json list

# Read a specific chat conversation
python cli.py --cookies-json cookies.json read c_abc123

# List the models your account can use
python cli.py --cookies-json cookies.json models

# Download a generated image
python cli.py --cookies-json cookies.json download "https://..." -o output.png

# Account diagnostics (status, quotas, usage limits, available models)
python cli.py --cookies-json cookies.json inspect

Deep Research Workflow

The CLI supports Gemini's Deep Research feature — an autonomous research agent that browses the web, analyzes sources, and produces a comprehensive report.

# 1. Submit a research task. Prints both a chat ID and a research (task) ID
python cli.py --cookies-json cookies.json research send --prompt "AI chip competition 2025"

# 2. Check progress. Pass --research-id for the actual task state - without it the chat
#    cannot be told apart from the plan and confirmation replies, which arrive immediately
python cli.py --cookies-json cookies.json research check c_abc123 --research-id 0f3d...

# 3. Fetch the full result
python cli.py --cookies-json cookies.json research get c_abc123

# 4. Save result to a file
python cli.py --cookies-json cookies.json research get c_abc123 --output report.md

References

Google AI Studio

acheong08/Bard

Stargazers

Star History Chart

Metadata

Release files for gemini-webapi 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gemini-webapi 2.1.0
File Size Uploaded Provenance Actions
gemini_webapi-2.1.0.tar.gz 335.0 kB With provenance Details for gemini_webapi-2.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for gemini-webapi 2.1.0
File Interpreter ABI Platform Actions
gemini_webapi-2.1.0-py3-none-any.whl Python 3 none any Details for gemini_webapi-2.1.0-py3-none-any.whl

Total release size: 450.3 kB

Release files / gemini_webapi-2.1.0.tar.gz

Download URL gemini_webapi-2.1.0.tar.gz
Size 335.0 kB
Tags Source
SHA-256 checksum
How to use checksums
08e1e3c659134b2b99c4c1980d4ff9c896fefbdf14b4cd3a9d8326f6cbe87eaa
BLAKE2b-256 checksum
How to use checksums
881b895b9f018864ecbeb9bfed65c73c1f787212f175c183441c8d9f4242763f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

Release files / gemini_webapi-2.1.0-py3-none-any.whl

Download URL gemini_webapi-2.1.0-py3-none-any.whl
Size 115.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce96cef1472d3b6aab906c7a45b6d400bd737bbb05baee8d3689ef91bd6620f7
BLAKE2b-256 checksum
How to use checksums
09da5f137f06a36dfa4232122cc51acd2984f3225b02daefa4c1a5e39cc81230
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

Security for gemini-webapi 2.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for gemini_webapi-2.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page