Skip to content

Fix Google Gemini OpenAI-compatible API integration #391

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

Open
bhouston opened this issue Mar 30, 2025 · 2 comments
Open

Fix Google Gemini OpenAI-compatible API integration #391

bhouston opened this issue Mar 30, 2025 · 2 comments

Comments

@bhouston
Copy link
Member

Issue Summary

After investigating issue #390 regarding the Google Gemini API integration with mycoder, I've identified potential causes for the error.

Findings

  1. The current implementation in mycoder supports using the OpenAI provider with alternative APIs through the baseUrl configuration, as evidenced by the xai provider which uses the OpenAIProvider.

  2. The error in issue Not Working with Google Gemini API #390 occurs when trying to use Google's Gemini API with the OpenAI-compatible endpoint:

export default {
    provider: 'openai',
    model: 'gemini-2.5-pro-exp-03-25',
    baseUrl: 'https://generativelanguage.googleapis.com/v1beta/openai',
    logLevel: 'verbose',
    githubMode: false,
    headless: true
};
  1. The error message indicates a 400 status code (bad request) with no response body:
Error: Error calling OpenAI API: 400 status code (no body)

Potential Causes

  1. Authentication Issues: The code is likely not passing the API key in the correct format for Google's API. Google Gemini API might require an API key to be passed differently than OpenAI.

  2. Request Format Incompatibility: While Google advertises OpenAI compatibility, there might be subtle differences in the request format or required parameters.

  3. Endpoint URL Structure: The endpoint URL might need to be structured differently, possibly requiring additional path components or query parameters.

Recommended Next Steps

  1. Review Google's Gemini API documentation for their OpenAI-compatible endpoint to understand authentication requirements.

  2. Add explicit support for Google Gemini in the provider registry, similar to how xai is implemented but with any Gemini-specific adjustments.

  3. Consider adding debug logging to capture the exact request being sent and any response headers to better diagnose the issue.

  4. Create a test case specifically for the Google Gemini OpenAI-compatible API to verify the integration.

I'll continue investigating the specific requirements for Google's OpenAI-compatible API and provide further updates.

@bhouston
Copy link
Member Author

Additional Findings on Google Gemini API OpenAI Compatibility

After further investigation, I've found some key insights about the Google Gemini API's OpenAI compatibility:

  1. The base URL structure used in the issue configuration appears to be problematic:
baseUrl: 'https://generativelanguage.googleapis.com/v1beta/openai'
  1. Based on the pattern used for other providers like xai in the mycoder codebase, the OpenAI provider is designed to work with alternative APIs that implement the OpenAI-compatible interface, but there are likely specific requirements for how the API key is passed and how the URL is structured.

  2. Looking at how API keys are handled in the code, there might be an issue with how the authentication is being passed to Google's API. The OpenAI provider expects the API key to be passed in a specific format, while Google's API might require a different authentication method.

Recommended Solution

  1. Create a dedicated provider entry for Google Gemini in the providerConfig object in provider.ts:
gemini: {
  keyName: 'GOOGLE_API_KEY',
  docsUrl: 'https://ai.google.dev/docs',
  baseUrl: 'https://generativelanguage.googleapis.com/v1beta/openai',
  model: 'gemini-2.5-pro-exp-03-25',
  factory: (model, options) => new OpenAIProvider(model, options),
}
  1. Modify the OpenAI provider to handle Google Gemini's specific authentication requirements, which might include:

    • Adding an API key query parameter to the URL instead of using an Authorization header
    • Adjusting the request format to match Google's expectations
  2. Add debug logging to capture the exact request and response details to diagnose the issue more precisely.

I'll continue to look for more specific documentation on Google's OpenAI-compatible API to provide more concrete recommendations.

@Sreenivas1323
Copy link

Sreenivas1323 commented Apr 15, 2025

Any further discussion on this @bhouston ? Really wanted to try something like mycoder with gemini 2.5 pro!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants