langchain-maskena
LangChain integration for Maskena: stop prompt injection in user input and in retrieved documents, and load documents with hidden text and personal data removed.
| Component | Where it goes | What it does |
|---|---|---|
MaskenaGuard |
Before the prompt | Blocks dangerous user input; continues with the clean version when it is suspicious |
MaskenaDocumentCompressor |
After the retriever | Drops dangerous retrieved documents; cleans suspicious ones (indirect prompt injection) |
MaskenaLoader |
Ingestion | Loads files through RAG sanitization: only safe content, already split into chunks |
Installation
pip install langchain-maskena
export MASKENA_API_KEY="sfk_..." # create a key in the dashboard, under API
Requires Python 3.11 or later and langchain-core 1.x.
Guarding user input
from langchain_core.prompts import ChatPromptTemplate
from langchain_maskena import MaskenaBlockedError, MaskenaGuard
prompt = ChatPromptTemplate.from_messages([("system", "You are a support assistant."), ("human", "{question}")])
chain = {"question": MaskenaGuard()} | prompt | llm
try:
answer = chain.invoke(user_message)
except MaskenaBlockedError as e:
answer = "Sorry, I can't help with that request."
log.warning("blocked: %s %s", e.outcome.verdict, [f.rule_id for f in e.outcome.findings])
MaskenaGuard accepts a string, a dict of prompt variables, a list of messages or a prompt value, and returns the
same type. With a dict, use input_keys=["question"] to check only some keys. With messages, only human messages are
checked unless check_all_messages=True.
Guarding retrieved documents
Documents retrieved from a vector store can carry instructions planted by whoever wrote them. Check them before the model reads them:
from langchain_classic.retrievers import ContextualCompressionRetriever
from langchain_maskena import MaskenaDocumentCompressor
retriever = ContextualCompressionRetriever(
base_compressor=MaskenaDocumentCompressor(),
base_retriever=vector_store.as_retriever(),
)
docs = retriever.invoke("How long do refunds take?")
Kept documents carry maskena_verdict, maskena_score and maskena_cleaned in their metadata. You can also call
compressor.compress_documents(docs, query) directly, or acompress_documents for parallel checks
(max_concurrency, default 8).
Loading documents for RAG
from langchain_maskena import MaskenaLoader
docs = MaskenaLoader(["contract.pdf", "manual.docx"], policy="redact", chunk_size=1200).load()
vector_store.add_documents(docs)
Hidden text (white, microscopic, off-page or covered by other elements) is removed based on how each page renders,
before anything reaches your index. Each chunk becomes a Document with source, page_start, page_end,
maskena_risk, maskena_status and maskena_job_id in its metadata. Chunks in quarantine (policy="drop") are not
returned.
Decision rule
All components use the same rule:
| Option | Values | Default |
|---|---|---|
block_on |
"dangerous", "flagged" (also suspicious), "never" |
"dangerous" |
use_clean_text |
"flagged", "always", "never" |
"flagged" |
Blocked input raises MaskenaBlockedError; blocked documents are dropped. The clean version removes dangerous passages
and replaces personal data with placeholders such as [EMAIL 1]. With use_clean_text="always", personal data is
removed from every text, not only from flagged ones.
Texts longer than 32,000 characters are split and checked in parts.
Configuration and costs
MaskenaGuard(api_key="sfk_...", base_url="https://maskena.com", language="en", depth="standard")
Each guarded text or document uses one text check from the monthly quota, and MaskenaLoader uses one RAG page per
page. To reuse a configured client (proxies, retries), pass client=maskena.Maskena(...) and
async_client=maskena.AsyncMaskena(...).
Links
License
MIT
Metadata
Release files for langchain-maskena 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| langchain_maskena-0.1.0.tar.gz | 13.0 kB | Without provenance | Details for langchain_maskena-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| langchain_maskena-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for langchain_maskena-0.1.0-py3-none-any.whl |
Total release size: 23.1 kB
Release files / langchain_maskena-0.1.0.tar.gz
| Download URL | langchain_maskena-0.1.0.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4eb157cba41f6119b43019ebab4221f8252dbf648e434e4f22e11c0d406e6cf4
|
|
BLAKE2b-256 checksum How to use checksums |
a73b25009c305f4e9a51ba72c0caf60749757ec45abd419239b27cf23efaec0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.9
|
Release files / langchain_maskena-0.1.0-py3-none-any.whl
| Download URL | langchain_maskena-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c891bb596ca3b054bdecf4069f0be2692cdd9400aa588a7c4fec829ccfd3aefe
|
|
BLAKE2b-256 checksum How to use checksums |
85ab736acfc8a6e35470512a37f343a4db2999c4bdbb8ec6c5c4908e6a86c411
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.9
|
Security for langchain-maskena 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.