Skip to content

feat(core): add OpenAI-compatible provider support - #23386

Closed
netbrah wants to merge 2 commits into
google-gemini:mainfrom
netbrah:feat/openai-compatible-provider
Closed

netbrah wants to merge 2 commits into
google-gemini:mainfrom
netbrah:feat/openai-compatible-provider

Conversation

@netbrah

@netbrah netbrah commented Mar 21, 2026

Copy link
Copy Markdown

Fixes #23385

What

Add OpenAIContentGenerator that implements the existing ContentGenerator interface, enabling gemini-cli to work with any OpenAI-compatible API endpoint — OpenAI, Anthropic Claude (via proxy), local models (Ollama, vLLM), and corporate LLM gateways.

Why

Multi-provider support is the most impactful way to grow gemini-cli's community. The agent loop, MCP integration, tool system, and TUI are excellent — but they're currently locked to the Gemini API. This PR unlocks them for every OpenAI-compatible model while preserving all Gemini-native functionality unchanged.

This is battle-tested: we've been running this architecture against Claude Opus 4, GPT-4o, and corporate LLM proxies on large codebases (100K+ files) for weeks.

How

File Change
packages/core/src/core/openaiContentGenerator.ts New 740-line file: Content/Part ↔ OpenAI message translation, SSE streaming, tool call accumulation, context budget enforcement, schema cleaning
packages/core/src/core/contentGenerator.ts Add OPENAI_COMPATIBLE auth type, env var detection, factory wiring
packages/core/src/core/tokenLimits.ts Pattern-based matching for Claude, GPT, o1/o3, Codex models
packages/cli/src/ui/utils/updateCheck.ts Skip update banner when running via proxy
packages/core/src/core/openaiContentGenerator.test.ts 11 tests: non-streaming, streaming, tool calls, schema cleaning, model defaults, conversation translation
packages/core/src/core/tokenLimits.test.ts Extended with 7 new model limit tests

Activation (zero-config for existing users):

export OPENAI_COMPATIBLE_ENDPOINT="https://api.openai.com"
export OPENAI_COMPATIBLE_API_KEY="sk-..."
export OPENAI_COMPATIBLE_MODEL="gpt-4o"  # optional

Test Plan

  • npx vitest run packages/core/src/core/openaiContentGenerator.test.ts — 11 tests pass
  • npx vitest run packages/core/src/core/tokenLimits.test.ts — 6 tests pass (including 7 new model cases)
  • npx tsc --noEmit -p packages/core/tsconfig.json — clean
  • Pre-commit hooks (ESLint + Prettier) pass
  • Zero changes to existing Gemini flow — all existing tests unaffected

Made with Cursor

@netbrah
netbrah requested a review from a team as a code owner March 21, 2026 14:24
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces significant enhancements to the gemini-cli by integrating support for OpenAI-compatible API endpoints. This change broadens the tool's applicability, allowing users to leverage a wider array of large language models and services. The implementation includes a new content generator, intelligent context management, and seamless translation between different API formats, ensuring that the core functionalities of gemini-cli are accessible across diverse LLM ecosystems.

Highlights

  • OpenAI-Compatible Provider Support: Added a new OpenAIContentGenerator that implements the existing ContentGenerator interface, allowing gemini-cli to work with any OpenAI-compatible API endpoint, including OpenAI, Anthropic Claude (via proxy), local models (Ollama, vLLM), and corporate LLM gateways.
  • Enhanced Multi-Provider Functionality: Unlocks the agent loop, MCP integration, tool system, and TUI for every OpenAI-compatible model, significantly growing the gemini-cli community by extending its capabilities beyond the Gemini API while preserving all Gemini-native functionality.
  • Context Budget Enforcement and Tool Handling: Implemented pre-send context budget enforcement by estimating token counts and trimming tool outputs, along with robust translation for content, tool calls, and function responses between Gemini and OpenAI formats, including SSE streaming accumulation.
  • Dynamic Token Limits and Update Check Logic: Updated token limit detection to include pattern-based matching for various OpenAI-compatible models (e.g., Claude, GPT, Codex) and modified the update check mechanism to skip when running via an OpenAI-compatible proxy.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for OpenAI-compatible providers by adding a new OpenAIContentGenerator. The changes are extensive, including the new generator, modifications to the content generator factory, and updates to token limit calculations. The implementation is thorough and includes comprehensive tests.

My review identified a potential issue in the new OpenAIContentGenerator. There's a risk of silently dropping tool responses when multiple responses lack an ID due to how they are deduplicated. This issue is flagged as high severity and should be addressed to ensure correctness and robustness.

Comment on lines +290 to +294
for (const fr of funcResponses) {

const frId = fr.functionResponse!.id ?? '';
seenToolIds.set(frId, fr);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There's a potential for data loss here when handling functionResponse parts. If multiple functionResponse parts in the contents array are missing an id, they will all be assigned an empty string '' as their frId. Consequently, they will overwrite each other in the seenToolIds map, and only the last one will be processed. This could lead to silently dropping tool responses, causing incorrect behavior in the agent loop.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 38cebcd — functionResponse parts without an id now get unique index-based fallback keys (_fr_0, _fr_1, ...) instead of collapsing into empty string. No data loss.

@gemini-cli gemini-cli Bot added the area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality label Mar 21, 2026
netbrah added 2 commits March 21, 2026 22:02
Enable gemini-cli to work with any OpenAI-compatible API endpoint,
including OpenAI, Anthropic Claude (via proxy), local models (Ollama,
vLLM), and corporate LLM gateways.

Architecture:
- OpenAIContentGenerator implements the ContentGenerator interface,
  translating Gemini's Content/Part format to OpenAI chat completions
  and back
- Factory in contentGenerator.ts detects OPENAI_COMPATIBLE_ENDPOINT +
  OPENAI_COMPATIBLE_API_KEY env vars and creates the right generator
- Full streaming support via SSE parsing with tool call accumulation
- Model-aware max_tokens defaults prevent silent output truncation
- Pre-send context budget enforcement trims tool outputs to fit
  smaller context windows (Claude 200K, GPT 128K)
- Token limits expanded with pattern-based matching for non-Gemini
  models

All Gemini-native functionality is preserved unchanged. OpenAI support
is purely additive.

Made-with: Cursor
Avoid data loss when multiple functionResponse parts lack an id field.
Previously they all mapped to empty string and only the last survived.
Now each gets a unique fallback key (_fr_0, _fr_1, ...).

Made-with: Cursor
@gemini-cli

gemini-cli Bot commented Apr 5, 2026

Copy link
Copy Markdown
Contributor

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we have updated our contribution policy (see Discussion #17383).

We only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. All other community pull requests are subject to closure after 14 days if they do not align with our current focus areas. For this reason, we strongly recommend that contributors only submit pull requests against issues explicitly labeled as 'help-wanted'.

This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding and for being part of our community!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality size/xl An extra large PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(core): Support OpenAI-compatible API endpoints for multi-provider workflows

2 participants