|
| 1 | +--- |
| 2 | +title: "PresidioDocumentCleaner" |
| 3 | +id: presidiodocumentcleaner |
| 4 | +slug: "/presidiodocumentcleaner" |
| 5 | +description: "Use `PresidioDocumentCleaner` to replace PII in Document text with entity type placeholders, powered by Microsoft Presidio." |
| 6 | +--- |
| 7 | + |
| 8 | +# PresidioDocumentCleaner |
| 9 | + |
| 10 | +`PresidioDocumentCleaner` replaces personally identifiable information (PII) in the text content of Documents with entity type placeholders such as `<PERSON>` or `<EMAIL_ADDRESS>`. Original Documents are not mutated. Documents without text content pass through unchanged. |
| 11 | + |
| 12 | +<div className="key-value-table"> |
| 13 | + |
| 14 | +| | | |
| 15 | +| --- | --- | |
| 16 | +| **Most common position in a pipeline** | In an indexing pipeline, before writing Documents to a Document Store | |
| 17 | +| **Mandatory run variables** | `documents`: A list of Document objects | |
| 18 | +| **Output variables** | `documents`: A list of Document objects with PII replaced | |
| 19 | +| **API reference** | [Presidio](/reference/integrations-presidio) | |
| 20 | +| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/presidio | |
| 21 | + |
| 22 | +</div> |
| 23 | + |
| 24 | +## Overview |
| 25 | + |
| 26 | +[Microsoft Presidio](https://microsoft.github.io/presidio/) is an open-source framework for PII detection and anonymization. `PresidioDocumentCleaner` uses Presidio's Analyzer and Anonymizer engines to scan document text and replace detected entities with type placeholders such as `<PERSON>` or `<EMAIL_ADDRESS>`. |
| 27 | + |
| 28 | +This is useful when you want to store sanitized versions of your documents in a Document Store — for example, to prevent sensitive information from being indexed or returned in search results. |
| 29 | + |
| 30 | +## Configuration |
| 31 | + |
| 32 | +| Parameter | Default | Description | |
| 33 | +| --- | --- | --- | |
| 34 | +| `language` | `"en"` | Language code for PII detection. See [supported languages](https://microsoft.github.io/presidio/analyzer/languages/). | |
| 35 | +| `entities` | `None` | List of PII entity types to detect (e.g. `["PERSON", "EMAIL_ADDRESS"]`). If `None`, all supported types are detected. See [supported entities](https://microsoft.github.io/presidio/supported_entities/). | |
| 36 | +| `score_threshold` | `0.35` | Minimum confidence score (0–1) for a detected entity to be included. | |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +Install the `presidio-haystack` package to use the `PresidioDocumentCleaner`. |
| 41 | + |
| 42 | +```bash |
| 43 | +pip install presidio-haystack |
| 44 | +# Download the English NLP model required by Presidio's analyzer engine |
| 45 | +python -m spacy download en_core_web_lg |
| 46 | +``` |
| 47 | + |
| 48 | +### On its own |
| 49 | + |
| 50 | +```python |
| 51 | +from haystack import Document |
| 52 | +from haystack_integrations.components.preprocessors.presidio import PresidioDocumentCleaner |
| 53 | + |
| 54 | +cleaner = PresidioDocumentCleaner() |
| 55 | +result = cleaner.run(documents=[ |
| 56 | + Document(content="Contact Alice Smith at alice@example.com or 212-555-1234.") |
| 57 | +]) |
| 58 | +print(result["documents"][0].content) |
| 59 | +# Contact <PERSON> at <EMAIL_ADDRESS> or <PHONE_NUMBER>. |
| 60 | +``` |
| 61 | + |
| 62 | +### In a pipeline |
| 63 | + |
| 64 | +```python |
| 65 | +from haystack import Document, Pipeline |
| 66 | +from haystack.components.writers import DocumentWriter |
| 67 | +from haystack.document_stores.in_memory import InMemoryDocumentStore |
| 68 | +from haystack_integrations.components.preprocessors.presidio import PresidioDocumentCleaner |
| 69 | + |
| 70 | +document_store = InMemoryDocumentStore() |
| 71 | + |
| 72 | +indexing_pipeline = Pipeline() |
| 73 | +indexing_pipeline.add_component("cleaner", PresidioDocumentCleaner()) |
| 74 | +indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store)) |
| 75 | +indexing_pipeline.connect("cleaner", "writer") |
| 76 | + |
| 77 | +indexing_pipeline.run({ |
| 78 | + "cleaner": { |
| 79 | + "documents": [ |
| 80 | + Document(content="Alice Smith's email is alice@example.com"), |
| 81 | + Document(content="Call Bob at 212-555-9876"), |
| 82 | + ] |
| 83 | + } |
| 84 | +}) |
| 85 | +``` |
| 86 | + |
| 87 | +### Using Custom Parameters |
| 88 | + |
| 89 | +To customize PII detection, pass parameters when initializing the cleaner: |
| 90 | + |
| 91 | +```python |
| 92 | +from haystack_integrations.components.preprocessors.presidio import PresidioDocumentCleaner |
| 93 | + |
| 94 | +cleaner = PresidioDocumentCleaner( |
| 95 | + language="de", |
| 96 | + entities=["PERSON", "EMAIL_ADDRESS"], |
| 97 | + score_threshold=0.7, |
| 98 | +) |
| 99 | +``` |
0 commit comments