Files
deepset-ai--haystack/docs-website/reference_versioned_docs/version-2.27/integrations-api/spacy.md
T
wehub-resource-sync c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:22:28 +08:00

3.7 KiB
Raw Blame History

title, id, description, slug
title id description slug
Spacy integrations-spacy Spacy integration for Haystack /integrations-spacy

haystack_integrations.components.extractors.spacy.named_entity_extractor

NamedEntityAnnotation

Describes a single NER annotation.

Parameters:

  • entity (str) Entity label.
  • start (int) Start index of the entity in the document.
  • end (int) End index of the entity in the document.
  • score (float | None) Score calculated by the model.

SpacyNamedEntityExtractor

Annotates named entities in a collection of documents.

The component can be used with any spaCy model that contains an NER component. Annotations are stored as metadata in the documents.

Usage example:

from haystack import Document

from haystack_integrations.components.extractors.spacy import SpacyNamedEntityExtractor

documents = [
    Document(content="I'm Merlin, the happy pig!"),
    Document(content="My name is Clara and I live in Berkeley, California."),
]
extractor = SpacyNamedEntityExtractor(model="en_core_web_sm")
results = extractor.run(documents=documents)["documents"]
annotations = [SpacyNamedEntityExtractor.get_stored_annotations(doc) for doc in results]
print(annotations)

init

__init__(
    *,
    model: str,
    pipeline_kwargs: dict[str, Any] | None = None,
    device: ComponentDevice | None = None
) -> None

Create a Named Entity extractor component.

Parameters:

  • model (str) Name of the spaCy model or a path to the model on the local disk.
  • pipeline_kwargs (dict[str, Any] | None) Keyword arguments passed to the pipeline. The pipeline can override these arguments.
  • device (ComponentDevice | None) The device on which the model is loaded. If None, the default device is automatically selected.

Raises:

  • ValueError If the device represents multiple devices, which the spaCy backend does not support.

warm_up

warm_up() -> None

Initialize the component.

Raises:

  • ComponentError If the component fails to initialize successfully.

run

run(documents: list[Document], batch_size: int = 1) -> dict[str, Any]

Annotate named entities in each document and store the annotations in the document's metadata.

Parameters:

  • documents (list[Document]) Documents to process.
  • batch_size (int) Batch size used for processing the documents.

Returns:

  • dict[str, Any] Processed documents.

Raises:

  • ComponentError If the model fails to process a document.

to_dict

to_dict() -> dict[str, Any]

Serializes the component to a dictionary.

Returns:

  • dict[str, Any] Dictionary with serialized data.

from_dict

from_dict(data: dict[str, Any]) -> SpacyNamedEntityExtractor

Deserializes the component from a dictionary.

Parameters:

  • data (dict[str, Any]) Dictionary to deserialize from.

Returns:

  • SpacyNamedEntityExtractor Deserialized component.

initialized

initialized: bool

Returns if the extractor is ready to annotate text.

get_stored_annotations

get_stored_annotations(
    document: Document,
) -> list[NamedEntityAnnotation] | None

Returns the document's named entity annotations stored in its metadata, if any.

Parameters:

  • document (Document) Document whose annotations are to be fetched.

Returns:

  • list[NamedEntityAnnotation] | None The stored annotations.