Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ export class VoyageAIRerank extends BaseDocumentCompressor {
}
try {
let returnedDocs = await axios.post(this.VOYAGEAI_RERANK_API_URL, data, config)
const results = returnedDocs.data.data ?? returnedDocs.data.results
const finalResults: Document<Record<string, any>>[] = []
returnedDocs.data.results.forEach((result: any) => {
results.forEach((result: any) => {
Comment on lines +41 to +43

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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
  1. 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.

const doc = documents[result.index]
doc.metadata.relevance_score = result.relevance_score
finalResults.push(doc)
Expand Down