-
Notifications
You must be signed in to change notification settings - Fork 197
fix(providers): strip models/ prefix for Gemini OpenAI-compat endpoint (#175) #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+136
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { isGeminiOpenAICompat, normalizeGeminiModelId } from './gemini-compat'; | ||
|
|
||
| describe('isGeminiOpenAICompat', () => { | ||
| it('detects the official Gemini OpenAI-compat endpoint', () => { | ||
| expect(isGeminiOpenAICompat('https://generativelanguage.googleapis.com/v1beta/openai/')).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it('returns false for non-Gemini bases', () => { | ||
| expect(isGeminiOpenAICompat('https://api.openai.com/v1')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false when baseUrl is undefined', () => { | ||
| expect(isGeminiOpenAICompat(undefined)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false when baseUrl is empty', () => { | ||
| expect(isGeminiOpenAICompat('')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false when baseUrl is not a parseable URL', () => { | ||
| expect(isGeminiOpenAICompat('not a url')).toBe(false); | ||
| }); | ||
|
|
||
| it('rejects spoofed URLs with Gemini host in query string', () => { | ||
| expect( | ||
| isGeminiOpenAICompat('https://attacker.com/?x=generativelanguage.googleapis.com/v1'), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it('rejects spoofed URLs with Gemini host as subdomain suffix of attacker domain', () => { | ||
| expect(isGeminiOpenAICompat('https://generativelanguage.googleapis.com.evil.com/v1')).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
|
|
||
| it('rejects spoofed URLs with Gemini host hyphenated into attacker domain', () => { | ||
| expect(isGeminiOpenAICompat('https://generativelanguage-googleapis-com.evil.com')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('normalizeGeminiModelId', () => { | ||
| it('strips the models/ prefix for Gemini hosts', () => { | ||
| expect( | ||
| normalizeGeminiModelId( | ||
| 'models/gemini-3.1-pro-preview', | ||
| 'https://generativelanguage.googleapis.com/v1beta/openai/', | ||
| ), | ||
| ).toBe('gemini-3.1-pro-preview'); | ||
| }); | ||
|
|
||
| it('leaves non-Gemini model ids untouched', () => { | ||
| expect(normalizeGeminiModelId('gpt-4', 'https://api.openai.com/v1')).toBe('gpt-4'); | ||
| }); | ||
|
|
||
| it('does not strip models/ prefix when baseUrl is not a Gemini host', () => { | ||
| expect(normalizeGeminiModelId('models/foo', 'https://api.openai.com/v1')).toBe('models/foo'); | ||
| }); | ||
|
|
||
| it('is a no-op when baseUrl is undefined', () => { | ||
| expect(normalizeGeminiModelId('models/gemini-2-pro', undefined)).toBe('models/gemini-2-pro'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * Google's OpenAI-compatible endpoint | ||
| * (https://generativelanguage.googleapis.com/v1beta/openai/) accepts the same | ||
| * request shape as OpenAI Chat Completions but rejects model ids carrying the | ||
| * `models/` prefix that its own /models listing returns. Settings UI keeps the | ||
| * prefixed id (so it matches the /models response), and we strip it only on | ||
| * the wire. See issue #175. | ||
| */ | ||
|
|
||
| export function isGeminiOpenAICompat(baseUrl: string | undefined): boolean { | ||
| if (!baseUrl) return false; | ||
| try { | ||
| const { hostname } = new URL(baseUrl); | ||
| return ( | ||
| hostname === 'generativelanguage.googleapis.com' || | ||
| hostname.endsWith('.generativelanguage.googleapis.com') | ||
| ); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export function normalizeGeminiModelId(modelId: string, baseUrl: string | undefined): string { | ||
| if (!isGeminiOpenAICompat(baseUrl)) return modelId; | ||
| return modelId.replace(/^models\//, ''); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Major] This matcher currently keys off hostname only, so non-OpenAI-compat URLs on the same host (for example
/v1beta/models) still triggermodels/stripping. That can misroute model lookup and synthesize the wrong id.Suggested fix: