Skip to content

fix(rerank): honor rank_by in VoyageAIReranker - #864

Merged
vishal-bala merged 2 commits into
redis:mainfrom
pcbeingused333:fix/voyageai-reranker-rank-by
Nov 25, 2026
Merged

vishal-bala merged 2 commits into
redis:mainfrom
pcbeingused333:fix/voyageai-reranker-rank-by

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

VoyageAIReranker takes rank_by (documented in its constructor and inherited from BaseReranker), but _preprocess never reads it. Dict documents without a content key, such as Redis search results with a description field, are sent to Voyage as str(doc): every key, the braces and fields like vector_distance all go into the rerank text, whatever rank_by says. CohereReranker does use rank_by.

reranker = VoyageAIReranker(model="rerank-2", rank_by=["text"])
reranker._preprocess("q", [{"text": "alpha", "id": 1}])[0]["documents"]
# before: ["{'text': 'alpha', 'id': 1}"]   after: ["alpha"]

Changes

VoyageAIReranker._preprocess reads rank_by

When rank_by is set (in the constructor or as a per-call kwarg, as in CohereReranker) and every doc is a dict, the text sent to Voyage is the rank_by fields joined by a space. A doc that lacks one of the fields raises ValueError naming the missing fields. Without rank_by, behavior is unchanged: the content key if every doc has one, otherwise str(doc). rank and arank share _preprocess, so both are covered.

  • New tests/unit/test_voyageai_reranker_rank_by.py, with the voyageai module mocked (no network): single field, several fields plus per-call override, missing field, default content path.

Release Notes

VoyageAIReranker now ranks dict documents by the fields given in rank_by instead of the whole stringified document. A document missing one of the rank_by fields now raises ValueError.

AI Assistance

  • No AI assistance was used for this PR.
  • This PR is AI-assisted. I have reviewed and understood everything in it, and I take responsibility for it.

Claude Code (claude-sonnet-5-5): implementation, tests and description; reviewed by Claude Code (claude-opus-5-5).

Reviewer Brief

Start here: redisvl/utils/rerank/voyageai.py, _preprocess.

Where it could fail: callers that set rank_by but pass dicts missing those fields used to get the whole str(doc) reranked silently; they now get a ValueError. Lists that mix dicts and strings skip rank_by and keep the old str(doc) path.

Not verified: a live Voyage /rerank call; the tests mock the client.

Beyond CI: with voyageai.py restored to main, 3 of the 4 new tests fail (the content default test passes on both); with the fix all 4 pass.

Reviewed commit: 8b675e0 (Claude Code, claude-opus-5-5).

🤖 Generated with Claude Code


Note

Medium Risk
Callers that set rank_by but pass incomplete dicts will now get ValueError instead of silently reranking stringified documents; this fixes ranking quality for Redis-style search hits.

Overview
VoyageAIReranker._preprocess now applies rank_by (constructor or per-call) when all documents are dicts: rerank text is the selected fields space-joined, matching CohereReranker semantics instead of sending str(doc) with metadata noise.

Dicts missing any rank_by field raise ValueError with the missing keys. With no rank_by, behavior is unchanged: use content when every dict has it, otherwise str(doc).

Unit tests mock the voyageai client and cover single/multi-field selection, override, validation, and the default content path.

Reviewed by Cursor Bugbot for commit 8d81adc. Bugbot is set up for automated code reviews on this repo. Configure here.

rank_by was accepted but never read, so dict docs without a 'content' key
were stringified wholesale. Build the rerank text from the rank_by fields
(per-call override supported) and raise if a field is missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread redisvl/utils/rerank/voyageai.py Resolved
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks @vishal-bala, restored the per-document isinstance(doc, dict) check in voyageai.py, so mixed lists of dicts and strings are handled per item. The new tests pass (4/4) and mypy is clean on the file.

This comment was drafted with Claude Code.

@vishal-bala vishal-bala added the auto:patch Increment the patch version when merged label Nov 24, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 80.82%. Comparing base (45a5df4) to head (8d81adc).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #864      +/-   ##
==========================================
+ Coverage   80.15%   80.82%   +0.66%     
==========================================
  Files         121      121              
  Lines       14867    15708     +841     
  Branches     2602     2813     +211     
==========================================
+ Hits        11917    12696     +779     
- Misses       2228     2267      +39     
- Partials      722      745      +23     
Flag Coverage Δ
core 79.19% <90.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vishal-bala
vishal-bala merged commit a595992 into redis:main Nov 25, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto:patch Increment the patch version when merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants