Skip to content

Replace

Replace mode replaces each detected entity with an alternative token. Anonymizer provides four strategies.


Strategy comparison

Strategy Example output for "Alice" (first_name) Deterministic Generated by LLM
Substitute Maya No Yes
Redact [REDACTED_FIRST_NAME] Yes No
Annotate <Alice, first_name> Yes No
Hash <HASH_FIRST_NAME_3bc51062973c> Yes No

Substitute

Replaces entities with LLM-generated synthetic values that are contextually plausible. This is the only replacement strategy that requires an LLM call.

Synthetic values must contain at least one non-whitespace character. Schema validation rejects empty strings and values made entirely of spaces, tabs, newlines, or Unicode whitespace. Valid one-character substitutes, such as an initial or a single-digit age, remain allowed. Valid values are preserved as generated, including any surrounding whitespace. This shared schema rule also applies to the replacement maps generated for rewrite mode.

After schema validation and filtering, Anonymizer checks that every unique requested (entity value, label) pair has a mapping. If mappings are missing, it retries only the missing pairs once, retaining the accepted substitutions. A record whose map is complete is returned; a record still missing mappings after the retry is excluded from successful output and reported in failed_records. Other complete records continue normally. Repeated occurrences of the same pair need only one mapping, and records without entities need no replacement-generator call.

Anonymizer compares retry proposals with detected originals ignoring case, trimming surrounding whitespace, and collapsing internal whitespace. A match receives a placeholder. New retry placeholders use the same comparison to avoid original values, accepted substitutions, other retry proposals, and placeholders already created in that retry. Different entities can still share an ordinary model-generated substitute.

Before discarding a mapping, Substitute can reconcile a copied original that differs only in no-break spaces (U+00A0), thin spaces (U+2009), narrow no-break spaces (U+202F), or ASCII spaces. The label must match exactly and the comparison must identify exactly one pair in the original requested inventory. Exact mappings take precedence. The mapping's original is restored from that inventory; the source text, span offsets, and synthetic value stay unchanged. This comparison does not strip or collapse whitespace or change other characters. Ambiguous or unmatched entries remain missing and follow the retry/failure flow above.

This completeness retry applies to schema-valid responses. DataDesigner already retries schema-invalid responses, validating the record's entire mapping response on each attempt. Its default is up to five conversation restarts after the initial attempt, controlled by max_conversation_restarts in the RunConfig passed as Anonymizer(data_designer_run_config=...). These schema retries also apply to the missing-pair generation call. The one completeness retry therefore does not mean a maximum of two LLM requests. Valid entries from a schema-rejected response are not retained for a targeted per-entry retry.

If the completeness retry fails validation after DataDesigner's recovery attempts, or its workflow fails, the affected records are reported as failed while complete records are retained. Initial generation retains the existing failure handling: dropped records appear in failed_records, but a failure of the entire initial workflow can raise AnonymizerWorkflowError. Completeness checks exact pairs; it does not guarantee the semantic quality of a generated substitute or validate span offsets.

from anonymizer import AnonymizerConfig, Substitute

# Default: uses the replacement_generator model
AnonymizerConfig(replace=Substitute())

# With additional instructions
AnonymizerConfig(replace=Substitute(
    instructions="Replacement IDs must always start with the same 4 characters as the original."
))
Field Default Description
instructions None Additional instructions for the LLM replacement generator.

Model role

Substitute uses the replacement_generator role. Override it if required in your model config:

selected_models:
  replace:
    replacement_generator: your-model-alias

Redact

Replaces entities with a label-based marker. The original text is removed entirely.

from anonymizer import AnonymizerConfig, Redact

# Default: [REDACTED_FIRST_NAME]
AnonymizerConfig(replace=Redact())

# Custom template
AnonymizerConfig(replace=Redact(format_template="****"))

# Template with label only
AnonymizerConfig(replace=Redact(format_template="[{label}]"))
Field Default Description
format_template [REDACTED_{label}] Template with optional {label} placeholder.
normalize_label True Uppercase and clean the label before substitution.

Annotate

Tags entities with their label but preserves the original text. Useful for review and debugging.

from anonymizer import AnonymizerConfig, Annotate

# Default: <Alice, first_name>
AnonymizerConfig(replace=Annotate())

# Custom template
AnonymizerConfig(replace=Annotate(format_template="[{label}: {text}]"))
Field Default Description
format_template <{text}, {label}> Template with {text} and {label} placeholders. Both are required.

Note

The original text is still present, so this is not privacy-safe on its own.


Hash

Replaces entities with a deterministic hash digest. The same entity text always produces the same hash, enabling consistent de-identification across records.

from anonymizer import AnonymizerConfig, Hash

# Default: <HASH_FIRST_NAME_3bc51062973c>
AnonymizerConfig(replace=Hash())

# Short digest with SHA-1
AnonymizerConfig(replace=Hash(algorithm="sha1", digest_length=8))
Field Default Description
algorithm sha256 Hash algorithm (sha256, sha1, or md5).
digest_length 12 Number of hex characters to keep (6--64).
format_template <HASH_{label}_{digest}> Template with {digest} required; {label} optional.

Evaluating replace output

After running replace, you can score detection recall and the quality of substitutions using LLM-as-judge evaluation. See Evaluation for details on the always-on entity coverage judge, the three Substitute quality judges (type fidelity, attribute fidelity, relational consistency), the opt-in detection validity judge, and how to call Anonymizer.evaluate().