spaCy Legacy

3.0.12 · active · verified Sun Apr 05

spacy-legacy is a Python package that provides outdated registered functions and architectures for spaCy v3.x, ensuring backwards compatibility for projects that rely on older component implementations. It is automatically installed as a dependency of the main spaCy library. The package's releases are typically tied to major spaCy updates where core components undergo backwards-incompatible changes, allowing older configurations and trained models to continue functioning. The current version is 3.0.12, released in January 2023.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how `spacy-legacy` allows spaCy to load configurations that refer to older, deprecated component architectures. When `spaCy` encounters `@architectures = "spacy.Tok2Vec.v1"` in a config, and `v1` is no longer in the core library, `spacy-legacy` provides the necessary implementation, ensuring the pipeline loads without error.

import spacy
import os
from spacy.util import load_config_from_str

# This config uses 'spacy.Tok2Vec.v1', an architecture moved to spacy-legacy
config_content = """
[paths]
vocab = null # Essential if not using pre-trained vectors, otherwise spacy might expect them

[nlp]
lang = "en"
pipeline = ["tok2vec"]

[components]
[components.tok2vec]
factory = "tok2vec"

[components.tok2vec.model]
@architectures = "spacy.Tok2Vec.v1"
width = 96
embed_size = 2000
"""

# Save config to a temporary file
config_path = "temp_legacy_config.cfg"
with open(config_path, "w") as f:
    f.write(config_content)

try:
    # spaCy will automatically resolve 'spacy.Tok2Vec.v1' to spacy-legacy's implementation
    print(f"Attempting to load pipeline using config from {config_path}...")
    nlp = spacy.load(config_path)
    print("Pipeline loaded successfully, leveraging spacy-legacy for 'Tok2Vec.v1'.")

    doc = nlp("This is a demonstration of spacy-legacy in action.")
    print(f"Processed text: {doc.text}")
    print(f"Number of tokens: {len(doc)}")

except Exception as e:
    print(f"An error occurred while loading the pipeline: {e}")
    print("Ensure spacy and spacy-legacy are installed. If spaCy's core architecture")
    print("for Tok2Vec.v1 has completely changed its signature, this example might need adjustment.")
finally:
    if os.path.exists(config_path):
        os.remove(config_path)

view raw JSON →