Skip to content

fix(search): batch missing-embedding calls to avoid API batch-size limits#1542

Closed
octo-patch wants to merge 1 commit into
MemTensor:mainfrom
octo-patch:fix/issue-1482-embed-batch-missing-documents
Closed

fix(search): batch missing-embedding calls to avoid API batch-size limits#1542
octo-patch wants to merge 1 commit into
MemTensor:mainfrom
octo-patch:fix/issue-1482-embed-batch-missing-documents

Conversation

@octo-patch

Copy link
Copy Markdown
Contributor

Fixes #1482

Problem

_extract_embeddings in search_handler.py collects all documents that lack a cached embedding and calls embedder.embed(all_missing) in a single shot. Providers such as Dashscope text-embedding-v4 reject or silently return None when the batch is too large (e.g. 25 documents), which then causes a TypeError when the code tries to iterate over the None result, or silently drops all missing embeddings.

The warning message before this:

[SearchHandler] MMR embedding metadata missing; will compute missing embeddings: missing_total=25

followed by a crash or silent failure in the MMR deduplication path.

Solution

Split missing_documents into chunks of _EMBED_BATCH_SIZE (16) and call embed() for each chunk, extending a combined result list. Batches that return None/empty are skipped gracefully so remaining embeddings can still be used.

_EMBED_BATCH_SIZE = 16

computed: list[list[float]] = []
for i in range(0, len(missing_documents), _EMBED_BATCH_SIZE):
    batch = missing_documents[i : i + _EMBED_BATCH_SIZE]
    batch_result = self.searcher.embedder.embed(batch)
    if batch_result:
        computed.extend(batch_result)

Testing

  • Verified with 25 missing documents: previously crashed with TypeError; now completes successfully using 2 batches of 16 and 9.
  • Verified with <16 missing documents: behaviour unchanged (single call).

…mits

When _extract_embeddings encounters many documents without cached
embeddings it previously called embedder.embed(all_missing) in one shot.
Providers like Dashscope text-embedding-v4 reject or silently return None
for large batches (e.g. 25 documents), causing a TypeError / empty result
downstream in the MMR deduplication path.

Fix: split missing_documents into chunks of _EMBED_BATCH_SIZE (16) and
accumulate results, skipping any batch that returns None/empty so the
rest of the embeddings can still be used.

Fixes MemTensor#1482

Co-Authored-By: Octopus <liyuan851277048@icloud.com>
@Memtensor-AI Memtensor-AI changed the base branch from main to dev-20260604-v2.0.19 June 10, 2026 15:42
@Memtensor-AI Memtensor-AI changed the base branch from dev-20260604-v2.0.19 to dev-v2.0.22 July 1, 2026 13:17
@Memtensor-AI

Copy link
Copy Markdown
Collaborator

Automated Test Results: PASSED

Cloud test-engine rerun against dev-v2.0.22 completed successfully after migrating this run from the old 10010 queue to the updated cloud test-engine.

  • Run: tr-99634e82-d49 on cloud test-engine 10011
  • Scope: memos_python_core/changed-python-source
  • Result: 2 passed, 0 failed, 0 skipped

Manual code review is still required before merge.

@CarltonXiang CarltonXiang deleted the branch MemTensor:main July 3, 2026 07:25
@syzsunshine219 syzsunshine219 reopened this Jul 3, 2026
@syzsunshine219 syzsunshine219 changed the base branch from dev-v2.0.22 to main July 3, 2026 08:24
@Memtensor-AI

Copy link
Copy Markdown
Collaborator

Thanks for the contribution. This PR addresses the same issue as #2041 (Fixes #1482).

We are going to continue the review in #2041 because it uses a smaller batch size, preserves per-batch index mapping, and includes a regression test for the missing-embedding batching behavior.

To keep the review queue focused, I’m closing this PR as a duplicate of #2041. We can carry any useful hardening ideas from this PR into the #2041 review if needed.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Extracting too many missing_documents embeddings cause embedder error

4 participants