Skip to content

fix(mem0): pass top_k to SDK in OSSBackend.get_all for accurate count - #52973

Closed
luxuguang-leo wants to merge 1 commit into
NousResearch:mainfrom
luxuguang-leo:fix/mem0-oss-getall-pagination
Closed

luxuguang-leo wants to merge 1 commit into
NousResearch:mainfrom
luxuguang-leo:fix/mem0-oss-getall-pagination

Conversation

@luxuguang-leo

Copy link
Copy Markdown
Contributor

Problem

OSSBackend.get_all() calls Memory.get_all(filters=filters) without passing top_k. The Mem0 SDK defaults top_k to 20 (source: mem0ai/mem0/memory/main.py), so the response is always truncated to at most 20 results.

Since OSSBackend computes count = len(all_results) from the raw SDK response, the reported count is wrong whenever there are more than 20 stored memories — it caps at 20.

Root Cause

In plugins/memory/mem0/_backend.py, OSSBackend.get_all():

def get_all(self, *, filters, page=1, page_size=100):
    response = self._memory.get_all(filters=filters)  # ← no top_k!
    all_results = _unwrap_results(response)
    total = len(all_results)  # ← always ≤ 20
    ...

The Mem0 SDK's Memory.get_all() signature is:

def get_all(self, *, filters=None, top_k: int = 20, show_expired=False):

The PlatformBackend does not have this problem — it uses MemoryClient.get_all(filters, page, page_size) which handles pagination server-side and returns an accurate count.

Fix

Pass top_k=10000 to self._memory.get_all() so the full result set is returned. OSSBackend already applies client-side pagination (page/page_size slicing) on the full set, so count = len(all_results) is correct again.

Testing

4 new tests in TestOSSBackendGetAll:

Test What it verifies
test_passes_top_k_to_sdk top_k=10000 is forwarded to the SDK
test_count_reflects_total_not_sdk_default 50 memories → count=50 (not 20)
test_client_side_pagination Page 2, size 10 returns items 10-19 with count=50
test_pagination_beyond_total_returns_empty Out-of-range page returns [] with correct count

All 46 mem0 tests pass.

Fixes #52921

OSSBackend.get_all() calls Memory.get_all(filters=filters) without
passing top_k. The Mem0 SDK defaults top_k to 20, so the response
is always truncated to at most 20 results. Since OSSBackend computes
count = len(all_results), the count is wrong whenever there are
more than 20 stored memories.

Fix: pass top_k=10000 to the SDK call so the full result set is
returned for accurate counting and client-side pagination.

Test: 4 new tests in TestOSSBackendGetAll:
- test_passes_top_k_to_sdk: verifies the parameter is forwarded
- test_count_reflects_total_not_sdk_default: 50 memories, count=50
- test_client_side_pagination: page=2, size=10 returns correct slice
- test_pagination_beyond_total_returns_empty: out-of-range page

Fixes: NousResearch#52921
@alt-glitch alt-glitch added type/bug Something isn't working comp/plugins Plugin system and bundled plugins tool/memory Memory tool and memory providers P3 Low — cosmetic, nice to have labels Jun 26, 2026
@luxuguang-leo

luxuguang-leo commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

Quick check-in. This PR's approach was independently confirmed today by two other contributors — #58323 (bvisible, running in production) and #58410 (nankingjing) were both triaged as duplicates of this one.

The check suites are stuck at action_required — CI needs a maintainer to approve the first run for an external fork. Is there anything else needed on this PR before that can happen? @alt-glitch

@luxuguang-leo

Copy link
Copy Markdown
Contributor Author

This PR is obsoleted by commit 9a32272 (kshitijk4poor), which pruned get_all() from the ABC and all three backends — its only caller mem0_list was already removed by an earlier recall-tuning commit.

Related:

Closing as no longer needed — the dead code path this PR fixed no longer exists on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/plugins Plugin system and bundled plugins P3 Low — cosmetic, nice to have tool/memory Memory tool and memory providers type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SelfHostedBackend.get_all() returns incorrect count when memories exceed server page limit

2 participants