Bridging Presidio And Spacy logo

Bridging Presidio And Spacy

CommunityPopular
maziyarpanahi
bridging-presidio-and-spacy

Combine OpenMed clinical NLP with Microsoft Presidio, spaCy, or LangChain through OpenMed's built-in interop adapter registry (openmed.interop). Covers the lazy adapter registry (available_adapters, get_adapter, adapter_spec), the presidio/spacy/langchain pip extras, and the verified callables — Presidio to_canonical/from_canonical/merge_with_openmed, the spaCy openmed_deid pipeline factory, and the LangChain create_redaction_runnable. Use when the user wants to add Presidio recognizers, embed OpenMed PII detection in a spaCy pipeline, or use OpenMed de-identification as a LangChain runnable. Pairs adjacent to the OpenMed PII skills.

Overview

Publishermaziyarpanahi
Repositoryopenmed
Skill namebridging-presidio-and-spacy
Stars
5.3K
Forks
677
Bundled files
Instructions only
LicenseApache-2.0
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by maziyarpanahi on GitHub. Read the source before you install it.

Installation

Install the Bridging Presidio And Spacy AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/maziyarpanahi/openmed.git /tmp/openmed
mkdir -p .claude/skills
cp -r /tmp/openmed/skills/bridging-presidio-and-spacy .claude/skills/bridging-presidio-and-spacy
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Bridging Presidio And Spacy in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Bridging Presidio And Spacy on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Bridging Presidio And Spacy is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Bridging Presidio, spaCy & LangChain

OpenMed interoperates with the dominant PII/NLP ecosystems through a single, lazy adapter registry: openmed.interop. Adapters live behind explicit imports, so importing openmed never drags in Presidio, spaCy, or LangChain — each is an optional extra you install only when you need that bridge.

When to use

Reach for a bridge when:

  • you already run Microsoft Presidio and want OpenMed's clinical PII recall on top (or to feed OpenMed spans back into Presidio's anonymizer);
  • you have a spaCy pipeline and want OpenMed PII spans on the Doc;
  • you build LangChain chains and want to redact PHI before text reaches an LLM (the on-device guardrail in front of a cloud model);
  • you need OpenMed's de-identification reachable from an existing framework instead of rewriting the pipeline around openmed.deidentify.

The lazy adapter registry (verified)

python
import openmed.interop as interop

interop.available_adapters()
# ('cda', 'hl7v2', 'langchain', 'presidio', 'spacy')

spec = interop.adapter_spec("presidio")
# AdapterSpec(name='presidio', module='openmed.interop.presidio',
#             extra='presidio', description='Presidio RecognizerResult adapter')

mod = interop.get_adapter("presidio")        # imports openmed.interop.presidio
# Attribute access also works lazily:
openmed.interop.presidio                      # same module, imported on first touch

available_adapters() and adapter_spec() never import the adapter module, so they are safe to call for discovery even without the extra installed. get_adapter(name) (and attribute access) triggers the import — and the adapter's own optional dependency.

Install only the extra you need:

bash
pip install "openmed[presidio]"     # Presidio RecognizerResult adapter
pip install "openmed[spacy]"        # spaCy openmed_deid component
pip install "openmed[langchain]"    # LangChain redaction runnable
# cda and hl7v2 adapters ship in core (no extra) — see their own skills

Presidio bridge (verified callables)

Module openmed.interop.presidio converts between Presidio RecognizerResults and OpenMed canonical PIIEntitys, and merges both detectors through OpenMed's semantic-unit merger.

python
from openmed.interop.presidio import (
    to_canonical,        # RecognizerResult(s) -> [PIIEntity]
    from_canonical,      # [PIIEntity] -> [RecognizerResult]  (needs presidio extra)
    merge_with_openmed,  # combine OpenMed + Presidio spans, resolve overlaps
    PresidioAdapterConfig,
)
import openmed

text = "Dr. Smith called patient at 617-555-0123 on 2024-03-02."

# Presidio gives you RecognizerResults; OpenMed gives PIIEntities.
openmed_spans = openmed.extract_pii(text).entities
presidio_results = analyzer.analyze(text=text, language="en")   # your Presidio analyzer

merged = merge_with_openmed(
    openmed_spans, presidio_results, text=text,
    config=PresidioAdapterConfig(preserve_presidio_labels=True),
)
# -> de-duplicated [PIIEntity]; overlaps resolved by score, length, OpenMed-origin

Why merge instead of union: merge_with_openmed runs both detectors' spans through merge_entities_with_semantic_units, so overlapping/adjacent detections collapse into one correct span (e.g. PHONE from Presidio vs a partial OpenMed hit) rather than producing double redactions. Label mapping is built in (Presidio PHONE_NUMBER ↔ OpenMed PHONE, US_SSNSSN, etc.).

To push OpenMed spans into Presidio's anonymizer, convert back:

python
results = from_canonical(openmed_spans)        # [RecognizerResult]
anonymized = anonymizer.anonymize(text=text, analyzer_results=results)

spaCy bridge (verified factory)

Module openmed.interop.spacy_component registers a spaCy pipeline factory named openmed_deid. Add it to a pipeline and OpenMed PII spans land on the Doc.

python
import spacy
import openmed.interop.spacy_component   # registers the @Language.factory

nlp = spacy.blank("en")
nlp.add_pipe("openmed_deid", config={
    "confidence_threshold": 0.5,
    "lang": "en",
    "target": "openmed_pii",     # doc.spans key
    "merge_ents": False,         # set True to also write doc.ents
    "alignment_mode": "expand",  # char->token alignment: strict|contract|expand
})

doc = nlp("Patient John Doe, MRN 12345, seen today.")
for span in doc.spans["openmed_pii"]:
    print(span.label_, span.text)
# raw char-offset spans also available on doc._.openmed_pii

merge_ents=True writes the spans into doc.ents, resolving overlaps with spaCy's filter_spans. Use OpenMedDeidComponent / OpenMedDeidConfig directly if you construct the component outside add_pipe.

LangChain bridge (verified runnable)

Module openmed.interop.langchain exposes a Runnable-shaped redactor you drop in front of an LLM step so PHI never leaves the device.

python
from openmed.interop.langchain import (
    create_redaction_runnable, LangChainRedactionConfig,
)

redactor = create_redaction_runnable(
    config=LangChainRedactionConfig(method="mask", policy="hipaa_safe_harbor"),
    input_key="text",        # redact this key in a dict payload (optional)
    output_key="text",
)

chain = redactor | prompt | llm          # redact -> prompt -> model
chain.invoke({"text": "John Doe, MRN 12345, has type 2 diabetes."})

The transform redacts strings, LangChain Documents (page_content), lists, tuples, and mapping payloads. Use create_redaction_transform(...) for the dependency-light object (no langchain-core needed) and .as_runnable() when you want the RunnableLambda. LangChainRedactionConfig forwards the full openmed.deidentify surface (method, policy, confidence_threshold, keep_year, consistent, lang, ...).

Hand-off to / from OpenMed

  • Into OpenMed: Presidio RecognizerResults and (implicitly) spaCy text become OpenMed PIIEntitys via the adapters; from there use the normal OpenMed de-id/audit/policy skills.
  • Out of OpenMed: from_canonical → Presidio anonymizer; the spaCy component → downstream spaCy components; the LangChain runnable → any chain.
  • The canonical object everywhere is openmed.core.pii.PIIEntity (text, label, confidence, start, end, entity_type, metadata).

Edge cases & gotchas

  • Discovery is free; import is not. Call available_adapters() / adapter_spec() to probe without installing the extra. Touching the module (get_adapter/attribute access) raises a clear ImportError telling you the extra to install if it is missing.
  • Offsets must match the same text. merge_with_openmed and the spaCy alignment both assume all spans index the same string. De-identify or normalise once, up front; do not mix offsets from pre- and post-normalised text.
  • alignment_mode="expand" (spaCy default here) snaps char spans out to token boundaries; use "strict" if you need exact char alignment and accept dropped spans that do not align.
  • LangChain redaction is a guardrail, not a guarantee. Gate de-id quality with openmed.eval leakage gates (evaluating-with-leakage-gates) before trusting it in front of a cloud LLM.
  • Local-first holds across bridges. OpenMed inference stays on-device; only your downstream LLM/cloud step (if any) leaves the machine — which is exactly why you redact first.

Standards & references

Frequently asked questions

What does the Bridging Presidio And Spacy AI skill do?

Combine OpenMed clinical NLP with Microsoft Presidio, spaCy, or LangChain through OpenMed's built-in interop adapter registry (openmed.interop). Covers the lazy adapter registry (available_adapters, get_adapter, adapter_spec), the presidio/spacy/langchain pip extras, and the verified callables — Presidio to_canonical/from_canonical/merge_with_openmed, the spaCy openmed_deid pipeline factory, and the LangChain create_redaction_runnable. Use when the user wants to add Presidio recognizers, embed OpenMed PII detection in a spaCy pipeline, or use OpenMed de-identification as a LangChain runnabl...

Why use Bridging Presidio And Spacy on TypingMind?

Because you install it once and use it with any model. Bridging Presidio And Spacy is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Bridging Presidio And Spacy in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/maziyarpanahi/openmed/tree/master/skills/bridging-presidio-and-spacy. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Bridging Presidio And Spacy?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Bridging Presidio And Spacy?

As many as you like. As long as a model supports skills, you can use Bridging Presidio And Spacy with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Bridging Presidio And Spacy AI skill free?

Yes. It is published on GitHub by maziyarpanahi under the Apache-2.0 license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇