-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: add run_async to CacheChecker #11271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
julian-risch
merged 6 commits into
deepset-ai:main
from
Aftabbs:feat/cache-checker-run-async
May 8, 2026
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bf45e04
feat: add run_async to CacheChecker
Aftabbs e5ae120
fix: remove unused import and add release note
Aftabbs 3aa348a
Fix release note inline code formatting
julian-risch 93fd902
Add async support guard to CacheChecker
julian-risch efd49f4
Restore type-ignore rationale comment in CacheChecker
julian-risch 523cd5d
fix: remove unused type: ignore comments flagged by mypy
julian-risch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
releasenotes/notes/add-run-async-for-CacheChecker-a42fa8062c33466b.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| features: | ||
| - | | ||
| Add ``run_async`` to ``CacheChecker``, enabling it to be used in ``AsyncPipeline`` without blocking the event loop. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai> | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from unittest.mock import AsyncMock, MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from haystack import Document | ||
| from haystack.components.caching.cache_checker import CacheChecker | ||
|
|
||
|
|
||
| class TestCacheCheckerAsync: | ||
| @pytest.mark.asyncio | ||
| async def test_run_async(self, in_memory_doc_store): | ||
| documents = [ | ||
| Document(content="doc1", meta={"url": "https://example.com/1"}), | ||
| Document(content="doc2", meta={"url": "https://example.com/2"}), | ||
| Document(content="doc3", meta={"url": "https://example.com/1"}), | ||
| Document(content="doc4", meta={"url": "https://example.com/2"}), | ||
| ] | ||
| in_memory_doc_store.write_documents(documents) | ||
| checker = CacheChecker(in_memory_doc_store, cache_field="url") | ||
| results = await checker.run_async(items=["https://example.com/1", "https://example.com/5"]) | ||
| assert results == {"hits": [documents[0], documents[2]], "misses": ["https://example.com/5"]} | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_run_async_all_hits(self, in_memory_doc_store): | ||
| documents = [ | ||
| Document(content="doc1", meta={"url": "https://example.com/1"}), | ||
| Document(content="doc2", meta={"url": "https://example.com/2"}), | ||
| ] | ||
| in_memory_doc_store.write_documents(documents) | ||
| checker = CacheChecker(in_memory_doc_store, cache_field="url") | ||
| results = await checker.run_async(items=["https://example.com/1", "https://example.com/2"]) | ||
| assert results["hits"] == documents | ||
| assert results["misses"] == [] | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_run_async_all_misses(self, in_memory_doc_store): | ||
| checker = CacheChecker(in_memory_doc_store, cache_field="url") | ||
| results = await checker.run_async(items=["https://example.com/1", "https://example.com/2"]) | ||
| assert results["hits"] == [] | ||
| assert results["misses"] == ["https://example.com/1", "https://example.com/2"] | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_run_async_filters_syntax(self): | ||
| mock_store = MagicMock() | ||
| mock_store.filter_documents_async = AsyncMock(return_value=[]) | ||
| checker = CacheChecker(document_store=mock_store, cache_field="url") | ||
| await checker.run_async(items=["https://example.com/1"]) | ||
| expected_filters = {"field": "url", "operator": "==", "value": "https://example.com/1"} | ||
| mock_store.filter_documents_async.assert_awaited_once_with(filters=expected_filters) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are similarly simply assuming in other parts of the code base that there is an implementation of
filter_documents_async, for example here:haystack/haystack/components/retrievers/sentence_window_retriever.py
Line 302 in 9aedf88
In other parts of the code base (DocumentWriter), we raise an error:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative would be to use a fallback:
If document_store has callable filter_documents_async: await it.
Else: await asyncio.to_thread(document_store.filter_documents, filters=filters).