Skip to main content

PyTextAD

PyTextAD: Text Anomaly Detection in Python

PyPI Documentation Tests Coverage License Open in Colab Demo

PyTextAD is a Python library for detecting anomalies in text, both whole anomalous documents and the individual tokens that make them anomalous.

  • 23 detectors with one interface: fit, decision_function, predict.
  • Token-level detection: any detector scores every token of a document, and token scores are aggregated into document scores.
  • Faithful implementations: each detector is the authors' code, or is checked numerically against it.
  • Benchmark datasets with a label for every word, and evaluation at the token and document levels.

Documentation: https://pytextad.readthedocs.io

Online demo (no installation): https://huggingface.co/spaces/Charles-Cao/pytextad

Citing PyTextAD

If you use PyTextAD, please cite it, together with the papers of the detectors you use (listed under References):

@software{cao2026pytextad,
  author = {Cao, Yang},
  title  = {{PyTextAD}: Text Anomaly Detection in Python},
  year   = {2026},
  url    = {https://github.com/charles-cao/pytextad}
}

If you use the built-in datasets, please also cite Towards Token-Level Text Anomaly Detection (WWW 2026), listed under References.

Installation

pip install pytextad        # install
pip install -U pytextad     # upgrade to the latest release

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

Quick start

from pytextad.datasets import load_dataset

ds = load_dataset("restaurant_review")
normal, anomalous = ds.normal_indices, ds.anomalous_indices
train = ds.subset(normal[:500])                         # 500 normal reviews
test = ds.subset(list(normal[500:]) + list(anomalous))  # the other reviews

Document level: one vector per document.

from pytextad import SIK, DocumentDetector, SentenceEmbedder

det = DocumentDetector(SIK(), embedder=SentenceEmbedder("bert-base-uncased"))
det.fit(train.texts)
doc_scores = det.decision_function(test.texts)          # higher = more anomalous

# with document vectors you already have ([n_documents, dim] arrays), use the detector directly
doc_scores = SIK().fit(X_train_docs).decision_function(X_test_docs)

Token level: one score per word, evaluated at the token and document levels.

from pytextad import SIK, TokenDetector, TokenEmbedder, align_labels
from pytextad.metrics import evaluate, format_results

emb = TokenEmbedder("bert-base-uncased")              # one vector per sub-word
X_train = emb.transform(train.tokens)                 # (vectors, word index of each sub-word)
X_test = emb.transform(test.tokens)
y_test = align_labels(test.token_labels, X_test[1])  # drops words cut off at 512 sub-words

# every sub-word is scored; a word's score is the max over its sub-words
result = evaluate(TokenDetector(SIK()), X_train, X_test, token_labels=y_test)
print(format_results({"SIK": result}))     # token and document AUROC, AP, FPR95

Implemented algorithms

Text detectors take text (or token embeddings) and are trained end to end.

Abbr Algorithm Year Token scores Ref
CVDD Context Vector Data Description 2019 yes [1]
DATE Detecting Anomalies in Text via Self-Supervision of Transformers 2021 yes [2]
FATE Few-shot Anomaly Detection in Text with Deviation Learning 2023 no [3]

Embedding detectors take one vector per document or per token. Wrapped in TokenDetector, each of them scores every sub-word, and sub-word scores are combined into word scores.

Abbr Algorithm Year Ref
NormalizingFlow Planar normalizing flow 2015 [4]
DAGMM Deep Autoencoding Gaussian Mixture Model 2018 [5]
GANomaly Adversarially trained encoder-decoder-encoder 2018 [6]
RSRAE Robust Subspace Recovery AutoEncoder 2020 [7]
GOAD Classification-based anomaly detection with random transformations 2020 [8]
DROCC Distributionally Robust One-Class Classifier 2020 [9]
ICL Internal Contrastive Learning 2022 [10]
SLAD Scale Learning-based Anomaly Detection 2023 [11]
DTE Diffusion Time Estimation (categorical, inverse-gamma, Gaussian, non-parametric) 2024 [12]
DDPM Denoising diffusion model, reconstruction error 2024 [12]
MCM Masked Cell Modeling 2024 [13]
DRL Decomposed Representation Learning 2025 [14]
DDAE Diffusion-Scheduled Denoising Autoencoder 2025 [15]
SIK Simplified Isolation Kernel 2025 [16]
ADERH Ensemble of Random Pairs of Hyperspheres 2025 [17]
TCCM Time-Conditioned Contraction Matching 2025 [18]
TokenCore Nearest-neighbour memory bank of token embeddings 2026 [19]

Wrappers turn any embedding detector with fit and decision_function, including those of PyOD [20], into a text detector: DocumentDetector (one embedding per document) and TokenDetector (one embedding per token).

from pyod.models.iforest import IForest      # pip install pyod
result = evaluate(TokenDetector(IForest()), X_train, X_test, token_labels=y_test)

Datasets

Six datasets with a 0/1 label for every word, downloaded once from the Hugging Face Hub.

from pytextad.datasets import load_dataset
ds = load_dataset("restaurant_review")
ds.tokens, ds.token_labels, ds.labels      # words, word labels, document labels
Dataset Documents Anomalous Anomaly
sms_spam 4,518 393 injected gibberish
restaurant_review 1,100 50 negative sentiment
grammar_correction 300 30 grammatical errors
hate_speech 4,302 140 hateful or offensive words
olid 650 30 offensive words
restaurant_review2 520 25 negative sentiment

License

BSD 2-Clause, except for some third-party code under its own licence (CC BY-SA 4.0, and a research-only licence for GOAD); see THIRD_PARTY_NOTICES.md.

Acknowledgements

Parts of the code, tests and documentation were written with the help of Claude (Anthropic).

References

[1] L. Ruff, Y. Zemlyanskiy, R. Vandermeulen, T. Schnake, M. Kloft. Self-Attentive, Multi-Context One-Class Classification for Unsupervised Anomaly Detection on Text. ACL, 2019. ACL Anthology

[2] A. Manolache, F. Brad, E. Burceanu. DATE: Detecting Anomalies in Text via Self-Supervision of Transformers. NAACL, 2021. ACL Anthology

[3] A. S. Das, A. Ajay, S. Saha, M. Bhuyan. Few-shot Anomaly Detection in Text with Deviation Learning. ICONIP, 2023. arXiv:2308.11780

[4] D. J. Rezende, S. Mohamed. Variational Inference with Normalizing Flows. ICML, 2015. PMLR

[5] B. Zong, Q. Song, M. R. Min, W. Cheng, C. Lumezanu, D. Cho, H. Chen. Deep Autoencoding Gaussian Mixture Model for Unsupervised Anomaly Detection. ICLR, 2018. OpenReview

[6] S. Akcay, A. Atapour-Abarghouei, T. P. Breckon. GANomaly: Semi-Supervised Anomaly Detection via Adversarial Training. ACCV, 2018. arXiv:1805.06725

[7] C.-H. Lai, D. Zou, G. Lerman. Robust Subspace Recovery Layer for Unsupervised Anomaly Detection. ICLR, 2020. arXiv:1904.00152

[8] L. Bergman, Y. Hoshen. Classification-Based Anomaly Detection for General Data. ICLR, 2020. arXiv:2005.02359

[9] S. Goyal, A. Raghunathan, M. Jain, H. V. Simhadri, P. Jain. DROCC: Deep Robust One-Class Classification. ICML, 2020. PMLR

[10] T. Shenkar, L. Wolf. Anomaly Detection for Tabular Data with Internal Contrastive Learning. ICLR, 2022. ICLR

[11] H. Xu, Y. Wang, J. Wei, S. Jian, Y. Li, N. Liu. Fascinating Supervisory Signals and Where to Find Them: Deep Anomaly Detection with Scale Learning. ICML, 2023. PMLR

[12] V. Livernoche, V. Jain, Y. Hezaveh, S. Ravanbakhsh. On Diffusion Modeling for Anomaly Detection. ICLR, 2024. arXiv:2305.18593

[13] J. Yin, Y. Qiao, Z. Zhou, X. Wang, J. Yang. MCM: Masked Cell Modeling for Anomaly Detection in Tabular Data. ICLR, 2024. ICLR

[14] H. Ye, H. Zhao, W. Fan, M. Zhou, D. Guo, Y. Chang. DRL: Decomposed Representation Learning for Tabular Anomaly Detection. ICLR, 2025. ICLR

[15] T. Sattarov, M. Schreyer, D. Borth. Diffusion-Scheduled Denoising Autoencoders for Anomaly Detection in Tabular Data. KDD, 2025. arXiv:2508.00758

[16] Y. Cao, S. Yang, Y. Yang, L. Qi, M. Liu. Text Anomaly Detection with Simplified Isolation Kernel. Findings of EMNLP, 2025. doi:10.18653/v1/2025.findings-emnlp.680

[17] W. Durani, C. Leiber, K. Durani, C. Plant, C. Böhm. Anomaly Detection by an Ensemble of Random Pairs of Hyperspheres. NeurIPS, 2025. NeurIPS

[18] Z. Li, Q. Huang, Y. Zhu, L. Yang, M. M. Amiri, N. van Stein, M. van Leeuwen. Scalable, Explainable and Provably Robust Anomaly Detection with One-Step Flow Matching. NeurIPS, 2025. arXiv:2510.18328

[19] Y. Cao, B. Yu, S. Yang, M. Liu, Y. Yang. Towards Token-Level Text Anomaly Detection. The ACM Web Conference (WWW), 2026. doi:10.1145/3774904.3792952

[20] Y. Zhao, Z. Nasrullah, Z. Li. PyOD: A Python Toolbox for Scalable Outlier Detection. JMLR, 20(96):1-7, 2019. JMLR

Metadata

Release files for pytextad 0.3.4

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.3.4
File Size Uploaded Provenance Actions
pytextad-0.3.4.tar.gz 117.0 kB With provenance Details for pytextad-0.3.4.tar.gz

Built distribution (wheel)

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

Total release size: 242.3 kB

Release files / pytextad-0.3.4.tar.gz

Download URL pytextad-0.3.4.tar.gz
Size 117.0 kB
Tags Source
SHA-256 checksum
How to use checksums
eb2970f04c92fffda451b4cf2dcd4a46007ae356b7c58b3911fda791b413b70e
BLAKE2b-256 checksum
How to use checksums
f6474a053da71bdc68d27e568e4c036390f787b935a1b77ee122f77dff4afd04
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 13, 2026.

Transparency log

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

Download URL pytextad-0.3.4-py3-none-any.whl
Size 125.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
82a1c960c8a2f0b50830cfbbda696c66fa557b158d1fe806cd462b81b5d31d3a
BLAKE2b-256 checksum
How to use checksums
a50ae79b5af3e5e971c5dcc29148ffd4ce773c0d5c09e585d1d4ec7ff7149e16
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 13, 2026.

Transparency log

Security for pytextad 0.3.4

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pytextad-0.3.4.tar.gz

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 13, 2026.

Transparency log

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.

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