fastembed icon indicating copy to clipboard operation
fastembed copied to clipboard

[Bug]: SparseTextEmbedding needs to implement aembed for non-blocking use (async/await)

Open sglebs opened this issue 4 months ago • 2 comments

What happened?

I want to be able to use non-blocking embedding, via "aembed" (async/await). This API is missing in this class, but it is part of the expected contract.

What is the expected behaviour?

When an AsyncQdrantClient is used, aembed can be used (async/await) as well. I am talking about https://github.com/langchain-ai/langchain/issues/32195 and testing the patch https://github.com/langchain-ai/langchain/pull/32196/files#r2226270515

I tried a hybrid search and, fair enough, "aembed" works fine with the dense variation but not with the sparse one (fast-sparse-bm25)

A minimal reproducible example

from fastembed.sparse.sparse_text_embedding import SparseTextEmbedding

embedding = SparseTextEmbedding('Qdrant/bm25')
embedding.aembed_query("foo bar")
    embedding.aembed_query("foo bar")
    ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'SparseTextEmbedding' object has no attribute 'aembed_query'

What Python version are you on? e.g. python --version

Python 3.11.10

FastEmbed version

fastembed==0.7.1

What os are you seeing the problem on?

MacOS

Relevant stack traces and/or logs


sglebs avatar Jul 24 '25 16:07 sglebs

Monkey patching can be used as a workaround:

# AttributeError: 'SparseTextEmbedding' object has no attribute 'aembed_query'
SparseTextEmbedding.aembed_query = aembed_query_monkey_patched

async def aembed_query_monkey_patched(self, text: str) -> List[float]:
    """Asynchronous Embed query text.

    Args:
        text: Text to embed.

    Returns:
        Embedding.
    """
    return await run_in_executor(None, self.embed_query, text)

sglebs avatar Jul 24 '25 17:07 sglebs

Hi @sglebs

Sorry for the late response It's not likely that we'll add this functionality straight into fastembed, since fastembed calls are blocking by nature, however, it probably might be added into langchain integration, if dense models already support this interface there cc: @Anush008

joein avatar Aug 06 '25 20:08 joein