fix(retrievers): handle Voyage AI rerank API response format change#6493
fix(retrievers): handle Voyage AI rerank API response format change#6493corporatepoetry wants to merge 1 commit into
Conversation
The Voyage AI rerank API may return results under `data.data` instead of `data.results`. Fall back to `data.results` for backward compatibility.
There was a problem hiding this comment.
Code Review
This pull request updates the Voyage AI reranking logic to support API responses that return results under either the 'data' or 'results' field. Feedback suggests adding optional chaining and verifying that the extracted results are an array, throwing an error if the format is invalid to ensure fail-fast behavior.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| const results = returnedDocs.data.data ?? returnedDocs.data.results | ||
| const finalResults: Document<Record<string, any>>[] = [] | ||
| returnedDocs.data.results.forEach((result: any) => { | ||
| results.forEach((result: any) => { |
There was a problem hiding this comment.
If the API response structure is unexpected or empty, returnedDocs.data might be nullish, or results might not be an array. When handling potentially invalid data from external sources (like an API response), we should prefer throwing an error for invalid input types rather than silently returning a default or empty value to promote fail-fast behavior.
| const results = returnedDocs.data.data ?? returnedDocs.data.results | |
| const finalResults: Document<Record<string, any>>[] = [] | |
| returnedDocs.data.results.forEach((result: any) => { | |
| results.forEach((result: any) => { | |
| const results = returnedDocs.data?.data ?? returnedDocs.data?.results | |
| if (!Array.isArray(results)) { | |
| throw new Error("Invalid response format from Voyage AI API: results is not an array") | |
| } | |
| const finalResults: Document<Record<string, any>>[] = [] | |
| results.forEach((result: any) => { |
References
- When handling potentially invalid data from external sources (like an API response), prefer throwing an error for invalid input types rather than silently returning a default or empty value. This promotes fail-fast behavior.
The Voyage AI rerank API may return results under
data.datainstead ofdata.results. Fall back todata.resultsfor backward compatibility.Fixes #6492