Files
wehub-resource-sync c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:22:28 +08:00

3.4 KiB
Raw Permalink Blame History

title, id, description, slug
title id description slug
Document Writers document-writers-api Writes Documents to a DocumentStore. /document-writers-api

document_writer

DocumentWriter

Writes documents to a DocumentStore.

Usage example

from haystack import Document
from haystack.components.writers import DocumentWriter
from haystack.document_stores.in_memory import InMemoryDocumentStore
docs = [
    Document(content="Python is a popular programming language"),
]
doc_store = InMemoryDocumentStore()
writer = DocumentWriter(document_store=doc_store)
writer.run(docs)

init

__init__(
    document_store: DocumentStore,
    policy: DuplicatePolicy = DuplicatePolicy.NONE,
) -> None

Create a DocumentWriter component.

Parameters:

  • document_store (DocumentStore) The instance of the document store where you want to store your documents.
  • policy (DuplicatePolicy) The policy to apply when a Document with the same ID already exists in the DocumentStore.
  • DuplicatePolicy.NONE: Default policy, relies on the DocumentStore settings.
  • DuplicatePolicy.SKIP: Skips documents with the same ID and doesn't write them to the DocumentStore.
  • DuplicatePolicy.OVERWRITE: Overwrites documents with the same ID.
  • DuplicatePolicy.FAIL: Raises an error if a Document with the same ID is already in the DocumentStore.

to_dict

to_dict() -> dict[str, Any]

Serializes the component to a dictionary.

Returns:

  • dict[str, Any] Dictionary with serialized data.

from_dict

from_dict(data: dict[str, Any]) -> DocumentWriter

Deserializes the component from a dictionary.

Parameters:

  • data (dict[str, Any]) The dictionary to deserialize from.

Returns:

  • DocumentWriter The deserialized component.

Raises:

  • DeserializationError If the document store is not properly specified in the serialization data or its type cannot be imported.

run

run(
    documents: list[Document], policy: DuplicatePolicy | None = None
) -> dict[str, int]

Run the DocumentWriter on the given input data.

Parameters:

  • documents (list[Document]) A list of documents to write to the document store.
  • policy (DuplicatePolicy | None) The policy to use when encountering duplicate documents.

Returns:

  • dict[str, int] Number of documents written to the document store.

Raises:

  • ValueError If the specified document store is not found.

run_async

run_async(
    documents: list[Document], policy: DuplicatePolicy | None = None
) -> dict[str, int]

Asynchronously run the DocumentWriter on the given input data.

This is the asynchronous version of the run method. It has the same parameters and return values but can be used with await in async code.

Parameters:

  • documents (list[Document]) A list of documents to write to the document store.
  • policy (DuplicatePolicy | None) The policy to use when encountering duplicate documents.

Returns:

  • dict[str, int] Number of documents written to the document store.

Raises:

  • ValueError If the specified document store is not found.
  • TypeError If the specified document store does not implement write_documents_async.