Skip to main content

PyTextAD: Text Anomaly Detection in Python

PyPI Documentation Tests License

PyTextAD is a Python library for detecting anomalies in text, at the document and at the token level. Every detector follows the PyOD interface (fit, decision_function, predict, decision_scores_, labels_), and every re-implemented method is checked numerically against its original code.

Installation

pip install pytextad

From source:

git clone https://github.com/charles-cao/pytextad.git
cd pytextad
pip install -e ".[test]"

Requires Python >= 3.9, PyTorch >= 1.13 and transformers >= 4.30. Install the PyTorch build that matches your CUDA version first (https://pytorch.org).

Quick start

from pytextad import CVDD, DATE, FATE, RSRAE, DocumentDetector, TokenDetector
from pytextad import SentenceEmbedder, TokenEmbedder
from pytextad.metrics import evaluate, format_results
from pyod.models.knn import KNN

# document level: any PyOD detector on sentence embeddings, or an end-to-end text detector
scores = DocumentDetector(KNN(), embedder=SentenceEmbedder("bert-base-uncased")) \
    .fit(train_texts).decision_function(test_texts)                     # higher = more anomalous
scores = DATE().fit(train_texts).decision_function(test_texts)

# token level: one embedding per word, any vector detector, token scores aggregated per document
emb = TokenEmbedder("bert-base-cased", word_pooling="max")
X_train, _ = emb.transform(train_words, cache="train.npz")               # lists of words in
X_test, _ = emb.transform(test_words, cache="test.npz")
res = evaluate(TokenDetector(KNN()), X_train, X_test, token_labels=test_labels, seeds=(0, 1, 2))
print(format_results({"KNN": res}))           # token and document AUROC / AP / FPR95, mean ± std

A runnable example on AG News: python examples/quickstart.py.

Implemented methods

Method Year Input Token scores Reference
CVDD 2019 frozen token embeddings yes Ruff et al., Self-Attentive, Multi-Context One-Class Classification for Unsupervised Anomaly Detection on Text, ACL 2019
RSRAE 2020 vectors via TokenDetector Lai et al., Robust Subspace Recovery Layer for Unsupervised Anomaly Detection, ICLR 2020
DATE 2021 raw text yes Manolache et al., DATE: Detecting Anomalies in Text via Self-Supervision of Transformers, NAACL 2021
FATE 2023 raw text (+ few labelled anomalies) no Das et al., Few-shot Anomaly Detection in Text with Deviation Learning, ICONIP 2023

Any vector detector (all of PyOD, or your own with fit / decision_function) works on text through DocumentDetector (sentence embeddings) and TokenDetector (token embeddings, token scores aggregated per document).

Default hyperparameters are those of the official code. Each module's docstring lists where the official code and the paper disagree and which one we follow.

Faithfulness to the original implementations

tests/verification/ runs each original implementation next to ours with the same weights, inputs and random seeds and compares the results (DATE against the original transformers 3.0.2 code, RSRAE against the original TensorFlow code). All checks pass; see tests/verification/README.md.

Running the tests

pytest                               # fast API tests, a few seconds
PYTEXTAD_DEVICE=cuda pytest            # same, on GPU (PowerShell: $env:PYTEXTAD_DEVICE="cuda"; pytest)

License

BSD 2-Clause. Third-party notices for the original implementations are in THIRD_PARTY_NOTICES.md.

Metadata

Release files for pytextad 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytextad 0.2.0
File Size Uploaded
pytextad-0.2.0.tar.gz 34.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytextad 0.2.0
File Interpreter ABI Platform
pytextad-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 67.3 kB

Release files / pytextad-0.2.0.tar.gz

Download URL pytextad-0.2.0.tar.gz
Size 34.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e0073bed682ec5da6f0db717b5a4a06f3378300742d0ffa710dd6c3aac116ca0
BLAKE2b-256 checksum
How to use checksums
4cf33ad71644e0d91e81820327379402597fc9ff491f4e6cc4e7336969615305
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

Release files / pytextad-0.2.0-py3-none-any.whl

Download URL pytextad-0.2.0-py3-none-any.whl
Size 33.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2c5cf02e03cbacb67b99fa28612d3c4f83ae9c8e894c2fa275ad7a40742b213
BLAKE2b-256 checksum
How to use checksums
c178ade0ed351f40284de39616418bf623598a09d56b522a589fb1bab608da90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

Release history Release notifications | RSS feed

0.3.0

2 release files

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page