Skip to content

feat: add async methods for FalkorDBDocumentStore - #3999

Merged
bogdankostic merged 9 commits into
deepset-ai:mainfrom
maxdswain:falkordb-async
Oct 5, 2026
Merged

bogdankostic merged 9 commits into
deepset-ai:mainfrom
maxdswain:falkordb-async

Conversation

@maxdswain

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

Add async equivalent methods to FalkorDBDocumentStore using falkordb's Python client async client.

How did you test it?

Added unit and integration tests, mostly coming from the document store mixin tests.

Notes for the reviewer

Checklist

@maxdswain
maxdswain requested a review from a team as a code owner September 24, 2026 17:12
@maxdswain
maxdswain requested review from bogdankostic and removed request for a team September 24, 2026 17:12
@github-actions github-actions Bot added integration:falkordb type:documentation Improvements or additions to documentation labels Sep 24, 2026
@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report (falkordb)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/falkordb/src/haystack_integrations/components/retrievers/falkordb
  cypher_retriever.py
  embedding_retriever.py
  integrations/falkordb/src/haystack_integrations/document_stores/falkordb
  document_store.py 143, 279, 337-344, 353-361, 666-667, 1064-1065, 1338-1339
Project Total  

This report was generated by python-coverage-comment-action

@bogdankostic bogdankostic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @maxdswain! I had a first pass and found mostly small things, but one needs fixing before we merge: with recreate_graph=True, whichever of the sync and async paths warms up second drops the graph the other one populated, so data written through one is silently lost.

Also, let's try to make sure that unit test coverage stays high.

Comment on lines +290 to +292
if self.recreate_graph:
try:
await async_client.select_graph(self.graph_name).delete()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recreate_graph=True silently wipes data once both paths warm up.

warm_up() and warm_up_async() each honour self.recreate_graph independently, so whichever warms up second drops the graph the first one populated.

store.write_documents([Document(content="hello")])
store.count_documents()                 # -> 1
await store.count_documents_async()     # -> 0   warm_up_async() just dropped the graph
store.count_documents()                 # -> 0   data is gone

Comment on lines +347 to +351
self.warm_up()
graph = self.graph
if graph is None:
msg = "The synchronous FalkorDB graph is unavailable after warm-up."
raise RuntimeError(msg)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is repeated throughout multiple methods. Let's move this to a separate method to reduce code duplication.

Also, I'm not sure if graph ever can be None in these cases after calling self.warm_up, so if the RuntieError is reachable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right that this code isn't reachable due to the self.warm_up method, but the type checker doesn't know that. It is a lot of duplicate code so I looked at other integrations and assert graph is not None # noqa: S101 is used instead so I've switched to that to make the type checker happy.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this throughout the file? Now there's a mix between assert graph is not None and the RuntimeError.

Also, the local graph variable isn't needed, mypy narrows self.graph directly after the assert:

self.warm_up()
assert self.graph is not None  # noqa: S101
result = self.graph.query(...)

This should also bring back most of the unit test coverage, as 48 of the 92 uncovered statements in document_store.py are the unreachable msg/raise lines of the RuntimeError checks.

Comment on lines +807 to +811
await self.warm_up_async()
graph = self.async_graph
if graph is None:
msg = "The asynchronous FalkorDB graph is unavailable after warm-up."
raise RuntimeError(msg)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here regarding duplicate code, let's move this to a separate method to be used by the async methods.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by reply to your other comment.

assert float_values == [1.5] and type(float_values[0]) is float
assert bool_values == [True] and type(bool_values[0]) is bool

def test_close_and_reopen(self, document_store):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close() resets initialized, so this warm_up() runs the recreate_graph delete from the fixture a second time:

store.write_documents([Document(content="hello")])
store.count_documents()   # 1
store.close()
store.warm_up()
store.count_documents()   # 0  <- graph dropped again

The test only passes because the store is empty at this point. Once the shared-flag fix is in, let's write a document before close() and assert it survives the reopen.

# Internal retrieval helpers (called by retriever components)
# ------------------------------------------------------------------

def _embedding_retrieval(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding _embedding_retrieval_async and _cypher_retrieval_async in this PR and making use of them in the retrievers through run_async? Or should this rather be tackled in a separate PR?

row = result.result_set[0]
return {"min": row[0], "max": row[1]}

async def get_metadata_field_unique_values_async(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sync get_metadata_field_unique_values documents that Cypher's DISTINCT collapses a whole-number float into an equal int.
Let's carry that note over here.

Same for write_documents_async (flat meta layout, NONE treated as FAIL), and _handle_duplicate_documents_async / _write_batch_async / delete_all_documents_async whose doc strings are much shorter compared to their sync counterparts which document params.

rows = result.result_set
return int(rows[0][0]) if rows else 0

async def filter_documents_async(self, filters: dict[str, Any] | None = None) -> list[Document]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the link to the metadata filtering docs that the sync filter_documents has in its :param filters:. Also, this one correctly says :raises FilterError: — the sync one still says :raises ValueError:, could we fix that while we're here?

Comment on lines +107 to +116
def assert_documents_are_equal(received: list[Document], expected: list[Document]) -> None:
"""Compare documents while allowing for FalkorDB's float32 embeddings."""
assert len(received) == len(expected), f"Expected {len(expected)} documents but got {len(received)}"
received_sorted = sorted(received, key=lambda document: document.id)
expected_sorted = sorted(expected, key=lambda document: document.id)
for received_document, expected_document in zip(received_sorted, expected_sorted, strict=True):
assert received_document.id == expected_document.id
assert received_document.content == expected_document.content
assert received_document.meta == expected_document.meta
assert (received_document.embedding is None) == (expected_document.embedding is None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a copy of assert_documents_are_equal in test_document_store.py. Let's move it to a shared helper so the two can't drift.

@maxdswain

Copy link
Copy Markdown
Contributor Author

Thanks for the review @bogdankostic, I should have now addressed all of your comments.

@bogdankostic bogdankostic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing my comments @maxdswain! This PR is almost good to go, just a few minor things before we can ship it.

Comment on lines +347 to +351
self.warm_up()
graph = self.graph
if graph is None:
msg = "The synchronous FalkorDB graph is unavailable after warm-up."
raise RuntimeError(msg)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this throughout the file? Now there's a mix between assert graph is not None and the RuntimeError.

Also, the local graph variable isn't needed, mypy narrows self.graph directly after the assert:

self.warm_up()
assert self.graph is not None  # noqa: S101
result = self.graph.query(...)

This should also bring back most of the unit test coverage, as 48 of the 92 uncovered statements in document_store.py are the unreachable msg/raise lines of the RuntimeError checks.


def _handle_duplicate_documents(
self,
graph: Graph,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, _handle_duplicate_documents and _write_batch didn't take a graph parameter. Let's remove it again and instead do assert self.graph is not None inside these methods, so their signatures stay the same as on main. Same for _handle_duplicate_documents_async and _write_batch_async with self.async_graph.

if self.recreate_graph:
if self.recreate_graph and not self._recreate_graph_applied:
# Recreation is shared by the sync and async connection lifecycles.
self._recreate_graph_applied = True

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flag is set before the delete, and any error during the delete is only logged at debug level. So if the first warm-up fails, e.g. because the server isn't reachable yet, the graph is never recreated on retry and we keep the old data. Should we only catch the ResponseError raised when the graph doesn't exist yet, and reset the flag on any other error? Same in warm_up_async.

@maxdswain

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing once again @bogdankostic, should have now addressed all of your comments.

@bogdankostic bogdankostic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing all my comments @maxdswain! I pushed a small fix on top: when several async calls hit a fresh store at the same time, each one created its own client, and with recreate_graph=True one of them could query the graph while another was still deleting it. warm_up_async() is now guarded by an asyncio.Lock

@bogdankostic
bogdankostic merged commit de6b613 into deepset-ai:main Oct 5, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration:falkordb type:documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add _async methods for FalkorDB Integration

2 participants