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, TokenEmbedder, mean_pool

# frozen token embeddings from any Hugging Face encoder
emb = TokenEmbedder("bert-base-uncased")
H_train, _ = emb.transform(train_texts)
H_test, _ = emb.transform(test_texts)

scores = CVDD().fit(H_train).decision_function(H_test)                       # higher = more anomalous
scores = RSRAE().fit(mean_pool(H_train)).decision_function(mean_pool(H_test))
scores = DATE().fit(train_texts).decision_function(test_texts)               # raw text in, trains its own model
scores = FATE().fit(texts, y).decision_function(test_texts)                  # few-shot: y = 1 for labelled anomalies

word_scores = DATE().fit(train_texts).token_scores([t.split() for t in test_texts])

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 document vectors no 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

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.1.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.1.0
File Size Uploaded
pytextad-0.1.0.tar.gz 23.2 kB Details

Built distribution (wheel)

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

Total release size: 48.3 kB

Release files / pytextad-0.1.0.tar.gz

Download URL pytextad-0.1.0.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
47c814d443a92b5d4db2840d659a9de65afb0ca8614463d282f77d90b3a42eec
BLAKE2b-256 checksum
How to use checksums
f1b7a507ba383565c0ecf136cd47c8fa2271247c01cd3cec9f689398244206b2
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.1.0-py3-none-any.whl

Download URL pytextad-0.1.0-py3-none-any.whl
Size 25.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56b95026a41c062934317031542f59697e4605e4d32cbcbc869161d2e80da58b
BLAKE2b-256 checksum
How to use checksums
3b02cc1af7fd392e322ef71ddd784cfca07583937e75eb90c63c3f32ebf98afa
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

0.2.0

2 release files

This release

0.1.0 This release

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