Skip to content

Customized API endpoint might returns "usage": null rather than {"p…#482

Open
pillsilly wants to merge 1 commit intoAsyncFuncAI:mainfrom
pillsilly:fixno-usage
Open

Customized API endpoint might returns "usage": null rather than {"p…#482
pillsilly wants to merge 1 commit intoAsyncFuncAI:mainfrom
pillsilly:fixno-usage

Conversation

@pillsilly
Copy link

Customized API endpoint might returns "usage": null rather than {"prompt_tokens": N, "total_tokens": N}.

Add implementation to fall back as handling when "usage" field is empty.

…mpt_tokens": N, "total_tokens": N}.

Add implmenetation to fall back as handling when "usage" field is empty.
@gemini-code-assist
Copy link
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 enhances the openai_client's ability to parse embedding responses by introducing a fallback mechanism. This change addresses an issue where some API gateways might return a null value for the usage field, causing parsing failures. The new implementation ensures that even in such scenarios, the system can still extract embedding data and provide default usage metrics, significantly improving the resilience and reliability of API integrations.

Highlights

  • API Response Handling: Implemented a robust fallback mechanism within the parse_embedding_response function to gracefully handle cases where the API returns usage: null instead of a structured usage object, which can occur with certain gateway proxies.
  • Usage Data Defaulting: When the usage field is null in an API response, the fallback logic now explicitly assigns prompt_tokens and total_tokens to 0, ensuring that the Usage object is always consistently structured and preventing downstream errors.
  • Improved Error Logging: Enhanced error logging to provide more context when both the standard embedding parsing and the new fallback parsing mechanisms fail, aiding in debugging and issue resolution.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • api/openai_client.py
    • Added a new if condition within the except block of parse_embedding_response to check for response.data when standard parsing fails.
    • Implemented manual parsing of embedding data from response.data when usage is null or parsing fails.
    • Introduced logic to construct a Usage object with prompt_tokens and total_tokens defaulting to 0 if response.usage is None.
    • Added log.warning for successful fallback parsing and log.error for failures in the fallback mechanism.
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 introduces a fallback mechanism for parsing embedding responses, specifically for cases where the usage field might be null. The overall approach is sound and addresses the problem described. However, I've found a bug in the error handling logic within the new fallback path. If the fallback parsing itself fails, the system currently reports the original error, which could be confusing for debugging. I've provided a suggestion to improve the error reporting in this scenario to make it more accurate and helpful.

Comment on lines +292 to 295
except Exception as e2:
log.error(f"Fallback embedding parsing also failed: {e2}")
log.error(f"Error parsing the embedding response: {e}")
return EmbedderOutput(data=[], error=str(e), raw_response=response)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

There is an issue with the error handling here. If the fallback parsing fails and this except block for e2 is entered, the code logs e2 but then proceeds to log the original error e and returns an EmbedderOutput with error=str(e). This is misleading because the more immediate cause of failure is e2.

The returned error should reflect that the fallback mechanism also failed. I suggest modifying this block to return immediately with a more informative error message that includes details from both exceptions.

                except Exception as e2:
                    error_msg = f"Standard parsing failed ('{e}') and fallback parsing also failed ('{e2}')"
                    log.error(error_msg)
                    return EmbedderOutput(data=[], error=error_msg, raw_response=response)
            log.error(f"Error parsing the embedding response: {e}")
            return EmbedderOutput(data=[], error=str(e), raw_response=response)

@pillsilly
Copy link
Author

@sng-asyncfunc kindly help to check this PR. thanks.

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