A Haystack document store is the boundary between prepared Document objects and later retrieval. Direct calls to write_documents() suit application code that already owns the store and does not need a DocumentWriter pipeline component.
Stable document IDs make the stored batch predictable across runs. DuplicatePolicy.FAIL protects those IDs by raising an error instead of silently replacing an existing document with different content.
An InMemoryDocumentStore keeps the storage run local and needs no database service. Calling filter_documents() after the write reconstructs each document's content and section metadata, allowing the stored state to be compared with the original batch rather than trusting only the returned write count.
$ source .venv/bin/activate
The project may use a virtual-environment path other than .venv.
Related: How to install Haystack with pip
from haystack import Document from haystack.document_stores.in_memory import InMemoryDocumentStore from haystack.document_stores.types import DuplicatePolicy documents = [ Document( id="handbook-api", content="Use the client library to submit service requests.", meta={"section": "API"}, ), Document( id="handbook-cli", content="Use the command line for scheduled maintenance.", meta={"section": "CLI"}, ), ]
expected_documents = { document.id: { "content": document.content, "section": document.meta["section"], } for document in documents }
document_store = InMemoryDocumentStore() written_count = document_store.write_documents( documents=documents, policy=DuplicatePolicy.FAIL, )
The direct method returns the number written, while DuplicatePolicy.FAIL stops if either explicit ID already exists.
Related: How to set duplicate document policy in Haystack
stored_documents = { document.id: { "content": document.content, "section": document.meta["section"], } for document in document_store.filter_documents() }
Reading through the store protocol checks the retained records instead of reusing the original Document list.
Related: How to filter documents in a Haystack document store
assert written_count == len(documents) assert stored_documents == expected_documents print(f"write_documents returned: {written_count}") print("verified stored documents:") for document_id in sorted(stored_documents): stored = stored_documents[document_id] print(f"- {document_id} [{stored['section']}]: {stored['content']}")
The second assertion fails when an ID, body, section value, or stored record differs from the requested batch.
Related: How to delete documents from a Haystack document store
from haystack import Document from haystack.document_stores.in_memory import InMemoryDocumentStore from haystack.document_stores.types import DuplicatePolicy documents = [ Document( id="handbook-api", content="Use the client library to submit service requests.", meta={"section": "API"}, ), Document( id="handbook-cli", content="Use the command line for scheduled maintenance.", meta={"section": "CLI"}, ), ] expected_documents = { document.id: { "content": document.content, "section": document.meta["section"], } for document in documents } document_store = InMemoryDocumentStore() written_count = document_store.write_documents( documents=documents, policy=DuplicatePolicy.FAIL, ) stored_documents = { document.id: { "content": document.content, "section": document.meta["section"], } for document in document_store.filter_documents() } assert written_count == len(documents) assert stored_documents == expected_documents print(f"write_documents returned: {written_count}") print("verified stored documents:") for document_id in sorted(stored_documents): stored = stored_documents[document_id] print(f"- {document_id} [{stored['section']}]: {stored['content']}")
$ python document_store_write_demo.py write_documents returned: 2 verified stored documents: - handbook-api [API]: Use the client library to submit service requests. - handbook-cli [CLI]: Use the command line for scheduled maintenance.