fix: normalize openai embedding responses#6919
fix: normalize openai embedding responses#69191zzxy1 wants to merge 3 commits intoAstrBotDevs:masterfrom
Conversation
Summary of ChangesHello, 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 significantly enhances the robustness and flexibility of the OpenAI embedding source by standardizing how it processes responses from the OpenAI API. It ensures that the system can gracefully handle various data formats, from native SDK objects to raw JSON strings, and consistently output float vectors, thereby improving reliability and reducing potential integration issues. The addition of dedicated tests provides strong assurance of the new logic's correctness. Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new _normalize_embedding_response method in the OpenAIEmbeddingProvider to robustly handle various response formats (SDK objects, JSON strings) from the OpenAI embedding API. This method ensures proper extraction and type conversion of embedding vectors, with new tests added to validate its behavior, including handling of empty vectors. The review suggests adding null checks within _normalize_embedding_response to prevent AttributeError if the response object or individual item objects within the response's data list are None.
| data = response.get("data") if isinstance(response, dict) else getattr( | ||
| response, "data", None | ||
| ) |
There was a problem hiding this comment.
The current implementation can raise an AttributeError if response is None, because getattr(None, "data", None) raises an exception. Since the function signature accepts Any, it should be robust against None inputs. You can make this safer by first checking if response is truthy before attempting to access its attributes.
data = response and (response.get("data") if isinstance(response, dict) else getattr(response, "data", None))| embedding = item.get("embedding") if isinstance(item, dict) else getattr( | ||
| item, "embedding", None | ||
| ) |
There was a problem hiding this comment.
Similar to the logic for retrieving data from the response, this part can raise an AttributeError if an item in the data list is None. To prevent this, you should add a check to ensure item is not None before attempting to access its attributes or keys.
embedding = item and (item.get("embedding") if isinstance(item, dict) else getattr(item, "embedding", None))
Summary
Closes #6887
Testing
Summary by Sourcery
Normalize OpenAI embedding provider responses to consistently return float vectors across different response formats and add regression coverage.
Bug Fixes:
Tests: