Skip to content

Conversation

@WillemMomma
Copy link

Description

Fixes authentication errors when using OpenAI-compatible API providers/routers like LiteLLM. The Authorization header was incorrectly sending a dictionary representation instead of the API key string.

Issue: In openai_user.py, get_credentials() returns {"api_key": "sk-..."} but was used directly in an f-string, resulting in:

Authorization: Bearer {'api_key': 'sk-...'}

instead of:

Authorization: Bearer sk-...

Fix: Use auth_provider.get_headers() which properly formats the Authorization header.

PR Type / Label

/kind bug

Related Issue

N/A - Bug discovered during manual testing with LiteLLM endpoint

Changes

  • Modified openai_user.py: Use get_headers() instead of manually constructing Authorization header
  • Updated test_openai_user.py: Added get_headers() mock to test fixture

Correctness Tests

  • Updated existing unit tests in tests/user/test_openai_user.py
  • Manually verified authentication works with LiteLLM endpoint

Checklist

  • I have rebased my branch on top of the latest main branch (git pull origin main)
  • I have run make check to ensure code is properly formatted and passes all lint checks
  • I have run make test to verify test coverage (~90% required)
  • I have added tests that fail without my code changes (for bug fixes)
  • I have added tests covering variants of new features (for new features)

Additional Information

This fix ensures compatibility with any OpenAI-compatible API endpoint, not just the official OpenAI API.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @WillemMomma, 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 addresses a critical bug that prevented proper authentication with OpenAI-compatible API providers due to an incorrectly formatted Authorization header. By standardizing the header generation process, the change ensures seamless integration and resolves authentication failures, thereby broadening the system's compatibility and reliability across different API endpoints.

Highlights

  • Authentication Fix: Resolved a bug where the OpenAI authentication header was incorrectly formatted, causing authentication errors with OpenAI-compatible API providers like LiteLLM.
  • Header Generation: The Authorization header in openai_user.py now correctly uses auth_provider.get_headers() to format the API key, instead of an f-string that produced a dictionary representation.
  • Test Updates: Updated test_openai_user.py to include a mock for get_headers() in the test fixture, ensuring proper testing of the new authentication mechanism.
  • Compatibility: This fix enhances compatibility with various OpenAI-compatible API endpoints, not just the official OpenAI API.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

Copy link
Contributor

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

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 correctly fixes a bug in the OpenAI authentication header formatting by delegating header creation to the ModelAuthProvider. This is a good architectural improvement. The associated test changes are also appropriate, ensuring the fix is covered. I have one suggestion to improve the robustness of the header merging logic in openai_user.py to prevent potential conflicts with headers provided by the authentication provider.

Comment on lines 44 to 47
self.headers = {
"Authorization": f"Bearer {self.auth_provider.get_credentials()}",
**auth_headers,
"Content-Type": "application/json",
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While this change correctly fixes the Authorization header issue, the current dictionary unpacking order will always override the Content-Type header from the authentication provider. This could cause issues if an auth provider requires a specific Content-Type.

To make this more robust and give the ModelAuthProvider full control over headers, consider setting Content-Type as a default that can be overridden by the provider. This aligns better with the goal of delegating header management.

Suggested change
self.headers = {
"Authorization": f"Bearer {self.auth_provider.get_credentials()}",
**auth_headers,
"Content-Type": "application/json",
}
self.headers = {
"Content-Type": "application/json",
**auth_headers,
}

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

Successfully merging this pull request may close these issues.

1 participant