chore: import upstream snapshot with attribution
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:22:28 +08:00
commit c56bef871b
9296 changed files with 1854228 additions and 0 deletions
@@ -0,0 +1,129 @@
---
title: "Document Writers"
id: document-writers-api
description: "Writes Documents to a DocumentStore."
slug: "/document-writers-api"
---
## document_writer
### DocumentWriter
Writes documents to a DocumentStore.
### Usage example
```python
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__
```python
__init__(
document_store: DocumentStore,
policy: DuplicatePolicy = DuplicatePolicy.NONE,
)
```
Create a DocumentWriter component.
**Parameters:**
- **document_store** (<code>DocumentStore</code>) The instance of the document store where you want to store your documents.
- **policy** (<code>DuplicatePolicy</code>) 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
```python
to_dict() -> dict[str, Any]
```
Serializes the component to a dictionary.
**Returns:**
- <code>dict\[str, Any\]</code> Dictionary with serialized data.
#### from_dict
```python
from_dict(data: dict[str, Any]) -> DocumentWriter
```
Deserializes the component from a dictionary.
**Parameters:**
- **data** (<code>dict\[str, Any\]</code>) The dictionary to deserialize from.
**Returns:**
- <code>DocumentWriter</code> The deserialized component.
**Raises:**
- <code>DeserializationError</code> If the document store is not properly specified in the serialization data or its type cannot be imported.
#### run
```python
run(
documents: list[Document], policy: DuplicatePolicy | None = None
) -> dict[str, int]
```
Run the DocumentWriter on the given input data.
**Parameters:**
- **documents** (<code>list\[Document\]</code>) A list of documents to write to the document store.
- **policy** (<code>DuplicatePolicy | None</code>) The policy to use when encountering duplicate documents.
**Returns:**
- <code>dict\[str, int\]</code> Number of documents written to the document store.
**Raises:**
- <code>ValueError</code> If the specified document store is not found.
#### run_async
```python
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** (<code>list\[Document\]</code>) A list of documents to write to the document store.
- **policy** (<code>DuplicatePolicy | None</code>) The policy to use when encountering duplicate documents.
**Returns:**
- <code>dict\[str, int\]</code> Number of documents written to the document store.
**Raises:**
- <code>ValueError</code> If the specified document store is not found.
- <code>TypeError</code> If the specified document store does not implement `write_documents_async`.