Presidio
haystack_integrations.components.extractors.presidio.presidio_entity_extractor
PresidioEntityExtractor
Detects PII entities in Haystack Documents using Microsoft Presidio Analyzer.
See Presidio Analyzer for details.
Accepts a list of Documents and returns new Documents with detected PII entities stored
in each Document's metadata under the key "entities". Each entry in the list contains
the entity type, start/end character offsets, and the confidence score.
Original Documents are not mutated. Documents without text content are passed through unchanged.
The analyzer engine is loaded on the first call to run(),
or by calling warm_up() explicitly beforehand.
Usage example
from haystack import Document
from haystack_integrations.components.extractors.presidio import PresidioEntityExtractor
extractor = PresidioEntityExtractor()
result = extractor.run(documents=[Document(content="Contact Alice at alice@example.com")])
print(result["documents"][0].meta["entities"])
# [{"entity_type": "PERSON", "start": 8, "end": 13, "score": 0.85},
# {"entity_type": "EMAIL_ADDRESS", "start": 17, "end": 34, "score": 1.0}]
SPACY_DEFAULT_MODELS
Mapping from ISO 639-1 language code to the largest available spaCy model for that language.
Used to automatically select an NLP model when models is not specified.
See spaCy documentation for the full list of available spaCy models.
init
__init__(
*,
language: str = "en",
entities: list[str] | None = None,
score_threshold: float = 0.35,
models: list[dict[str, str]] | None = None
) -> None
Initializes the PresidioEntityExtractor.
Parameters:
- language (
str) – ISO 639-1 language code for PII detection. Defaults to"en". For languages in the built-in mapping (e.g."de","fr","es"), the appropriate spaCy model is loaded automatically at warm-up time — no need to setmodels. For unsupported languages, use themodelsparameter to configure a custom model. See Presidio supported languages. - entities (
list[str] | None) – List of PII entity types to detect (e.g.["PERSON", "EMAIL_ADDRESS"]). IfNone, all supported entity types are detected. See Presidio supported entities. - score_threshold (
float) – Minimum confidence score (0-1) for a detected entity to be included. Defaults to0.35. See Presidio analyzer documentation. - models (
list[dict[str, str]] | None) – Advanced override: list of spaCy model configurations. Each entry must contain"lang_code"and"model_name"keys, e.g.[{"lang_code": "fr", "model_name": "fr_core_news_md"}]. Use this only when you need a specific model variant or a language not covered by the built-in mapping. IfNone, the model is selected automatically fromSPACY_DEFAULT_MODELSbased onlanguage.
warm_up
Initializes the Presidio analyzer engine.
This method loads the underlying NLP models. In a Haystack Pipeline, this is called automatically before the first run.
run
Detects PII entities in the provided Documents.
Parameters:
- documents (
list[Document]) – List of Documents to analyze for PII entities.
Returns:
dict[str, list[Document]]– A dictionary with keydocumentscontaining Documents with detected entities stored in metadata under the key"entities".
haystack_integrations.components.preprocessors.presidio.presidio_document_cleaner
PresidioDocumentCleaner
Anonymizes PII in Haystack Documents using Microsoft Presidio.
Accepts a list of Documents, detects personally identifiable information (PII) in their
text content, and returns new Documents with PII replaced by entity type placeholders
(e.g. <PERSON>, <EMAIL_ADDRESS>). Original Documents are not mutated.
Documents without text content are passed through unchanged.
The analyzer and anonymizer engines are loaded on the first call to run(),
or by calling warm_up() explicitly beforehand.
Usage example
from haystack import Document
from haystack_integrations.components.preprocessors.presidio import PresidioDocumentCleaner
cleaner = PresidioDocumentCleaner()
result = cleaner.run(documents=[Document(content="My name is John and my email is john@example.com")])
print(result["documents"][0].content)
# My name is <PERSON> and my email is <EMAIL_ADDRESS>
SPACY_DEFAULT_MODELS
Mapping from ISO 639-1 language code to the largest available spaCy model for that language.
Used to automatically select an NLP model when models is not specified.
See spaCy documentation for the full list of available spaCy models.
init
__init__(
*,
language: str = "en",
entities: list[str] | None = None,
score_threshold: float = 0.35,
models: list[dict[str, str]] | None = None
) -> None
Initializes the PresidioDocumentCleaner.
Parameters:
- language (
str) – ISO 639-1 language code for PII detection. Defaults to"en". For languages in the built-in mapping (e.g."de","fr","es"), the appropriate spaCy model is loaded automatically at warm-up time — no need to setmodels. For unsupported languages, use themodelsparameter to configure a custom model. See Presidio supported languages. - entities (
list[str] | None) – List of PII entity types to detect and anonymize (e.g.["PERSON", "EMAIL_ADDRESS"]). IfNone, all supported entity types are used. See Presidio supported entities. - score_threshold (
float) – Minimum confidence score (0-1) for a detected entity to be anonymized. Defaults to0.35. See Presidio analyzer documentation. - models (
list[dict[str, str]] | None) – Advanced override: list of spaCy model configurations. Each entry must contain"lang_code"and"model_name"keys, e.g.[{"lang_code": "fr", "model_name": "fr_core_news_md"}]. Use this only when you need a specific model variant or a language not covered by the built-in mapping. IfNone, the model is selected automatically fromSPACY_DEFAULT_MODELSbased onlanguage.
warm_up
Initializes the Presidio analyzer and anonymizer engines.
This method loads the underlying NLP models. In a Haystack Pipeline, this is called automatically before the first run.
run
Anonymizes PII in the provided Documents.
Parameters:
- documents (
list[Document]) – List of Documents whose text content will be anonymized.
Returns:
dict[str, list[Document]]– A dictionary with keydocumentscontaining the cleaned Documents.
haystack_integrations.components.preprocessors.presidio.presidio_text_cleaner
PresidioTextCleaner
Anonymizes PII in plain strings using Microsoft Presidio.
Accepts a list of strings, detects personally identifiable information (PII), and returns
a new list of strings with PII replaced by entity type placeholders (e.g. <PERSON>).
Useful for sanitizing user queries before they are sent to an LLM.
The analyzer and anonymizer engines are loaded on the first call to run(),
or by calling warm_up() explicitly beforehand.
Usage example
from haystack_integrations.components.preprocessors.presidio import PresidioTextCleaner
cleaner = PresidioTextCleaner()
result = cleaner.run(texts=["Hi, I am John Smith, call me at 212-555-1234"])
print(result["texts"][0])
# Hi, I am <PERSON>, call me at <PHONE_NUMBER>
SPACY_DEFAULT_MODELS
Mapping from ISO 639-1 language code to the largest available spaCy model for that language.
Used to automatically select an NLP model when models is not specified.
See spaCy documentation for the full list of available spaCy models.
init
__init__(
*,
language: str = "en",
entities: list[str] | None = None,
score_threshold: float = 0.35,
models: list[dict[str, str]] | None = None
) -> None
Initializes the PresidioTextCleaner.
Parameters:
- language (
str) – ISO 639-1 language code for PII detection. Defaults to"en". For languages in the built-in mapping (e.g."de","fr","es"), the appropriate spaCy model is loaded automatically at warm-up time — no need to setmodels. For unsupported languages, use themodelsparameter to configure a custom model. See Presidio supported languages. - entities (
list[str] | None) – List of PII entity types to detect and anonymize (e.g.["PERSON", "PHONE_NUMBER"]). IfNone, all supported entity types are used. See Presidio supported entities. - score_threshold (
float) – Minimum confidence score (0-1) for a detected entity to be anonymized. Defaults to0.35. See Presidio analyzer documentation. - models (
list[dict[str, str]] | None) – Advanced override: list of spaCy model configurations. Each entry must contain"lang_code"and"model_name"keys, e.g.[{"lang_code": "fr", "model_name": "fr_core_news_md"}]. Use this only when you need a specific model variant or a language not covered by the built-in mapping. IfNone, the model is selected automatically fromSPACY_DEFAULT_MODELSbased onlanguage.
warm_up
Initializes the Presidio analyzer and anonymizer engines.
This method loads the underlying NLP models. In a Haystack Pipeline, this is called automatically before the first run.
run
Anonymizes PII in the provided strings.
Parameters:
- texts (
list[str]) – List of strings to anonymize.
Returns:
dict[str, list[str]]– A dictionary with keytextscontaining the cleaned strings.
haystack_integrations.hooks.presidio.presidio_pii_hook
PresidioPIIHook
Detects personally identifiable information (PII) in an Agent's messages with Microsoft Presidio.
Register the same instance at before_llm, so PII never reaches the LLM provider, and at after_run, so the final
reply returned to the user is checked too. The final reply is produced by the last LLM call, so no before_llm
hook runs after it:
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack_integrations.hooks.presidio import PresidioPIIHook
pii_hook = PresidioPIIHook(
entity_score_thresholds={"PERSON": 0.6},
entity_actions={"CREDIT_CARD": "block", "IBAN_CODE": "stop"},
)
agent = Agent(
chat_generator=OpenAIChatGenerator(),
hooks={"before_llm": [pii_hook], "after_run": [pii_hook]},
)
result = agent.run(messages=[ChatMessage.from_user("Hi, I'm John Smith from Berlin.")])
# The LLM receives: "Hi, I'm <PERSON> from <LOCATION>."
Each run checks the messages it has not checked yet: user and assistant text and tool results. System messages, system reminders, tool-call arguments, reasoning, images, and files are not checked. Every detected entity maps to an action, and a message takes the strictest action among its entities:
"redact"(default): Presidio's anonymizer replaces each entity with a placeholder such as<PERSON>."block": The message's text is replaced withblock_message. Mid-run, a system reminder is added telling the model the message was removed; once the run is exiting, the reply is replaced without a reminder."stop": The message is redacted and the run stops before the next LLM call, withexit_reasonset to"pii_detected".
Hooks at the same hook point run in list order, and any hook listed before this one sees unredacted messages.
List it first, for example ahead of a CompactionHook that summarizes the conversation with an LLM. To keep PII
out of a ToolResultOffloadHook store, also register it first at after_tool:
hooks = {
"after_tool": [pii_hook, offload_hook],
"before_llm": [pii_hook, compaction_hook],
"after_run": [pii_hook],
}
Limitations:
- When the Agent or its chat generator has a
streaming_callback, the reply streams to the user before any hook runs, so PII in the reply cannot be stopped in that mode. - Because the model only sees placeholders, the tool-call arguments it writes contain placeholders too, so a tool
that needs the real value (for example, an email address to send to) receives
<EMAIL_ADDRESS>. - If the hook runs at
after_tooland blocks the result of a tool inexit_conditions, the reminder becomes the Agent's last message. Read the tool result fromresult["messages"]instead ofresult["last_message"]. - After an
on_exithook keeps the run going, messages blocked later in the run get no reminder.
Each run records its decision on the hook's tracing span under haystack.agent.hook.pii.*: how many messages it
checked, whether anything was found, the action taken, the count of each entity type, and whether Presidio failed.
The checked and rewritten texts are recorded as content tags, so they are only emitted when content tracing is on.
With content tracing on, traces contain raw PII: this hook's input tag holds the original text, and the Agent's own
spans record messages before any hook redacts them.
SPACY_DEFAULT_MODELS
Mapping from ISO 639-1 language code to the largest available spaCy model for that language.
Used to automatically select an NLP model when models is not specified.
See spaCy documentation for the full list of available spaCy models.
init
__init__(
*,
language: str = "en",
entities: list[str] | None = None,
score_threshold: float = 0.35,
entity_score_thresholds: dict[str, float] | None = None,
entity_actions: dict[str, PIIAction] | None = None,
on_error: PIIErrorAction = "block",
block_message: str = _DEFAULT_BLOCK_MESSAGE,
models: list[dict[str, str]] | None = None
) -> None
Initialize the hook.
Parameters:
- language (
str) – ISO 639-1 language code for PII detection. Defaults to"en". For languages inSPACY_DEFAULT_MODELS, the spaCy model is loaded automatically at warm-up time. See Presidio supported languages. - entities (
list[str] | None) – PII entity types to detect, for example["PERSON", "PHONE_NUMBER"]. IfNone, all entity types Presidio supports forlanguageare detected. See Presidio supported entities. - score_threshold (
float) – Minimum confidence score (0-1) for a detected entity to count, for entity types not listed inentity_score_thresholds. Defaults to0.35. - entity_score_thresholds (
dict[str, float] | None) – Per-entity-type minimum confidence scores (0-1) that overridescore_threshold, for example{"PERSON": 0.6}to ignore low-confidence name matches. - entity_actions (
dict[str, PIIAction] | None) – Per-entity-type actions, one of"redact","block", or"stop", for example{"CREDIT_CARD": "block"}. Entity types not listed are redacted. - on_error (
PIIErrorAction) – What to do with a message when Presidio raises an error while checking it:"block"(default) replaces it withblock_message,"stop"also stops the run, and"pass"leaves the message unchanged, which can let PII through. - block_message (
str) – The text that replaces the text of a blocked message. - models (
list[dict[str, str]] | None) – Advanced override: spaCy model configurations, each with"lang_code"and"model_name"keys, for example[{"lang_code": "fr", "model_name": "fr_core_news_md"}]. IfNone, the model is selected fromSPACY_DEFAULT_MODELSbased onlanguage.
Raises:
ValueError– If a score threshold is outside 0-1, or an action oron_errorvalue is unknown.
warm_up
Load the Presidio analyzer and anonymizer engines and check the configured entity types.
The Agent calls this from its own warm_up. Calling it again does nothing.
Raises:
ValueError– If no spaCy model is available forlanguage, or an entity type inentities,entity_score_thresholds, orentity_actionsis not supported forlanguage, or an entity type inentity_score_thresholdsorentity_actionsis not inentities.
run
Check the messages not checked yet and redact, block, or stop as configured.
Parameters:
- state (
State) – The Agent's liveState. Itsmessagesare rewritten in place;stop_runis set when a message triggers"stop".
to_dict
Serialize the hook to a dictionary.
Returns:
dict[str, Any]– The serialized hook.
from_dict
Deserialize the hook from a dictionary.
Parameters:
- data (
dict[str, Any]) – The dictionary produced byto_dict.
Returns:
PresidioPIIHook– The deserialized hook.