SIGN IN SIGN UP

INTPYTHON-966 Add $rerank support to langgraph.store.mongodb.MongoDBStore (#397)

<!--
We do not accept pull requests that are primarily or substantially
generated by AI tools (ChatGPT, Copilot, etc.).
All contributions must be written and understood by human contributors.
-->
[JIRA TICKET](https://jira.mongodb.org/browse/INTPYTHON-966)

## Summary

<!-- What is this PR introducing? If context is already provided from
the JIRA ticket, still place it in the Pull Request as you should not
make the reviewer do digging for a basic summary. -->

Adds support new MQL function $rerank to use VoyageAI models to rerank
search results.

## Changes in this PR

<!-- What changes did you make to the code? What new APIs (public or
private) were added, removed, or edited to generate the desired outcome
explained in the above summary? -->

Adds RerankConfig TypedDict and a rerank_config parameter to
MongoDBStore. When set, search() expands the $vectorSearch top_k to
num_docs_to_rerank so the reranker has a full candidate pool, then
appends $rerank and $limit limit.

$rerank only resolves top-level field paths, so value.<index_field> is
temporarily surfaced as a top-level field via $addFields before the
stage and removed with $unset afterward.

Requires MongoDB Atlas 8.3+, Native Reranking enabled in Atlas Project
Settings, and a Voyage AI API key configured in Atlas. Runs entirely
server-side — no Voyage AI client dependency needed.

Adds integration tests with the adversarial negation corpus from
INTPYTHON-964: French toast ranks above club sandwich under vector
search (negation blindness); the reranker corrects this.
## Test Plan

<!-- How did you test the code? If you added unit tests, you can say
that. If you didn’t introduce unit tests, explain why. All code should
be tested in some way – so please list what your validation strategy
was. -->

Unit and Integration tests have been added that check structure and
confirm realistic rerank performance to use as example usage.

## Checklist

<!-- Do not delete the items provided on this checklist -->

### Checklist for Author

- [X] Did you update the changelog (if necessary)?
- [X] Is the intention of the code captured in relevant tests?
- [ ] If there are new TODOs, has a related JIRA ticket been created?
- [x] Has a MongoDB Employee run [the patch build of this
PR](https://github.com/mongodb-labs/ai-ml-pipeline-testing?tab=readme-ov-file#running-a-patch-build-of-a-given-pr)?

### Checklist for Reviewer

- [ ] Does the title of the PR reference a JIRA Ticket?
- [ ] Do you fully understand the implementation? (Would you be
comfortable explaining how this code works to someone else?)
- [ ] Is all relevant documentation (README or docstring) updated?

---------

Signed-off-by: Casey Clements <casey.clements@mongodb.com>
C
Casey Clements committed
94a4a37a936fc36c723db9b435633f85e0e909be
Parent: c674fab
Committed by GitHub <noreply@github.com> on 6/17/2026, 1:08:08 AM