#2312·presidio

Docs: sample for anonymizing streamed LLM output

Author: ninadphalakCreated Sep 29, 2026Updated Sep 29, 2026

Is your feature request related to a problem? Please describe.

Presidio is often run on LLM output, for example by LiteLLM's Presidio guardrail and NeMo Guardrails' sensitive data rail, and LLM output is usually streamed. A stream arrives as small text deltas split wherever the tokenizer and the network put them, so an entity can arrive in two deltas. The docs have no guidance for this case.

Analyzing each delta on its own leaks. With presidio-analyzer 2.2.364 and en_core_web_lg:

python
deltas = [
    "Sure. You can reach Jane at jane.d",
    "[email protected] or on 212-55",
    "5-0147.\nAnything else?",
]
for d in deltas:
    send(anonymizer.anonymize(text=d, analyzer_results=analyzer.analyze(text=d, language="en")).text)

sends

Sure. You can reach <PERSON> at jane.d<EMAIL_ADDRESS> or on 212-555-0147.
Anything else?

The phone number goes out in full, and jane.d goes out before the rest of the address is detected. The usual next fix, prepending the tail of the previous delta, detects the entity but only after its first part was sent. Integrations have shipped both versions (for example BerriAI/litellm#41611).

The existing chunkers (#1805) solve the offline version of this with overlap. Streaming is different because Presidio does not choose the split points and text already sent cannot be taken back.

Describe the solution you'd like

A self-contained sample under docs/samples/python/streaming_llm_output/, added to docs/samples/index.md and mkdocs.yml. No library changes and no new dependencies.

  • streaming_anonymizer.py: a small StreamingAnonymizer with feed(delta) and flush(). It holds text until a segment boundary (a newline, or whitespace after ., ! or ?) and analyzes each complete segment. Boundaries depend only on the text, so the output is the same however the stream was split. A segment longer than a limit raises instead of being sent unanalyzed.
  • test_streaming_anonymizer.py: checks that the output is identical for every two-part split and every fixed delta size of the input, and shows that per-delta analysis fails the same check. Runs in about 15 seconds.
  • index.md: the problem, the design, its tradeoffs (sentence-level latency, context words in earlier segments are not seen, choosing a boundary your entities cannot contain), and how to test any streaming wrapper around Presidio.

I have a working draft and can open a PR if this is useful.

Describe alternatives you've considered

  • A streaming API in presidio-anonymizer. A bigger change; a sample shows whether there is demand first.
  • Holding back a fixed number of characters. Some entities have no maximum length, so any fixed window can be crossed.

Additional context

Background on the same defect in other LLM gateways and frameworks: https://doi.org/10.5281/zenodo.22909585

Source: data-privacy-stack/presidio