$ show Sophomoresty/gemini-web2api
This repository implements a reverse-engineered proxy that converts Google Gemini's web interface into an OpenAI-compatible API. The server provides endpoints /v1/chat/completions, /v1/models, /v1/responses, and Google-native /v1beta models, supporting streaming (SSE), function calling, multimodal input (images), and multiple Gemini models with configurable thinking depth. It is architecturally a threaded HTTP server built on Python's standard library, with an optional httpx dependency for true streaming. The code is modular across packages (config, gemini protocol, models, server, tools, multimodal) and includes a separate monolithic entry point for simplicity. It demonstrates robust reverse-engineering of Gemini's StreamGenerate protocol, proper handling of authentication via cookies and SAPISID hashing, and thorough conversion between OpenAI and Gemini message formats. Testing coverage exists for payload building, message parsing, streaming endpoints, and multimodal uploads.
The LLM identified 4 files worth highlighting for reuse:
Core Gemini protocol implementation. Contains methods to build request payloads (80-element array), construct URLs and authentication headers (including SAPISID hash generation), and perform both non-streaming and httpx-based streaming generation with retry logic. This module is fully self-contained and could be reused in any project that needs to call Gemini's internal web API.
Handles conversion between OpenAI Chat Completions / Responses API message formats and Gemini's prompt format, including function calling (tool_call blocks) and image attachment extraction. Also provides Google API content conversion. The tool call parsing and image extraction logic are isolated from server concerns and could be reused in other proxy or client projects.
Implements Scotty resumable upload for Gemini image input, fetching page tokens (Push-ID, X-Client-Pctx), detecting image MIME types from magic bytes, and uploading via Google's content-push API. This module is independent of the rest of the server and could be reused for any Gemini image upload scenario.
Comprehensive test suite using threading and mock to test the modular server endpoints. Covers streaming SSE, chunked request bodies, image upload flows (both data URL and remote URL), Responses API event sequences, and Google native API. Demonstrates a clean pattern for testing HTTP servers with mocked external dependencies.