Registry / ai-ml / spacy-transformers

spacy-transformers

JSON →
library1.4.0pypypiunverified

The `spacy-transformers` library provides spaCy components and architectures to seamlessly integrate pre-trained transformer models from Hugging Face's `transformers` library into spaCy pipelines. It enables convenient access to state-of-the-art architectures like BERT, GPT-2, and XLNet for various NLP tasks, leveraging spaCy v3's powerful and extensible configuration system for multi-task learning. The current version is 1.4.0, and releases are generally aligned with spaCy's major version updates and `transformers` library advancements.

pip install 'spacy[transformers]' python -m spacy download en_core_web_trf
INSTALL
IMPORT
SIG · SPACY-TRANSFORMERS
S
spacy-transformers
ai-mlpythonv1.4.0
Install
—
Import
—
Disk
—
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.3.9 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ timeout
✕ timeout
py 3.11
✕ build_error
✕ timeout
py 3.12
✕ build_error
4/8 runs
py 3.13
✕ build_error
✕ dependency_conflict
py 3.9
✕ no_wheel
✕ no_wheel
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Transformer
✓ from spacy_transformers import Transformer
✗ from spacy_transformers import Transformer

This quickstart demonstrates loading a pre-trained, transformer-backed spaCy model (like `en_core_web_trf`) and processing text to extract entities, showcasing the integration. It also briefly touches on accessing the transformer's vector outputs, which power subsequent spaCy components.

import spacy # Ensure you have downloaded a transformer-backed model, e.g., using: # python -m spacy download en_core_web_trf nlp = spacy.load("en_core_web_trf") text = "Apple is acquiring a London-based AI startup for $200M." doc = nlp(text) print(f"Text: {text}") print(f"Entities: {[(ent.text, ent.label_) for ent in doc.ents]}") # Accessing transformer output (e.g., pooled vector for the doc) # Note: Raw transformer outputs are typically stored in doc._.trf_data or doc.tensor if doc.has_annotation("SENT_START"): # Check if sentencizer is in pipeline print(f"Document vector (first token of first sentence): {doc[0].vector[:5]}") # First 5 elements of vector
Debug
Known issues
breaking`spacy-transformers` underwent a significant refactoring for spaCy v3.0+. Versions 0.6.x and earlier (for spaCy v2.x) are incompatible with v1.x and later (for spaCy v3.x). Pipelines trained with v0.x will not work with v1.x.
fix
Upgrade your spaCy installation to v3.0+ and then install `spacy-transformers` v1.x. Retrain any custom pipelines or download compatible `_trf` models for spaCy v3.
affects: <1.0.0
gotchaStrict version compatibility exists between `spacy-transformers` and `spaCy`. For example, `spacy-transformers` v1.2.x requires `spaCy` v3.5.0+. Installing incompatible versions can lead to unexpected errors or warnings about pipeline incompatibility.
fix
Always check the `spacy-transformers` documentation or `pyproject.toml` for the exact `spaCy` version requirements. Use `pip install 'spacy[transformers]'` to let pip resolve compatible versions. Run `python -m spacy validate` to check installed package compatibility.
affects: All versions
gotchaThe `Transformer` component in `spacy-transformers` acts as a feature extractor, providing contextual embeddings to downstream spaCy components. It does not natively expose task-specific heads (e.g., for text classification or token classification) from the Hugging Face model for direct inference or training.
fix
To use task-specific heads, either train separate spaCy components (like `textcat` or `ner`) that consume the transformer features, or consider `spacy-huggingface-pipelines` for direct integration of task-specific Hugging Face models.
affects: All versions
gotchaTransformer models are computationally intensive and memory-hungry. Training and inference, especially with larger models or long documents, are significantly slower on CPU and often require a GPU (with CUDA) for practical performance. Memory issues ('CUDA out of memory') are common.
fix
For GPU, ensure `spacy[transformers,cudaXX]` is installed with the correct PyTorch CUDA build. Reduce `batch_size` and `max_length` in your config. Consider smaller transformer models (e.g., DistilBERT). For very long documents, `spacy-transformers` handles sentence splitting internally, but excessive length can still be an issue. Use `nlp.pipe(texts, batch_size=...)` for efficient batch processing.
affects: All versions
Upgrade
Version history
1.4.0latest on PyPI · released Mar 17, 2026
Audit
Dependencies
spacyrequiredCore NLP library; spacy-transformers requires spaCy v3.0+ and has specific minor version compatibility.
transformersrequiredThe underlying library providing access to pre-trained transformer models from Hugging Face.
torchrequiredBackend deep learning framework for transformer models. PyTorch is typically installed automatically but specific CUDA versions may require manual installation.
cupyoptionalProvides GPU support for spaCy's Thinc (and thus spacy-transformers) when using CUDA, installed via spaCy's `[cudaXX]` extras.
Agent activity
35 hits · last 30 days
node
32
OpenAI (training)
1
Resources
spacy-transformers — pip install spacy-transformers · libregistry